<?php
   use \CodeClouds\Unify\Service\Helper;
   
   ?>
<!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
<form id="unify_product_shipping" name="unify_product_shipping" method="post" action="<?php echo esc_html(admin_url('admin-post.php')); ?>">
   <!-- For plugins, we also need to ensure that the form posts back to our current page -->
   <input type="hidden" name="check_submit" id="check_submit" value="update_product" />
   <!-- Now we can render the completed list table -->
   <?php
      $unify_zones = array();
      $unify_zone                                              = new \WC_Shipping_Zone(0);
      $unify_zones[$unify_zone->get_id()]                            = $unify_zone->get_data();
      $unify_zones[$unify_zone->get_id()]['formatted_zone_location'] = $unify_zone->get_formatted_location();
      $unify_zones[$unify_zone->get_id()]['shipping_methods']        = $unify_zone->get_shipping_methods();
      $unify_shipping_zones = array_merge( $unify_zones, WC_Shipping_Zones::get_zones() );
      $unify_zoneShippingArray = array();
      $unify_shippingMethodsArray = array();
      $unify_zone_shipping_methods_count = 0;
      $unify_count_shipping_methods = array();
      foreach ( $unify_shipping_zones as $unify_shipping_zone ) {
          $unify_zone_id = $unify_shipping_zone['id'];
      
          $unify_zone_name = $unify_zone_id == '0' ? __('Rest of the word', 'unify') : $unify_shipping_zone['zone_name'];
          $unify_zone_locations = $unify_shipping_zone['zone_locations']; 
          $unify_zone_location_name = $unify_shipping_zone['formatted_zone_location'];
          $unify_zone_shipping_methods = $unify_shipping_zone['shipping_methods'];
          $unify_zone_shipping_methods_count = count($unify_zone_shipping_methods);
          foreach ( $unify_zone_shipping_methods as $unify_shipping_method_obj ) {
            $unify_count_shipping_methods[] = $unify_shipping_method_obj->get_instance_id();
          	$unify_shippingMethodsArray[$unify_zone_id][] = array("ID"=>$unify_shipping_method_obj->id,'name'=>$unify_shipping_method_obj->get_method_title(),'instance_id'=>$unify_shipping_method_obj->get_instance_id(),'shipping_cost'=>(empty($unify_shipping_method_obj->cost))?"0.0":$unify_shipping_method_obj->cost);
          }
          if($unify_zone_id == '0'){continue;} $unify_zoneArray[$unify_zone_id][] = array('name'=>$unify_zone_name);
          
      }
      
      ?>
   <div class="container-fluid tran-bg-in p-0 mb-0">
      <div class="row clearfix">
         <div class="col-6">
            <p class="prd-dp-text">
               <?php if($crm_meta=='sublytics'){ ?>
               <em><b>Map your CRM Shipping Price with WooCommerce’s Shipping Class ID</b></em>
               <?php }else{ ?>
                  <em><b>Map your CRM Shipping ID with WooCommerce’s Shipping Class ID</b></em>
               <?php } ?>
            </p>
         </div>
         <div class="col-6">
            <span class="uni-show-num">Showing <?php echo count($unify_count_shipping_methods); ?> items</span>
         </div>
      </div>
   </div>
   <div class="container-fluid unify-table uni-shadow-box p-0 ">
      <div class="row">
         <div class="col-12">
            <div class="table-responsive product-table">
               <table class="table table-hover">
                  <thead>
                     <tr>
                        <th class="mid-in-tb sortab" data-order-by="post_title" data-order="<?php echo (!empty($request['orderby']) && $request['orderby'] == 'post_title') ? esc_html($request['order']) : 'asc'; ?>"  >
                           <a href="javascript:void(0);"  id="sort-by-post_title" >
                           <span>Shipping Name</span>
                           </a>
                        </th>
                        <th>WooCommerce Shipping Class ID</th>
                        <th>CRM Shipping ID</th>                      
                     </tr>
                  </thead>
                  <tbody>
                     <?php
                        if (!empty($unify_shippingMethodsArray))
                        {
                        	foreach ($unify_shippingMethodsArray as $unify_key => $unify_value)
                        	{
                        		for($unify_i=0;$unify_i<count($unify_value);$unify_i++){
                        		?>
                     <tr>
                        <td class=""><?php echo esc_html($unify_zoneArray[$unify_key][0]['name']."_".$unify_value[$unify_i]['name']) ?></td>
                        <td>
                           <p class="product-field">
                              <?php echo esc_html($unify_value[$unify_i]['instance_id']); ?>
                              <input type="hidden" name="map[<?php echo esc_html($unify_value[$unify_i]['instance_id']) ?>][woo_shipping_method_price]" value="<?php echo esc_html($unify_value[$unify_i]['shipping_cost']);?>">
                           </p>
                        </td>
                        <td>
                        	<?php $unify_mapped_shipping_id = get_post_meta($unify_value[$unify_i]['instance_id'], "crm_shipping_id");?>
                           <p class="product-field">
                              <input type="text" name="map[<?php echo esc_html($unify_value[$unify_i]['instance_id']) ?>][crm_shipping_id]" class="form-control" aria-required="true" aria-invalid="false" value="<?php echo !empty($unify_mapped_shipping_id[0])?esc_html($unify_mapped_shipping_id[0]):'';?>"/>
                           </p>
                        </td>
                     </tr>
                     <?php
                        }
                        }
                        }
                        else
                        { ?>
                        <tr>
                        	<td>Data not found!</td>
                        </tr>
                        <?php }
                        
                        ?>							
                  </tbody>
               </table>
            </div>
         </div>
      </div>
   </div>
   <div class="container-fluid tran-bg-in p-0">
      <div class="row">
         <div class="col-12 text-right mgtp-20">
            <button type="submit" onclick="document.getElementById('check_submit').value='update_product'" class="btn btn-primary gen-col-btn">
            <span class="">Update Shipping Mapping</span> 
            <span class=""></span>
            </button>
         </div>
      </div>
   </div>
   <input type="hidden" name="action" value="unify_product_shipping" />
   <input type="hidden" id="post_type" name="post_type" value="unify_connections">
   <input type="hidden" id="section" name="section" value="product-mapping-new">
   <?php wp_nonce_field('unify-shipping'); ?>
</form>