<?php
include 'inc/metadata-functions.php';
class EX_Food_Taxonomy {
	public function __construct()
    {
		add_action( 'init', array( $this, 'register_category_taxonomies' ) );
		add_action( 'init', array( $this, 'register_location_taxonomies' ) );
		add_action( 'cmb2_admin_init', array( $this,'register_taxonomy_category_metabox') );
		add_filter( 'manage_edit-product_cat_columns', array( $this,'_edit_columns_exfood_menu'));
		add_action( 'manage_product_cat_custom_column', array( $this,'_custom_columns_content_exfood_menu'),10,3);
    }
	function register_category_taxonomies(){
		$labels = array(
			'name'              => esc_html__( 'Food Menu', 'woocommerce-food' ),
			'singular_name'     => esc_html__( 'Food Menu', 'woocommerce-food' ),
			'search_items'      => esc_html__( 'Food Menu','woocommerce-food' ),
			'all_items'         => esc_html__( 'All Menu','woocommerce-food' ),
			'parent_item'       => esc_html__( 'Parent Menu' ,'woocommerce-food'),
			'parent_item_colon' => esc_html__( 'Parent Menu:','woocommerce-food' ),
			'edit_item'         => esc_html__( 'Edit Menu' ,'woocommerce-food'),
			'update_item'       => esc_html__( 'Update Menu','woocommerce-food' ),
			'add_new_item'      => esc_html__( 'Add New Menu' ,'woocommerce-food'),
			'menu_name'         => esc_html__( 'Food Menus','woocommerce-food' ),
		);			
		$args = array(
			'hierarchical'      => true,
			'labels'            => $labels,
			'show_ui'           => true,
			'show_admin_column' => true,
			'query_var'         => true,
			'rewrite'           => array( 'slug' => 'food-menu' ),
		);
		
	}
	function register_location_taxonomies(){
		$labels = array(
			'name'              => esc_html__( 'Location', 'woocommerce-food' ),
			'singular_name'     => esc_html__( 'Location', 'woocommerce-food' ),
			'search_items'      => esc_html__( 'Location','woocommerce-food' ),
			'all_items'         => esc_html__( 'All Location','woocommerce-food' ),
			'parent_item'       => esc_html__( 'Parent Location' ,'woocommerce-food'),
			'parent_item_colon' => esc_html__( 'Parent Location:','woocommerce-food' ),
			'edit_item'         => esc_html__( 'Edit Location' ,'woocommerce-food'),
			'update_item'       => esc_html__( 'Update Location','woocommerce-food' ),
			'add_new_item'      => esc_html__( 'Add New Location' ,'woocommerce-food'),
			'menu_name'         => esc_html__( 'Food Locations','woocommerce-food' ),
		);			
		$args = array(
			'hierarchical'      => true,
			'labels'            => $labels,
			'show_ui'           => true,
			'show_admin_column' => true,
			'query_var'         => true,
			'rewrite'           => array( 'slug' => 'food-menu' ),
		);
		$menu_byloc = 'no';//exwoofood_get_option('exwoofood_enable_loc','exwoofood_options');
		if ($menu_byloc =='yes') {
			register_taxonomy('exwoofood_loc', array( 'exwf_menubydate','product' ), $args);
		}else{
			register_taxonomy('exwoofood_loc','product', $args);
		}	
	}
	// Register email field in location
	function register_taxonomy_category_metabox() {
		$prefix = 'exwp_loc_';
		/**
		 * Metabox to add fields to categories and tags
		 */
		$exwf_log_meta = new_cmb2_box( array(
			'id'               => $prefix . 'data',
			'title'            => esc_html__( 'Category Metabox', 'woocommerce-food' ), // Doesn't output for term boxes
			'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
			'taxonomies'       => array( 'exwoofood_loc'), // Tells CMB2 which taxonomies should have these fields
			'new_term_section' => true, // Will display in the "Add New Category" section
		) );
		$exwf_log_meta->add_field( array(
			'name' => esc_html__( 'Address', 'woocommerce-food' ),
			'id'   => $prefix .'address',
			'desc' => esc_html__( 'Add full address of this location to calculate radius shipping', 'woocommerce-food' ),
			'type' => 'text',
		) );
		$exwf_log_meta->add_field( array(
			'name' => esc_html__( 'Latitude and Longitude', 'woocommerce-food' ),
			'id'   => $prefix .'address_lat_long',
			'desc' => esc_html__( 'Physical address of your store location, you can see how to find physical address here: https://support.google.com/maps/answer/18539. Enter Latitude and Longitude, separated by a comma. Ex for London: 42.9869502,-81.243177', 'woocommerce-food' ),
			'type' => 'text',
		) );
		$ship_mode = exwoofood_get_option('exwoofood_ship_mode','exwoofood_shpping_options');
		if($ship_mode!='postcode'){
			$dis_restr = array(
				'name' => esc_html__( 'Distance restrict (km)', 'woocommerce-food' ),
				'id'   => $prefix .'diskm',
				'desc' => esc_html__( 'Enter number of kilometer to restrict delivery for this location, leave blank to use value from setting page', 'woocommerce-food' ),
				'type' => 'text',
			);
			$dis_restr = apply_filters('exwf_op_dis_restr_byloc',$dis_restr);
			$exwf_log_meta->add_field( $dis_restr );
		}
		$exwf_log_meta->add_field( array(
			'name' => esc_html__( 'Minimum Order Amount required', 'woocommerce-food' ),
			'id'   => $prefix .'min_amount',
			'desc' => esc_html__( 'Set minimum amount required for this location', 'woocommerce-food' ),
			'type' => 'text',
		) );
		$exwf_log_meta->add_field( array(
			'name' => esc_html__('Shipping fee','woocommerce-food'),
			'desc' => esc_html__('Set Shipping fee for delivery, enter number','woocommerce-food'),
			'id'   => $prefix .'ship_fee',
			'type' => 'text',
			'sanitization_cb' => '',
		) );
		$exwf_log_meta->add_field( array(
			'name' => esc_html__('Minimum order amount to free shipping','woocommerce-food'),
			'desc' => esc_html__('Enter number','woocommerce-food'),
			'id'   => $prefix .'ship_free',
			'type' => 'text',
			'sanitization_cb' => '',
		) );
		$exwf_log_meta->add_field( array(
			'name' => esc_html__( 'Email recipients', 'woocommerce-food' ),
			'id'   => $prefix .'email',
			'desc' => esc_html__( 'Set email to get notification when user order food from this location', 'woocommerce-food' ),
			'type' => 'text',
		) );
		$exwf_log_meta->add_field( array(
			'name' => esc_html__( 'Hide menu/category filter', 'woocommerce-food' ),
			'id'   => $prefix .'hide_menu',
			'desc' => esc_html__( 'Select menu/category filter to hide from this location', 'woocommerce-food' ),
			'taxonomy'       => 'product_cat',
			'type'           => 'taxonomy_multicheck_inline',
			'remove_default' => 'true', // Removes the default metabox provided by WP core.
			'select_all_button' => false,
			'query_args' => array(
				// 'orderby' => 'slug',
				// 'hide_empty' => true,
			),
			'classes'		 => 'cmb-type-taxonomy-multicheck-inline',
		) );
		// disable day,date
		/*
		$exwf_log_meta->add_field( array(
			'name'       => esc_html__( 'Timezone', 'woocommerce-food' ),
			'desc'       => esc_html__( 'Select special Timezone for this Location', 'woocommerce-food' ),
			'id'         => $prefix .'timezone',
			'type'       => 'select',
			'default'    => '',
			'classes'		 => '',
			'after_field'  => '',
			'options'          => array(
				'' => esc_html__('Default', 'woocommerce-food'), 
				'-12' => esc_html__('UTC-12', 'woocommerce-food'), 
				'-11.5' => esc_html__('UTC-11:30', 'woocommerce-food'),
				'-11' => esc_html__('UTC-11', 'woocommerce-food'),
				'-10.5' => esc_html__('UTC-10:30', 'woocommerce-food'),
				'-10' => esc_html__('UTC-10', 'woocommerce-food'),
				'-9.5' => esc_html__('UTC-9:30', 'woocommerce-food'),
				'-9' => esc_html__('UTC-9', 'woocommerce-food'),
				'-8.5' => esc_html__('UTC-8:30', 'woocommerce-food'),
				'-8' => esc_html__('UTC-8', 'woocommerce-food'),
				'-7.5' => esc_html__('UTC-7:30', 'woocommerce-food'),
				'-7' => esc_html__('UTC-7', 'woocommerce-food'),
				'-6.5' => esc_html__('UTC-6:30', 'woocommerce-food'),
				'-6' => esc_html__('UTC-6', 'woocommerce-food'),
				'-5.5' => esc_html__('UTC-5:30', 'woocommerce-food'),
				'-5' => esc_html__('UTC-5', 'woocommerce-food'),
				'-4.5' => esc_html__('UTC-4:30', 'woocommerce-food'),
				'-4' => esc_html__('UTC-4', 'woocommerce-food'),
				'-3.5' => esc_html__('UTC-3:30', 'woocommerce-food'),
				'-3' => esc_html__('UTC-3', 'woocommerce-food'),
				'-2.5' => esc_html__('UTC-2:30', 'woocommerce-food'),
				'-2' => esc_html__('UTC-2', 'woocommerce-food'),
				'-1.5' => esc_html__('UTC-1:30', 'woocommerce-food'),
				'-1' => esc_html__('UTC-1', 'woocommerce-food'),
				'-0.5' => esc_html__('UTC-0:30', 'woocommerce-food'),
				'+0' => esc_html__('UTC+0', 'woocommerce-food'),
				'0.5' => esc_html__('UTC+0:30', 'woocommerce-food'),
				'1' => esc_html__('UTC+1', 'woocommerce-food'),
				'1.5' => esc_html__('UTC+1:30', 'woocommerce-food'),
				'2' => esc_html__('UTC+2', 'woocommerce-food'),
				'2.5' => esc_html__('UTC+2:30', 'woocommerce-food'),
				'3' => esc_html__('UTC+3', 'woocommerce-food'),
				'3.5' => esc_html__('UTC+3:30', 'woocommerce-food'),
				'4' => esc_html__('UTC+4', 'woocommerce-food'),
				'4.5' => esc_html__('UTC+4:30', 'woocommerce-food'),
				'5' => esc_html__('UTC+5', 'woocommerce-food'),
				'5.30' => esc_html__('UTC+5:30', 'woocommerce-food'),
				'5.45' => esc_html__('UTC+5:45', 'woocommerce-food'),
				'6' => esc_html__('UTC+6', 'woocommerce-food'),
				'6.5' => esc_html__('UTC+6:30', 'woocommerce-food'),
				'7' => esc_html__('UTC+7', 'woocommerce-food'),
				'7.5' => esc_html__('UTC+7:30', 'woocommerce-food'),
				'8' => esc_html__('UTC+8', 'woocommerce-food'),
				'8.30' => esc_html__('UTC+8:30', 'woocommerce-food'),
				'8.45' => esc_html__('UTC+8:45', 'woocommerce-food'),
				'9' => esc_html__('UTC+9', 'woocommerce-food'),
				'9.5' => esc_html__('UTC+9:30', 'woocommerce-food'),
				'10' => esc_html__('UTC+10', 'woocommerce-food'),
				'10.30' => esc_html__('UTC+10:30', 'woocommerce-food'),
				'11' => esc_html__('UTC+11', 'woocommerce-food'),
				'11.5' => esc_html__('UTC+11:30', 'woocommerce-food'),
				'12' => esc_html__('UTC+12', 'woocommerce-food'),
				'12.45' => esc_html__('UTC+12:45', 'woocommerce-food'),
				'13' => esc_html__('UTC+13', 'woocommerce-food'),
				'13.45' => esc_html__('UTC+13:45', 'woocommerce-food'),
				'14' => esc_html__('UTC+14', 'woocommerce-food'),
			),
		) );
		*/
		$loc_advdate = exwoofood_get_option('exwoofood_advdate_loc','exwoofood_advanced_options');
		if($loc_advdate=='enable'){
			$exwf_log_meta->add_field( array(
				'name' => esc_html__('Advanced Date','woocommerce-food'),
				'desc' => esc_html__( 'Enable/disable date, day for each location (Leave blank to use values from settings page)', 'woocommerce-food' ),
				'id'   => $prefix .'adv_date',
				'type'        => 'title', 
				'before_row'     => 'exwf_locjs_limit_group_repeat',
				'rows_limit'   => 3,
    			'groupId' => $prefix .'adv_date_gr',
			) );
			$group_option = $exwf_log_meta->add_field( array(
				'id'          => $prefix .'adv_date_gr',
				'type'        => 'group',
				'description' => '',
				'options'     => array(
					'group_title'   => esc_html__( 'Rule {#}', 'woocommerce-food' ), // since version 1.1.4, {#} gets replaced by row number
					'add_button'    => esc_html__( 'Add new', 'woocommerce-food' ),
					'remove_button' => esc_html__( 'Remove', 'woocommerce-food' ),
					'sortable'      => false,
					'closed'     => false, 
				),
				'after_group' => '</div></div>',
				'before_group'     => '<div class="exwf-collapse-con">',
				'classes'		 => 'exwf-loc-advdate',
			) );
			$exwf_log_meta->add_group_field( $group_option, array(
				'name'             => esc_html__( 'Shipping method', 'woocommerce-food' ),
				'desc'             => esc_html__( 'Select Shipping method for this rule, select Default to apply for all methods', 'woocommerce-food' ),
				'id'               => 'deli_method',
				'type'             => 'select',
				'show_option_none' => false,
				'options'          => array(
					''   => esc_html__( 'Default', 'woocommerce-food' ),
					'takeaway'   => esc_html__( 'Only Takeaway', 'woocommerce-food' ),
					'delivery'   => esc_html__( 'Only Delivery', 'woocommerce-food' ),
					'dinein'   => esc_html__( 'Only Dine-in', 'woocommerce-food' ),
				),
			) );
			$exwf_log_meta->add_group_field( $group_option, array(
				'name'       => esc_html__( 'Disable dates', 'woocommerce-food' ),
				'desc'       => esc_html__( 'Disable special Delivery Date', 'woocommerce-food' ),
				'id'         => 'exwoofood_ck_disdate',
				'type' => 'text_date_timestamp',
				'default'          => '',
				'date_format' => 'Y-m-d',
				'repeatable'     => true,
				'show_option_none' => true,
			) );
			$exwf_log_meta->add_group_field( $group_option, array(
				'name'       => esc_html__( 'Enable Special delivery dates', 'woocommerce-food' ),
				'desc'       => esc_html__( 'Add dates to allow user only can select these special Delivery Dates (only support display delivery date in Select box)', 'woocommerce-food' ),
				'id'         => 'exwoofood_ck_enadate',
				'type' => 'text_date_timestamp',
				'default'          => '',
				'date_format' => 'Y-m-d',
				'repeatable'     => true,
				'show_option_none' => true,
			) );
			$exwf_log_meta->add_group_field( $group_option, array(
				'name'       => esc_html__( 'Disable days', 'woocommerce-food' ),
				'desc'       => esc_html__( 'Disable special Day Delivery', 'woocommerce-food' ),
				'id'         => 'exwoofood_ck_disday',
				'type' => 'multicheck_inline',
				'options' => array(
					'1' => esc_html__( 'Monday', 'woocommerce-food' ),
					'2' => esc_html__( 'Tuesday', 'woocommerce-food' ),
					'3' => esc_html__( 'Wednesday', 'woocommerce-food' ),
					'4' => esc_html__( 'Thursday', 'woocommerce-food' ),
					'5' => esc_html__( 'Friday', 'woocommerce-food' ),
					'6' => esc_html__( 'Saturday', 'woocommerce-food' ),
					'7' => esc_html__( 'Sunday', 'woocommerce-food' ),
				),
				'after_row'     => '',
			) );
		}
		// Open close time
		$loc_opcls = exwoofood_get_option('exwoofood_open_close_loc','exwoofood_advanced_options');
		if($loc_opcls=='yes'){
			$exwf_log_meta->add_field( array(
				'name' => esc_html__('Opening and Closing time','woocommerce-food'),
				'desc' => esc_html__('Leave blank to use values from settings page','woocommerce-food'),
				'id'   => 'exwfood_op_cl',
				'type'        => 'title', 
				'before_row'     => '<div class="exwf-collapse">',
			) );
			$exwf_log_meta->add_field(  array(
				'name' => esc_html__( 'Closed?', 'woocommerce-food' ),
				'description' => esc_html__( 'Select yes to set this location is closed', 'woocommerce-food' ),
				'id'   => 'exwfood_loc_closed',
				'type' => 'select',
				'show_option_none' => false,
				'default' => '',
				'options'          => array(
					'' => esc_html__( 'No', 'woocommerce-food' ),
					'yes'   => esc_html__( 'Yes', 'woocommerce-food' ),
				),
				'before_row'     => '<div class="exwf-collapse-con">',
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Monday', 'woocommerce-food' ),
				'id'         => 'exwfood_Mon_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Tuesday', 'woocommerce-food' ),
				'id'         => 'exwfood_Tue_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Wednesday', 'woocommerce-food' ),
				'id'         => 'exwfood_Wed_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Thursday', 'woocommerce-food' ),
				'id'         => 'exwfood_Thu_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Friday', 'woocommerce-food' ),
				'id'         => 'exwfood_Fri_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
				
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Saturday', 'woocommerce-food' ),
				'id'         => 'exwfood_Sat_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
				
			) );
			$exwf_log_meta->add_field( array(
				'name'       => esc_html__( 'Sunday', 'woocommerce-food' ),
				'id'         => 'exwfood_Sun_opcl_time',
				'type' => 'openclose',
				'time_format' => 'H:i',
				'repeatable'     => true,
				'attributes' => array(
					'data-timepicker' => json_encode( array(
						'stepMinute' => 1,
			            'timeFormat' => 'HH:mm'
					) ),
				),
				'after_row'     => '</div></div>',
			) );
		}
		$loc_shipkm = exwoofood_get_option('exwfood_km_loc','exwoofood_shpping_options');
		if($ship_mode!='postcode' && $loc_shipkm=='yes'){
			$exwf_log_meta->add_field( array(
				'name' => esc_html__('Shipping fee by km','woocommerce-food'),
				'desc' => esc_html__('Leave blank to use value from setting page','woocommerce-food'),
				'id'   => 'exwfood_sh_km',
				'type'        => 'title', 
				'before_row'     => '<div class="exwf-collapse">',
			) );
			$feebykm_option = $exwf_log_meta->add_field( array(
				'id'          => 'exwfood_adv_feekm',
				'type'        => 'group',
				'description' => esc_html__( 'Set shipping fee by km, leave blank to use default shipping fee above', 'woocommerce-food' ),
				// 'repeatable'  => false, // use false if you want non-repeatable group
				'options'     => array(
					'group_title'   => esc_html__( 'Shipping fee by km {#}', 'woocommerce-food' ), // since version 1.1.4, {#} gets replaced by row number
					'add_button'    => esc_html__( 'Add new', 'woocommerce-food' ),
					'remove_button' => esc_html__( 'Remove', 'woocommerce-food' ),
					'sortable'      => true, // beta
					'closed'     => false, // true to have the groups closed by default
				),
				'after_group' => '</div></div>',
				'before_group'     => '<div class="exwf-collapse-con">',
			) );
			$exwf_log_meta->add_group_field( $feebykm_option, array(
				'name' => esc_html__( 'Max number of km', 'tv-schedule' ),
				'id'   => 'km',
				'type' => 'text',
					
				'classes'		 => 'column-4',
			) );
			$exwf_log_meta->add_group_field( $feebykm_option, array(
				'name' => esc_html__( 'Fee', 'tv-schedule' ),
				'id'   => 'fee',
				'type' => 'text',
					
				'classes'		 => 'column-4',
			) );
			$exwf_log_meta->add_group_field( $feebykm_option, array(
				'name' => esc_html__( 'Free if total amount reach', 'tv-schedule' ),
				'id'   => 'free',
				'type' => 'text',
					
				'classes'		 => 'column-4',
			) );
			$exwf_log_meta->add_group_field( $feebykm_option, array(
				'name' => esc_html__( 'Minimum amount required', 'tv-schedule' ),
				'id'   => 'min_amount',
				'type' => 'text',
					
				'classes'		 => 'column-4',
				'after_row'     => '',
			) );
		}
		if($ship_mode=='postcode'){
			$exwf_log_meta->add_field( array(
				'name' => esc_html__( 'Postcodes', 'woocommerce-food' ),
				'id'   => $prefix .'ship_postcodes',
				'desc' => esc_html__( 'Enter list of your Postcodes here, separated by a comma, leave blank to use default postcodes from settings page', 'woocommerce-food' ),
				'type' => 'textarea',
			) );
		}

	}
	function _edit_columns_exfood_menu($columns){
		$columns['_order'] = esc_html__( 'Order Menu' , 'woocommerce-food' );	
		return $columns;
	}
	function _custom_columns_content_exfood_menu( $content,$column_name,$term_id) {
		switch ( $column_name ) {
			case '_order':
				$term_order = get_term_meta($term_id, 'exwoofood_menu_order', true);
				echo '<input type="number" class="exfd-sort-menu" data-id="' . esc_attr($term_id) . '" name="exfd_sort_menu" value="'.esc_attr($term_order).'">';
				break;	
		}
	}	
}
$EX_Food_Taxonomy = new EX_Food_Taxonomy();


