<?php

class Wiaas_Admin_Order_Projects {

	public static function init() {
		// Add admin page and subpage since woocommerce orders have custom menu page
		// so this will not be automatic
		add_filter('admin_menu', array(__CLASS__, 'add_admin_page'));

		// Add is available fields to create and edit forms
		add_action( 'shop_order_project_add_form_fields', array( __CLASS__, 'add_is_available_field' ) );
		add_action( 'shop_order_project_edit_form_fields', array( __CLASS__, 'edit_is_available_field' ) );

		// Add is available column
		add_filter( 'manage_edit-shop_order_project_columns', array( __CLASS__, 'update_table_headers' ) );
		add_filter( 'manage_shop_order_project_custom_column', array( __CLASS__, 'update_table_column_content' ), 10, 3 );

		// Save is available field when creating and editing
		add_action( 'created_term', array( __CLASS__, 'save_is_available_field' ), 10, 3 );
		add_action( 'edit_term', array( __CLASS__, 'save_is_available_field' ), 10, 3 );
	}

	/**
	 * Add admin submenu page for order projects
	 */
	public static function add_admin_page() {
		add_submenu_page( 'woocommerce',
			esc_html__( 'Order projects', 'wiaas' ),
			esc_html__( 'Order projects', 'wiaas' ),
			'manage_woocommerce',
			'edit-tags.php?taxonomy=shop_order_project'
		);
	}

	/**
	 * Add is available field to order project creation form
	 */
	public static function add_is_available_field() {
		?>
		<div class="form-field form-required">
			<input
				type="checkbox"
				class="postform"
				style="float:left; margin-top: 4px; margin-right: 10px;"
				id="is_available"
				name="is_available"
			/>
			<label for="is_available"><?php _e( 'Is Available?', 'wiaas' ); ?></label>
		</div>
		<?php
	}

	/**
	 * Add is available field to order project edit form
	 * @param $order_project
	 */
	public static function edit_is_available_field($order_project) {
		?>
		<tr class="form-field">
			<th scope="row" valign="top"><label><?php _e( 'Is Available?', 'wiaas' ); ?></label></th>
			<td>
				<input
					type="checkbox"
					class="checkbox"
					id="is_available"
					name="is_available"
					<?php
					checked( true, Wiaas_Order_Project::is_order_project_available($order_project), true )
					?>
				/>
			</td>
		</tr>
		<?php
	}

	/**
	 * Update table headers for order projects
     * Will add `is available` and `wiaas count` columns and remove default count column
     *
	 * @param array $columns Available order project table headers
	 *
	 * @return array Edited order project table headers
	 */
	public static function update_table_headers($columns) {
		$columns['wiaas_is_available'] = __( 'Is Available?', 'wiaas' );

		$columns['wiaas_count'] = __('Count', 'wiaas');

		unset($columns['posts']);

		return $columns;
	}

	/**
	 * Render content for order project table column
     * Will add content for `is available` and `wiaas count` columns
     *
	 * @param string $columns  Row content for order projects table
	 * @param string $column   Current column header
	 * @param int $id          Order project id
	 *
	 * @return string          Edited row content with appended is available info
	 */
	public static function update_table_column_content($columns, $column, $id) {
		if ($column === 'wiaas_is_available') {
			$is_available = Wiaas_Order_Project::is_order_project_available($id);
			$columns .=  $is_available ? 'Yes' : 'No';
		}

		if ($column === 'wiaas_count') {
		    $term = get_term($id, 'shop_order_project');
		    $args = array(
                'shop_order_project' => $term->slug,
                'post_type' => 'shop_order'
            );
		    $columns .= "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$term->count</a>";
        }

		return $columns;
	}

	/**
	 * Save posted is available field for created or edited order project
	 * @param $term_id
	 * @param string $tt_id
	 * @param string $taxonomy
	 */
	public static function save_is_available_field($term_id, $tt_id = '', $taxonomy = '') {
		if ( 'shop_order_project' === $taxonomy ) {
			Wiaas_Order_Project::set_is_order_project_available($term_id, $_POST['is_available'] === 'on');
		}
	}
}

Wiaas_Admin_Order_Projects::init();
