<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Provide a admin area view for the plugin
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       http://myworks.design/
 * @since      1.0.0
 *
 * @package    MW_QBO_Desktop
 * @subpackage MW_QBO_Desktop/admin/partials
 */
?>
<?php
	global $MWQDC_LB;
	global $wpdb;
	$page_url = 'admin.php?page=mw-qbo-desktop-map&tab=shipping-method';
	
	$wc_sh_methods = WC()->shipping->load_shipping_methods();
	//$MWQDC_LB->_p($wc_sh_methods);
	
	if ( ! empty( $_POST ) && check_admin_referer( 'myworks_wc_qbo_sync_map_wc_qbo_shipping_method_desk', 'map_wc_qbo_shipping_method_desk' ) ) {
		//$MWQDC_LB->_p($_POST);die;
		$item_ids = array();
		foreach ($_POST as $key=>$value){
			if ($MWQDC_LB->start_with($key, "map_shipping_method_")){
				$id = str_replace("map_shipping_method_", "", $key);
				$id = trim($id);
				if($id!='' && is_array($wc_sh_methods) && isset($wc_sh_methods[$id])){
					$item_ids[$id] = $value;
				}
				
				/**/
				$is_wf_multi_carrier_shipping = false;
				$smd_mc = array();				
				if(strpos( $id, 'wf_multi_carrier_shipping:' ) !== false){
					$is_wf_multi_carrier_shipping = true;
					$smd_mc = $MWQDC_LB->get_smd_s_opts('wf_multi_carrier_shipping');
				}
				
				/**/
				$is_ups_instance = false;
				if(strpos( $id, 'wf_shipping_ups:' ) !== false){
					$is_ups_instance = true;
				}				
				
				//New
				if(strpos( $id, ':' ) !== false && !$is_wf_multi_carrier_shipping && !$is_ups_instance){
					$sm = $MWQDC_LB->wc_get_sm_data_from_method_id_str($id);
					$id_int = (int) $MWQDC_LB->wc_get_sm_data_from_method_id_str($id,'id');
					$sm = trim($sm);
					if($id_int>0 && $sm!=''){
						$smd = $MWQDC_LB->get_wc_smt_data_from_sm_and_id($sm,$id_int);						
						if(is_array($smd) && count($smd)){
							$item_ids[$id] = $value;
						}
					}
				}
				
				/**/
				if($is_ups_instance){
					$uii = str_replace("wf_shipping_ups:", "", $id);
					if((int) $uii > 0){
						$ups_il_arr = $MWQDC_LB->get_ups_sm_instance_list();
						if(is_array($ups_il_arr) && isset($ups_il_arr[$uii])){
							$item_ids[$id] = $value;
						}
					}					
				}
				
				/**/
				if($is_wf_multi_carrier_shipping){
					if(is_array($smd_mc) && !empty($smd_mc)){
						foreach($smd_mc as $mci){
							if(is_array($mci) && isset($mci['sm_id']) && (int) $mci['sm_id'] > 0){
								if(str_replace('wf_multi_carrier_shipping:','',$id) == $mci['sm_id']){
									$item_ids[$id] = $value;
								}
							}
						}
					}					
				}
			}
		}
		
		//$MWQDC_LB->_p($item_ids);die;
		
		if(count($item_ids)){
			foreach ($item_ids as $key=>$value){
				$save_data = array();			
				$save_data['qbo_product_id'] = $value;
				$save_data['class_id'] = (isset($_POST['class_map_shipping_method_'.$key]))?$_POST['class_map_shipping_method_'.$key]:'';
				
				$save_data['qb_shipmethod_id'] = (isset($_POST['qbdsm_map_shipping_method_'.$key]))?$_POST['qbdsm_map_shipping_method_'.$key]:'';
				
				$table = $wpdb->prefix.'mw_wc_qbo_desk_qbd_map_shipping_product';
				if($MWQDC_LB->get_field_by_val($table,'id','wc_shippingmethod',$key)){
					$wpdb->update($table,$save_data,array('wc_shippingmethod'=>$key),'',array('%s'));
				}else{
					$save_data['wc_shippingmethod'] = $key;
					$wpdb->insert($table, $save_data);
				}
			}
			$MWQDC_LB->set_session_val('map_page_update_message',__('Shipping methods mapped successfully.','mw_wc_qbo_desk'));
		}
		$MWQDC_LB->redirect($page_url);
	}
	
	$qbo_product_options = '';
	if(!$MWQDC_LB->option_checked('mw_wc_qbo_desk_select2_ajax')){
		$qbo_product_options = $MWQDC_LB->option_html('', $wpdb->prefix.'mw_wc_qbo_desk_qbd_items','qbd_id','name','','name ASC','',true);
	}
	
	$qbo_class_options_value = $MWQDC_LB->get_class_dropdown_list();
	$qbo_class_options = '<option value=""></option>';	
	$qbo_class_options.= ''.$qbo_class_options_value;
	
	$qbd_shipmethods_values = $MWQDC_LB->option_html('', $wpdb->prefix.'mw_wc_qbo_desk_qbd_list_shipmethod','qbd_id','name','','name ASC','',true);
	$qbd_shipmethods_options = '<option value=""></option>';
	$qbd_shipmethods_options.= ''.$qbd_shipmethods_values;

	$selected_options_script = '';
	$sm_map_data = $MWQDC_LB->get_tbl($wpdb->prefix.'mw_wc_qbo_desk_qbd_map_shipping_product');
	$sm_map_data_kv_arr = array();
	if(is_array($sm_map_data) && count($sm_map_data)){
		foreach($sm_map_data as $sm_k=>$sm_val){
			$wc_shippingmethod = $sm_val['wc_shippingmethod'];
			$wc_shippingmethod = str_replace(':','_',$wc_shippingmethod);
			
			if(!$MWQDC_LB->option_checked('mw_wc_qbo_desk_select2_ajax')){
				$selected_options_script.='jQuery(\'#map_shipping_method_'.$wc_shippingmethod.'\').val(\''.$sm_val['qbo_product_id'].'\');';
			}else{
				$sm_map_data_kv_arr[$sm_val['wc_shippingmethod']] = $sm_val['qbo_product_id'];
			}			
			$selected_options_script.='jQuery(\'#class_map_shipping_method_'.$wc_shippingmethod.'\').val(\''.$sm_val['class_id'].'\');';
			
			$selected_options_script.='jQuery(\'#qbdsm_map_shipping_method_'.$wc_shippingmethod.'\').val(\''.$sm_val['qb_shipmethod_id'].'\');';
		}	
	}