function exwf_locjs_limit_group_repeat( $post_id, $cmb ) {
	$limit = absint( $cmb->prop( 'rows_limit' ) );
	$limit = $limit ? $limit : 0;
	$group =  $cmb->prop( 'groupId' )
	?>
	<div class="exwf-collapse">
	<script type="text/javascript">
	jQuery(document).ready(function($){
		var limit            = <?php echo $limit; ?>;
		var fieldGroupId     = '<?php echo $group; ?>'; 
		var $fieldGroupTable = $( document.getElementById( fieldGroupId + '_repeat' ) );
		var countRows = function() {
			return $fieldGroupTable.find( '> .cmb-row.cmb-repeatable-grouping' ).length;
		};
		var disableAdder = function() {
			$fieldGroupTable.find('.cmb-add-group-row.button-secondary').prop( {disabled: true} );
		};
		var enableAdder = function() {
			$fieldGroupTable.find('.cmb-add-group-row.button-secondary').prop( {disabled: false} );
		};
		$fieldGroupTable
			.on( 'cmb2_add_row', function() {
				if ( countRows() >= limit ) {
					disableAdder();
				}
			})
			.on( 'cmb2_remove_row', function() {
				if ( countRows() < limit ) {
					enableAdder();
				}
			});
	});
	</script>
	<?php
}