?>
<?php require_once plugin_dir_path( __FILE__ ) . 'mw-qbo-desktop-admin-menu-map-nav.php';?>
<div class="container map-shipping-method-outer">
	<div class="page_title"><h4><?php _e( 'Shipping Method Mappings', 'mw_wc_qbo_desk' );?></h4></div>
	<div class="card">
		<div class="card-content">
			<div class="row">
			<?php if(is_array($wc_sh_methods) && count($wc_sh_methods)):?>
				<form method="POST" class="col s12 m12 l12" action="<?php echo $page_url;?>">
					<div class="row">
						<div class="col s12 m12 l12">
							<div class="myworks-wc-qbd-sync-table-responsive">
								<table class="mw-qbo-sync-map-table menu-blue-bg" width="100%">	
	                            	<thead>				
	                                    <tr>
	                                        <th width="40%">
	                                            Woocommerce Shipping Method								    	
	                                        </th>
	                                        <th width="<?php echo !empty($qbo_class_options_value)?'20%':'30%' ?>">
	                                            Quickbooks Product								    	
	                                        </th>
											<?php if(!empty($qbo_class_options_value)):?>
	                                        <th width="20%">
	                                            Quickbooks Class
	                                        </th>
											<?php endif;?>
											
											<th width="<?php echo !empty($qbo_class_options_value)?'20%':'30%' ?>">
	                                            Quickbooks ShipVia
	                                        </th>
	                                    </tr>
	                                 </thead>   
									<?php foreach($wc_sh_methods as $sm_key => $sm_val):?>
									<?php
										$smt_data = $MWQDC_LB->get_smd_s_opts($sm_key);
										//$MWQDC_LB->_p($sm_val->method_title);
										//$MWQDC_LB->_p($smt_data);
									?>
									<tr>
										<td>
										<b><?php echo $sm_val->method_title;?></b> (<?php echo $sm_key;?>)
										<p><?php echo stripslashes(strip_tags($sm_val->method_description));?></p>
										</td>
										<td>
											<?php
												$dd_options = '<option value=""></option>';
												$dd_ext_class = '';
												if($MWQDC_LB->option_checked('mw_wc_qbo_desk_select2_ajax')){
													$dd_ext_class = 'mwqs_dynamic_select_desk';
													if(count($sm_map_data_kv_arr) && isset($sm_map_data_kv_arr[$sm_key]) && $sm_map_data_kv_arr[$sm_key]!=''){
														$itemid = $sm_map_data_kv_arr[$sm_key];
														$qb_item_name = $MWQDC_LB->get_field_by_val($wpdb->prefix.'mw_wc_qbo_desk_qbd_items','name','qbd_id',$itemid);
														if($qb_item_name!=''){
															$dd_options = '<option value="'.$itemid.'">'.$qb_item_name.'</option>';
														}
													}
												}else{
													$dd_options.=$qbo_product_options;
												}
											?>
											<select class="mw_wc_qbo_sync_select2_desk <?php echo $dd_ext_class;?>" name="map_shipping_method_<?php echo $sm_key?>" id="map_shipping_method_<?php echo $sm_key?>">
												<?php echo $dd_options;?>
											</select>
										</td>
										<?php if(!empty($qbo_class_options_value)):?>
										<td>
											<select class="mw_wc_qbo_sync_select2_desk" name="class_map_shipping_method_<?php echo $sm_key?>" id="class_map_shipping_method_<?php echo $sm_key?>">
												<?php echo $qbo_class_options;?>
											</select>										
										</td>
										<?php endif;?>
										
										<td>
											<select class="mw_wc_qbo_sync_select2_desk" name="qbdsm_map_shipping_method_<?php echo $sm_key?>" id="qbdsm_map_shipping_method_<?php echo $sm_key?>">
												<?php echo $qbd_shipmethods_options;?>
											</select>										
										</td>
									</tr>
									
									<?php if(is_array($smt_data) && count($smt_data)):?>
									<?php foreach($smt_data as $smt_v):?>
									<tr>
										<td>
											<div style="padding-left:2em;">
												<b><?php echo $smt_v['title'];?></b> (<?php echo $sm_key;?>)
											</div>
										</td>
										<td>
											<?php
												$dd_options = '<option value=""></option>';
												$dd_ext_class = '';
												if($MWQDC_LB->option_checked('mw_wc_qbo_desk_select2_ajax')){
													$dd_ext_class = 'mwqs_dynamic_select_desk';
													if(count($sm_map_data_kv_arr) && isset($sm_map_data_kv_arr[$sm_key.':'.$smt_v['sm_id']]) && $sm_map_data_kv_arr[$sm_key.':'.$smt_v['sm_id']]!=''){
														$itemid = $sm_map_data_kv_arr[$sm_key.':'.$smt_v['sm_id']];
														$qb_item_name = $MWQDC_LB->get_field_by_val($wpdb->prefix.'mw_wc_qbo_desk_qbd_items','name','qbd_id',$itemid);
														if($qb_item_name!=''){
															$dd_options = '<option value="'.$itemid.'">'.$qb_item_name.'</option>';
														}
													}
												}else{
													$dd_options.=$qbo_product_options;
												}
											?>
											<select class="mw_wc_qbo_sync_select2_desk <?php echo $dd_ext_class;?>" name="map_shipping_method_<?php echo $sm_key?>:<?php echo $smt_v['sm_id'];?>" id="map_shipping_method_<?php echo $sm_key?>_<?php echo $smt_v['sm_id'];?>">
												<?php echo $dd_options;?>
											</select>
										</td>
										<?php if(!empty($qbo_class_options_value)):?>
										<td>
											<select class="mw_wc_qbo_sync_select2_desk" name="class_map_shipping_method_<?php echo $sm_key?>:<?php echo $smt_v['sm_id'];?>" id="class_map_shipping_method_<?php echo $sm_key?>_<?php echo $smt_v['sm_id'];?>">
												<?php echo $qbo_class_options;?>
											</select>										
										</td>
										<?php endif;?>
										
										<td>
											<select class="mw_wc_qbo_sync_select2_desk" name="qbdsm_map_shipping_method_<?php echo $sm_key?>:<?php echo $smt_v['sm_id'];?>" id="qbdsm_map_shipping_method_<?php echo $sm_key?>_<?php echo $smt_v['sm_id'];?>">
												<?php echo $qbd_shipmethods_options;?>
											</select>										
										</td>
									</tr>
									<?php endforeach;?>
									<?php endif;?>
									
									<?php endforeach;?>							
								</table>
							</div>
						</div>
					</div>
					<div class="row">
						<?php wp_nonce_field( 'myworks_wc_qbo_sync_map_wc_qbo_shipping_method_desk', 'map_wc_qbo_shipping_method_desk' ); ?>
						<div class="input-field col s12 m6 l4">
							<button class="waves-effect waves-light btn save-btn mw-qbo-sync-green">Save</button>
						</div>
					</div>
				</form>
			<?php else:?>
				<p><?php _e( 'No shipping method found.', 'mw_wc_qbo_desk' );?></p>
			<?php endif;?>
			</div>
		</div>
	</div>
</div>
<?php if($selected_options_script!=''):?>
<script type="text/javascript">
jQuery(document).ready(function(){
	<?php echo $selected_options_script;?>
});
</script>
<?php endif;?>
<?php echo $MWQDC_LB->get_select2_js('.mw_wc_qbo_sync_select2_desk','qbo_product');?>