<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
 <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="button" id="save_form" form="form-xshippingpro" value="save" name="action" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
        <a id="quick_save" onclick="return false;" data-toggle="tooltip" title="<?php echo $button_save_continue; ?>" id="quick_save" class="btn btn-info"><i class="fa fa-clipboard"></i></a>&nbsp;
        <a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
      <h1><?php echo $heading_title; ?></h1>
      <ul class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb) { ?>
        <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
        <?php } ?>
      </ul>
    </div>
 </div>
<div class="container-fluid">
    <?php if ($error_warning) { ?>
    <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    <?php } ?>
    <?php if ($success) { ?>
    <div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> <?php echo $success; ?>
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    <?php } ?>
    <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
          </div>
          <div class="panel-body">
           <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-xshippingpro" class="form-horizontal">
             <input type="file" class="import-csv" accept="text/csv" name="file" />
             <div class="row">
                    <div class="col-sm-3">
                      <ul id="method-list" class="nav nav-pills nav-stacked">
                        <li class="active"><a href="#tab-general" draggable="false" class="global" data-toggle="tab"><?php echo $tab_general; ?></a></li>
                        <?php
                            foreach($methods as $no_of_tab=>$name){
                               ?>
                          <li><a draggable="true" class="draggable tab<?php echo $no_of_tab;?>" href="#shipping-<?php echo $no_of_tab; ?>"  data-toggle="tab"><?php echo $name;?></a></li>
                        <?php } ?>
                      </ul>
                      
                      <button class="btn btn-success add-new" data-toggle="tooltip" form="form-xshippingpro" type="button"  data-placement="bottom"  data-original-title="<?php echo $text_add_new_method?>"><i class="fa fa-plus"></i></button>
                    </div>
	                
                  <div class="col-sm-9">
                    <div id="shipping-container" class="tab-content">
                     <div class="tab-pane active global-tab-content" id="tab-general">
                        <ul class="nav nav-tabs" id="language-heading">
                            <?php $active_class=''; foreach ($languages as $language) { ?>
                             <li <?php if(!$active_class) echo 'active'; $active_class='1'; ?>><a href="#language<?php echo $language['language_id']; ?>heading" data-toggle="tab"><img src="<?php echo $language_dir.$language['image']; ?>" title="<?php echo $language['name']; ?>" /> <?php echo $language['name']; ?></a></li>
                            <?php } ?>
                         </ul>
                         
                         <div class="tab-content global-heading">
                            <?php $active_class=''; foreach ($languages as $language) { ?>
                              <div class="tab-pane<?php if(!$active_class) echo ' active'; $active_class='1'; ?>" id="language<?php echo $language['language_id']; ?>heading">                         
                                <div class="form-group required">
                                    <label class="col-sm-2 control-label" for="lang-heading<?php echo $language['language_id']; ?>"><span data-toggle="tooltip" title="<?php echo $tip_heading; ?>"><?php echo $text_heading; ?> </span></label>
                                    <div class="col-sm-10">
                                      <input type="text" name="xshippingpro_heading[<?php echo $language['language_id']; ?>]" value="<?php echo isset($xshippingpro_heading[$language['language_id']])?$xshippingpro_heading[$language['language_id']]:'Shipping Options'; ?>" placeholder="<?php echo $text_heading; ?>" id="lang-heading<?php echo $language['language_id']; ?>" class="form-control" />
                                    </div>
                                  </div>
                               </div> 
                            <?php } ?>
                          </div>
                         
                          <ul class="nav nav-tabs">
                             <li class="active"><a href="#global-general" data-toggle="tab"><?php echo $tab_general_general?></a></li>
                             <li><a href="#global-group" data-toggle="tab"><?php echo $tab_general_global?></a></li>
                             <li><a href="#global-export" data-toggle="tab"><?php echo $tab_import_export?></a></li>
                             <li><a href="#global-help" data-toggle="tab"><?php echo $text_help?></a></li>
                          </ul>
                         
                         <div class="tab-content"> 
                           <div class="tab-pane active" id="global-general">
                            <div class="form-group">
                              <label class="col-sm-2 control-label" for="input-sort-order"><span data-toggle="tooltip" title="<?php echo $tip_sorting_global; ?>"><?php echo $entry_sort_order; ?> </span></label>
                              <div class="col-sm-10">
                                <input type="text" name="xshippingpro_sort_order" value="<?php echo $xshippingpro_sort_order; ?>" placeholder="<?php echo $entry_sort_order; ?>" id="input-sort-order" class="form-control" />
                              </div>
                           </div>
                          <div class="form-group">
                            <label for="xshippingpro_desc_mail" class="col-sm-2 control-label"><?php echo $text_description; ?> </label>
                             <div class="col-sm-10">
                                 <input <?php if(isset($xshippingpro_desc_mail) && $xshippingpro_desc_mail) echo 'checked';?> type="checkbox" name="xshippingpro_desc_mail" value="1" id="xshippingpro_desc_mail" />
                              </div>
                            </div>
                           <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-group-sorting"><span data-toggle="tooltip" title="<?php echo $tip_text_sort_type; ?>"><?php echo $text_sort_type; ?></span></label>
                            <div class="col-sm-10">
                              <select name="xshippingpro_sorting" id="input-group-sorting" class="form-control">
                               		 <?php
                   
                  					 foreach($sort_options as $sort_option_key=>$sort_option_value){
                    					$selected=(isset($xshippingpro_sorting) && $xshippingpro_sorting==$sort_option_key)?'selected':'';
                  					?>
                                   <option value="<?php echo $sort_option_key?>" <?php echo $selected?>><?php echo $sort_option_value?></option>
                                  <?php } ?>
                              </select>
                             </div>
                          </div> 
                             <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-status"><span data-toggle="tooltip" title="<?php echo $tip_status_global; ?>"><?php echo $module_status; ?></span></label>
                            <div class="col-sm-10">
                              <select name="xshippingpro_status" id="input-status" class="form-control">
                                <?php if ($xshippingpro_status) { ?>
                                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                                  <option value="0"><?php echo $text_disabled; ?></option>
                                  <?php } else { ?>
                                  <option value="1"><?php echo $text_enabled; ?></option>
                                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                                <?php } ?>
                              </select>
                             </div>
                          </div>
                            <div class="form-group">
                            <label class="col-sm-2 control-label" for="xshippingpro_debug"><span data-toggle="tooltip" title="<?php echo $tip_debug; ?>"><?php echo $text_debug; ?></span></label>
                            <div class="col-sm-10">
                              <select name="xshippingpro_debug" id="xshippingpro_debug" class="form-control">
                               <?php if ($xshippingpro_debug) { ?>
                                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                                  <option value="0"><?php echo $text_disabled; ?></option>
                                  <?php } else { ?>
                                  <option value="1"><?php echo $text_enabled; ?></option>
                                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                                  <?php } ?>
                              </select>
                             </div>
                          </div>
                         </div>
                         <div class="tab-pane" id="global-export">
                            <div class="form-group">
                              <label class="col-sm-2 control-label" for="input-import"><span data-toggle="tooltip" title="<?php echo $tip_import; ?>"><?php echo $text_import; ?></span></label>
                              <div class="col-sm-8">
                                 <input type="file" class="form-control" id="input-import" accept="text/txt" name="file_import" />
                              </div>
                              <div class="col-sm-2">
                                <button type="submit" form="form-xshippingpro" data-toggle="tooltip" name="action" title="<?php echo $text_import; ?>" value="import" class="btn btn-primary"><i class="fa fa-upload"></i></button>
                              </div>
                           </div>
                           <div class="form-group">
                              <label class="col-sm-2 control-label" for="input-export"><span data-toggle="tooltip" title="<?php echo $tip_export; ?>"><?php echo $text_export; ?></span></label>
                              <div class="col-sm-10">
                                 <a href="<?php echo $export;?>" target="_blank" class="btn btn-primary"><?php echo $text_export; ?></a>
                              </div>
                           </div>
                         </div>
                         <div class="tab-pane" id="global-help">
                          <div class="form-group">
                            <div class="col-sm-12">
                               <div class="help-data">
                                <a class="btn btn-info" href="http://opencartmart.com/docs/xshippingpro/index.html" target="_blank" role="button"><?php echo $text_documentation;?></a> <br />
                                <a class="btn btn-warning" href="http://version.opencartmart.com/index.php?v=<?php echo $x_version;?>&id=<?php echo $x_id;?>" target="_blank" role="button"><?php echo $text_update;?></a>
                              </div>
                            </div>
                          </div>
                        </div> 
                         <div class="tab-pane" id="global-group">
                         <div class="form-group">
              				<label class="col-sm-2 control-label" for="input-method-group"><span data-toggle="tooltip" title="<?php echo $tip_method_group; ?>"> <?php echo $text_method_group;?> </span></label>
              			    <div class="col-sm-10">	
             				 <div class="table-responsive">
                 				<table class="table table-striped table-bordered table-hover">
                    				<thead>
                    					<tr>
                     				 		<td class="text-left">
                       			 				<?php echo $text_group_name;?>
                      						</td>
                      		 				<td class="text-left">
                        						<?php echo $text_group_type;?>
                     		 				</td>
                     		 				<td class="text-left">
                         						<span data-toggle="tooltip" title="<?php echo $tip_group_limit; ?>"><?php echo $text_group_limit;?></span>
                     		 				</td>
                     		 				<td class="text-left">
                         						<span data-toggle="tooltip" title="<?php echo $tip_group_name; ?>"><?php echo $entry_group_name;?></span>
                     		 				</td>
                     		 		   </tr>		
                   		 			</thead>
                   				 <tbody>
                   		 			<?php
                      
                     		 			for($sg=1; $sg<=$xshippingpro_sub_groups_count;$sg++) {
                      						$current_method_mode = 'no_group';
                    				?>
                    				 <tr>
                      					<td class="text-left">Group<?php echo $sg;?></td>
                      					<td class="text-left">
                       		 				<select class="xshippingpro_sub_group<?php echo $sg;?>" name="xshippingpro_sub_group[<?php echo $sg;?>]">
                  								<?php
                  				 					foreach($group_options as $type=>$name) {
                    			 	 					$selected=(isset($xshippingpro_sub_group[$sg]) && $xshippingpro_sub_group[$sg]==$type)?'selected':'';
                    			  						$current_method_mode = (isset($xshippingpro_sub_group[$sg]) && $xshippingpro_sub_group[$sg]==$type)? $type: $current_method_mode; 
                 								?>
                 				 					<option value="<?php echo $type?>" <?php echo $selected?>><?php echo $name?></option>
                 				 				<?php } ?>
               				 				</select>
                     	 				</td>
                     	 				<td class="text-left"> 
                         						<select <?php if($current_method_mode!='lowest' && $current_method_mode!='highest') echo 'style="display:none;"'; ?> class="xshippingpro_sub_group_limit<?php echo $sg;?>" name="xshippingpro_sub_group_limit[<?php echo $sg;?>]">
                									<?php
                   										for($gi=1; $gi<=5; $gi++){
                    										$selected=(isset($xshippingpro_sub_group_limit[$sg]) && $xshippingpro_sub_group_limit[$sg]==$gi)?'selected':'';
                  										?>
                  										<option value="<?php echo $gi?>" <?php echo $selected?>><?php echo $gi?></option>
                 				 					<?php } ?>
               				 					</select>
                      					</td>
                      					<td class="text-left"> 
                      					     <input type="text" name="xshippingpro_sub_group_name[<?php echo $sg;?>]" value="<?php echo isset($xshippingpro_sub_group_name[$sg])?$xshippingpro_sub_group_name[$sg]:''; ?>" placeholder="<?php echo $entry_name; ?>" class="form-control" />
                      					</td>
                    				</tr> 
                     			  <?php } ?> 
                    		    </tbody>
                  		       </table>
                  		       </div>
              			     </div>
            		       </div>
            		       <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-group"><span data-toggle="tooltip" title="<?php echo $tip_grouping; ?>"><?php echo $text_group_shipping_mode; ?></span></label>
                            <div class="col-sm-10">
                              <select name="xshippingpro_group" id="input-group" class="form-control xshippingpro_group">
                                <?php
                                   foreach($group_options as $type=>$name){
                                   if($type =='and') continue;
                                    $selected=(isset($xshippingpro_group) && $xshippingpro_group==$type)?'selected':'';
                                  ?>
                                  <option value="<?php echo $type?>" <?php echo $selected?>><?php echo $name?></option>
                                  <?php } ?>
                              </select>
                             </div>
                          </div>
                          <div <?php if($xshippingpro_group!='lowest' && $xshippingpro_group!='highest') echo 'style="display:none;"'; ?> id="group-limit" class="form-group">
                            <label class="col-sm-2 control-label" for="input-group-limit"><span data-toggle="tooltip" title="<?php echo $tip_group_limit; ?>"><?php echo $text_group_limit; ?></span></label>
                            <div class="col-sm-10">
                              <select name="xshippingpro_group_limit" id="input-group-limit" class="form-control">
                                <?php
                                   for($gi=1; $gi<=5; $gi++){
                                    $selected=(isset($xshippingpro_group_limit) && $xshippingpro_group_limit==$gi)?'selected':'';
                                  ?>
                                  <option value="<?php echo $gi?>" <?php echo $selected?>><?php echo $gi?></option>
                                  <?php } ?>
                              </select>
                             </div>
                          </div>
                         </div>   
                        </div>   
                       </div> <!--end of tab general-->
                       <?php  echo $form_data;?>
                   </div>
                 </div>
               </div>      
            </form>
      </div>
    </div>
  </div>

<style type="text/css">
.xshipping-checkbox label{ margin-right: 15px;}
.xshipping-checkbox label input {
        margin-right: 5px;
}
.well {
    margin-bottom: 5px;
}
.well-sm{ display:none;}
.well-days, .well-desc, .product-category, .product-product, .product-option, .product-manufacturer, .hide-methods, .product-location, .hide-methods-inactive{ display:block;}
div.category, div.product, div.option, div.postal-option, div.range-option, div.coupon-option, div.manufacturer, div.dimensional-option, div.city-option, div.location{ display:none;}
div.checkbox-selection-wrap{
    display: none;
}
div.checkbox-selection-wrap a {
    float: right;
    margin-left: 10px;
  }
label.any-class {
    margin-top: 4px;
 }
div.range-option textarea{height: 45px;}
.tbl-wrapper{ width:99%;}
.import-btn-wrapper{width:99%; height:auto; overflow:hidden; margin-bottom:10px;}
.import-btn-wrapper a.btn{ float:right;margin-right: 5px;}
input.import-csv[type="file"]{ display:none;}
div.shipping {
    position: relative;
}
.action-btn {
    position: absolute;
    right: 10px;
    top: 0;
}
.action-btn button {
    margin-left: 5px;
}
button.add-new {
    margin-top: 10px;
}

div.tooltip div.tooltip-inner{ font-weight:normal !important; text-align:left !important;}
div.tooltip div.tooltip-inner b{ display:block !important;}
.global-waiting{display:block;position:fixed; width:124px; height:34px; text-align:center;font-size:16px;font-weight:bold; color:#ffffff;background-color:#D96E7C; border-radius:5px;padding-top:5px;}
.fa-minus-circle{ cursor:pointer;}
/* End of new*/

/*tab related*/
.global-tab-content{margin-top:15px;}
#shipping-container {
  border: 1px solid #ddd;
  padding: 0 15px 15px;
}
.global-heading {
    margin-bottom: 30px;
    border-bottom: 1px solid #efefef;
    padding-bottom: 15px;
}
#method-list > li > a.global{
  text-transform: uppercase;
  background-color: #c7c7c7;
  color: #555;
  font-weight: bold;
}
#method-list > li > a {
    background-color: #f7f7f7;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    margin-bottom: 2px;
    border-right:1px solid #ccc;
}
#method-list > li.active > a, #method-list > li > a:hover {
  background-color: #1e91cf;
  color: #fff;
}
#method-list > li.active a.global {background-color: #FF5722 !important;}
.display-name-row {
    background: #f7f7f7;
    margin-bottom: 11px;
    border-bottom: 1px solid #d7d7d7;
}
.method-tab {
  background: #f7f7f7;
   margin-bottom: 0;
}
.method-content {
    border: 1px solid #ddd;
    padding: 15px;
    border-top: none;
}

.help-data{
      text-align: center;
    }
.help-data a{
   margin-bottom: 10px;
 }
 #method-list li > a.dropable {
    border: 2px dotted #f38733;
 }
 #method-list li > a.dragging {
    opacity: 0.5;
 }
</style>
<script type="text/javascript"><!--
var current_tab=1; 
var save_type = '';   
var range ='<tr>'; 
    range += '    <td class="text-left"><input size="15" type="text" name="xshippingpro[rate_start][]" class="form-control" value="__VALUE_START__" /></td>';
    range += '    <td class="text-left"><input size="15" type="text" name="xshippingpro[rate_end][]" class="form-control" value="__VALUE_END__" /></td>';
    range += '    <td class="text-left"><input size="15" type="text" name="xshippingpro[rate_total][]" class="form-control" value="__VALUE_COST__" /></td>';
    range += '    <td class="text-left"><input size="6" type="text" name="xshippingpro[rate_block][]" class="form-control" value="__VALUE_PG__" /></td>';
	range += '    <td class="text-left"><select name="xshippingpro[rate_partial][]"><option __VALUE_PA2__ value="0"><?php echo $text_no;?></option><option __VALUE_PA1__ value="1"><?php echo $text_yes;?></option></select></td>';
    range += '    <td class="text-right"><a class="btn btn-danger remove-row"><?php echo $text_remove;?></a></td>';
    range += '  </tr>';
       
 var tmp = <?php echo json_encode($tpl); ?>;
 var shipping_methods = [];
 var auto_complete_cat={
				'source': function(request, response) {
					$.ajax({
						url: 'index.php?route=catalog/category/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
						dataType: 'json',			
						success: function(json) {
							response($.map(json, function(item) {
								return {
									label: item['name'],
									value: item['category_id']
								}
							}));
						}
					});
				},
				'select': function(item) {
					
					var my_method_area=$('#shipping-'+current_tab);
					$('input[name=\'category\']',my_method_area).val('');
					$('.product-category' + item['value'],my_method_area).remove();
					$('.product-category',my_method_area).append('<div class="product-category' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="xshippingpro[product_category][]" value="' + item['value'] + '" /></div>');	
					
				}
	};
	
 var auto_complete_prod={
	 'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
			dataType: 'json',			
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['product_id']
					}
				}));
			}
		});
	},
	 'select': function(item) {
		 
		var my_method_area=$('#shipping-'+current_tab); 
		$('input[name=\'product\']', my_method_area).val('');
		$('.product-product' + item['value'], my_method_area).remove();
		$('.product-product', my_method_area).append('<div class="product-product' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="xshippingpro[product_product][]" value="' + item['value'] + '" /></div>');	
	  }	
  };
  
  var auto_complete_option={
	 'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=extension/shipping/xshippingpro/getOption&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
			dataType: 'json',			
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['option_value_id']
					}
				}));
			}
		});
	},
	 'select': function(item) {
		 
		var my_method_area=$('#shipping-'+current_tab); 
		$('input[name=\'option\']', my_method_area).val('');
		$('.product-option' + item['value'], my_method_area).remove();
		$('.product-option', my_method_area).append('<div class="product-option' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="xshippingpro[product_option][]" value="' + item['value'] + '" /></div>');	
	  }	
  };

  var auto_complete_manufacturer={
   'source': function(request, response) {
    $.ajax({
      url: 'index.php?route=catalog/manufacturer/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
      dataType: 'json',     
      success: function(json) {
        response($.map(json, function(item) {
          return {
            label: item['name'],
            value: item['manufacturer_id']
          }
        }));
      }
    });
  },
   'select': function(item) {
     
    var my_method_area=$('#shipping-'+current_tab); 
    $('input[name=\'input_manufacturer\']', my_method_area).val('');
    $('.product-manufacturer' + item['value'], my_method_area).remove();
    $('.product-manufacturer', my_method_area).append('<div class="product-manufacturer' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="xshippingpro[manufacturer][]" value="' + item['value'] + '" /></div>'); 
    } 
  };

  
 var auto_complete_methods = {
   'source': function(request, response) {
    var methods = [];
    $('#shipping-container').find('div.shipping').each(function(){
             var name,
                 tab_id,
                 $this = $(this);
             tab_id = $this.attr('id');
             tab_id = parseInt(tab_id.replace('shipping-',''));
             name = $this.find('input.display-name').val();
             if (!name) name = 'Untitled Method'+tab_id;
             if (current_tab != tab_id && name.toLowerCase().indexOf(request) != -1) {
                  methods.push({
                  label: name,
                  value: tab_id
                });
              }
      });
    response(methods);
  },
   'select': function(item) {

      var hide_class = 'hide-method',
          hide_wrapper = 'hide-methods',
          field_name = 'hide';
      if ($(this).hasClass('hide-inactive')) {
         hide_class = 'hide-method-inactive';
         hide_wrapper = 'hide-methods-inactive';
         field_name = 'hide_inactive';
      }

     
      var my_method_area=$('#shipping-'+current_tab); 
      $('input.hide-shipping', my_method_area).val('');
      $('.'+hide_class + item['value'], my_method_area).remove();
      $('.'+hide_wrapper, my_method_area).append('<div class="'+hide_class + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="xshippingpro['+field_name+'][]" value="' + item['value'] + '" /></div>'); 
    }
 };

 var auto_complete_location={
   'source': function(request, response) {
    $.ajax({
      url: 'index.php?route=extension/shipping/xshippingpro/getLocation&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
      dataType: 'json',     
      success: function(json) {
        response($.map(json, function(item) {
          return {
            label: item['name'],
            value: item['id']
          }
        }));
      }
    });
  },
   'select': function(item) {
     
      var my_method_area=$('#shipping-'+current_tab); 
      $('input[name=\'input_location\']', my_method_area).val('');
      $('.product-location' + item['value'], my_method_area).remove();
      $('.product-location', my_method_area).append('<div class="product-location' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="xshippingpro[location][]" value="' + item['label'] + '" /></div>');  
    } 
  };

   
$(document).ready(function () {		
 
	 /* Enable tab*/
	 $('#method-list a:first').tab('show');
	 $('#language-heading a:first').tab('show');
	 
	 $("#method-list").on("click","li",function(){
		 current_tab=$(this).find('a').attr('href').replace('#shipping-','');  
	 });

   makedropzone();

   $('.date').datetimepicker({
      pickTime: false
   });
	 
	 /* Creating New method*/
	 $('.add-new').on('click',function(e) {
		  e.preventDefault();
		  $this=$(this);
		  var no_of_tab=$('#shipping-container').find('div.shipping').length;
		  no_of_tab=parseInt(no_of_tab)+1;
		  //finding qnique id
		  while($('#shipping-'+no_of_tab).length!=0)
		   {
		     no_of_tab++;
		   }
		  var tab_html=tmp;
		  tab_html=tab_html.replace('__ID__','shipping-'+no_of_tab);
		  tab_html=tab_html.replace(/__INDEX__/g, no_of_tab);
		  $('#shipping-container').append(tab_html);
		  
		  $('#method-list').append('<li><a draggable="true" data-toggle="tab" class="draggable tab'+no_of_tab+'" href="#shipping-'+no_of_tab+'">Untitled Method '+no_of_tab+'</a></li>');
		   enableEvents(no_of_tab); 
		   current_tab=no_of_tab;
    });
	 /* End of creating new*/
	 
	 $("#shipping-container").on('click','button.btn-delete', function() { 
		  if(confirm('Are you sure to delete this method?')){
		              deleteMethod(current_tab);
					  $('a.tab'+current_tab).remove();
					  $('#shipping-'+current_tab).remove();
					  $('#method-list a:first').tab('show');
				}
	 });
	 
	 $("#shipping-container").on('click','button.btn-copy', function() { 
		  if(confirm('Are you sure to copy this method?')){
					  copyMethod(current_tab);
				}
	 });
	 
	 
	   $('.xshippingpro_group').change(function(){
		   if($(this).val()=='lowest' || $(this).val()=='highest') {
			  $('#group-limit').show();  
		   }else{
			  $('#group-limit').hide();     
		   }  
		});
		
		$("select[class^='xshippingpro_sub_group']").change(function(){
		   var groupid = $(this).attr('class').replace('xshippingpro_sub_group','');
		   if($(this).val()=='lowest' || $(this).val()=='highest') {
			  $('.xshippingpro_sub_group_limit'+parseInt(groupid)).show();  
		   }else{
			   $('.xshippingpro_sub_group_limit'+parseInt(groupid)).hide();     
		   }  
		
		});
	   
       
	   $("#shipping-container").on('keyup','input.display-name', function() {		
		  var method_name=$(this).val();
		  if(method_name=='') method_name='Untitled Method '+current_tab;
		  $('a.tab'+current_tab).html(method_name);
	   }); 
	   
	   
	    $("#shipping-container").on('change','select.selection', function() {
		    
			var relto=$(this).attr('rel');
			 if($(this).val()=='1'){
			    $(this).closest('div.form-group').siblings('div.'+relto).hide();  
			 }else{
			    $(this).closest('div.form-group').siblings('div.'+relto).show();  
			 }
		 }); 
		 
		 $('#shipping-container').delegate('.fa-minus-circle', 'click', function() {
			  $(this).parent().remove();
	     });
		
		  // Category selection 
		  $('input[name=\'category\']').autocomplete(auto_complete_cat);
		  $('input[name=\'product\']').autocomplete(auto_complete_prod);
		  $('input[name=\'option\']').autocomplete(auto_complete_option);
      $('input[name=\'input_manufacturer\']').autocomplete(auto_complete_manufacturer);
      $('input[name=\'input_location\']').autocomplete(auto_complete_location);
		  $('input.hide-shipping').autocomplete(auto_complete_methods);

      $("#shipping-container").on('click', '.check-all', function(e) {
           e.preventDefault();
           $(this).closest('div.form-group').find('div.checkbox input[type="checkbox"]').prop( "checked",true);;
      });

      $("#shipping-container").on('click', '.uncheck-all', function(e) {
           e.preventDefault();
           $(this).closest('div.form-group').find('div.checkbox input[type="checkbox"]').prop( "checked",false);;
      });
			
		  $("#shipping-container").on('click', '.choose-any', function() {
				 if($(this).prop('checked')){
					  $(this).parent().next('div.well-sm').slideUp(); 
            $(this).parent().siblings('div.checkbox-selection-wrap').hide(); 
				 }else{
					  $(this).parent().next('div.well-sm').slideDown();
            $(this).parent().siblings('div.checkbox-selection-wrap').show();
				}
			});
			
		  $("#shipping-container").on('click', '.choose-any-with', function() {
                var relto=$(this).attr('rel');
				  
				  if(relto=='manufacturer-option') {
					 if($(this).prop('checked')){
						 $(this).parent().next('div.well-sm').slideUp();  
					 }else{
						 $(this).parent().next('div.well-sm').slideDown();
					 }
				  }
				 	
				 if($(this).prop('checked')){
					 $(this).closest('div.form-group').siblings('div.'+relto).hide();  
				 }else{
					$(this).closest('div.form-group').siblings('div.'+relto).show();  
				}
			});
                
		
    $("#shipping-container").on('change', 'select.rate-selection', function() {
		      
      			if($(this).val()=='flat'){
      			   $(this).closest('div.form-group').siblings('div.single-option').show();
               $(this).closest('div.form-group').siblings('div.range-option').hide();
      			   $(this).closest('div.form-group').siblings('div.dimensional-option').hide();
      			   
      			}
      		   else if($(this).val()=='dimensional' || $(this).val()=='volumetric'){
      			   $(this).closest('div.form-group').siblings('div.single-option').hide();
      			   $(this).closest('div.form-group').siblings('div.dimensional-option').show();
      			   $(this).closest('div.form-group').siblings('div.range-option').show();
      			  
      			 }
      		   else {
      			   $(this).closest('div.form-group').siblings('div.single-option').hide();
      			   $(this).closest('div.form-group').siblings('div.dimensional-option').hide();
               $(this).closest('div.form-group').siblings('div.range-option').show();
      			 }

            /* additional ranges */
            if ($(this).val() =='quantity') {
              $(this).closest('div.method-content').find('div.additional-weight').show();
              $(this).closest('div.method-content').find('div.additional-total').show();
              $(this).closest('div.method-content').find('div.additional-quantity').hide();
            }
            else if ($(this).val() =='weight') {
              $(this).closest('div.method-content').find('div.additional-total').show();
              $(this).closest('div.method-content').find('div.additional-quantity').show();
              $(this).closest('div.method-content').find('div.additional-weight').hide();
            }
            else if ($(this).val() =='total'
              || $(this).val() =='sub'
              || $(this).val() =='total_coupon'
              || $(this).val() =='grand_shipping'
              || $(this).val() =='grand') {
              $(this).closest('div.method-content').find('div.additional-weight').show();
              $(this).closest('div.method-content').find('div.additional-quantity').show();
              $(this).closest('div.method-content').find('div.additional-total').hide();
            } else {
              $(this).closest('div.method-content').find('div.additional-weight').show();
              $(this).closest('div.method-content').find('div.additional-quantity').show();
              $(this).closest('div.method-content').find('div.additional-total').show();
            }
		});
                
                
     /* Range Options */
	 $("#shipping-container").on('click','a.add-row',function(){
	          
	          var tpl=range;
			  tpl=tpl.replace(/__INDEX__/g, current_tab);
			  tpl=tpl.replace(/__VALUE_START__/g, '0');
			  tpl=tpl.replace(/__VALUE_END__/g, '0');
			  tpl=tpl.replace(/__VALUE_COST__/g, '0');
			  tpl=tpl.replace(/__VALUE_PG__/g, '0');
			  tpl=tpl.replace(/__VALUE_PA1__/g, '');
			  tpl=tpl.replace(/__VALUE_PA2__/g, '');
             
			  $(this).closest('table').find('tbody tr.no-row').remove();     
		      $(this).closest('table').find('tbody').append(tpl);		  
        });
		
		 $("#shipping-container").on('click','a.remove-row',function(){									  
	        $(this).closest('tr').remove();	
			 if($('#shipping-'+current_tab).find('.tbl-wrapper tbody tr').length==0){
			  $('#shipping-'+current_tab).find('.tbl-wrapper tbody').append('<tr class="no-row"><td colspan="6"><?php echo $no_unit_row;?></td></tr>');     	
		    }									  
        });
        
        $("#shipping-container").on('click','a.delete-all',function(){									  
	       $(this).closest('div.tbl-wrapper').find('tbody > tr').remove();	
		   if($('#shipping-'+current_tab).find('.tbl-wrapper tbody tr').length==0) {
			  $('#shipping-'+current_tab).find('.tbl-wrapper tbody').append('<tr class="no-row"><td colspan="6"><?php echo $no_unit_row;?></td></tr>');     	
		    }										  
        });
        
		/* CSV upload*/
		$("#shipping-container").on('click', 'a.import-btn', function() {
		    $('input.import-csv:file').trigger('click');      
		});
		
		$("input.import-csv:file").change(function (){
		    var file = $('input.import-csv:file').get(0).files[0];
			
           var fd = new FormData();
           fd.append('file', file);
		    $.ajax({
					url: 'index.php?route=extension/shipping/xshippingpro/csv_upload&token=<?php echo $token; ?>',
					dataType: 'json',
                  	data:fd,
				    processData: false,
                  	contentType: false,
                  	type:'POST',
				    beforeSend: function(){
				      showLoader();
					},
					success: function(json) {		
					  
					   if (json['data']) {
							   for(i=0;i<json['data'].length;i++) {
								  var data=json['data'][i];
								  var tpl=range;
								  var pa1=1, pa2=0;
								  if(data.pa=='1') pa1='selected';
								  if(data.pa=='0') pa2='selected';
								  tpl=tpl.replace(/__INDEX__/g, current_tab);
								  tpl=tpl.replace(/__VALUE_START__/g, data.start);
								  tpl=tpl.replace(/__VALUE_END__/g, data.end);
								  tpl=tpl.replace(/__VALUE_COST__/g, data.cost);
								  tpl=tpl.replace(/__VALUE_PG__/g, data.pg);
								  tpl=tpl.replace(/__VALUE_PA1__/g, pa1);
								  tpl=tpl.replace(/__VALUE_PA2__/g, pa2);
								  $('#shipping-'+current_tab).find('.tbl-wrapper tbody').append(tpl);	
							  }
                       }
					    if (json['error']) {
                            alert(json['error']);
                        }
                      $('.global-waiting').remove();	  
					}
			 }); 
		});
          
        /* End of CSV upload*/
         
        /*Quick save*/
        $('#quick_save, #save_form').click(function(){
    			
    			save_type = $(this).attr('id');
    			
    			$('#method-list').find('li > a.draggable').each(function(sort_order){
    			   var data,
    			   	   tab_id,
    			   	   $this = $(this);
    			   tab_id = $this.attr('href');
    			   tab_id = tab_id.replace('#shipping-','');
    			   data = $('#shipping-container #shipping-'+tab_id).find(':input').serialize();
    			   shipping_methods.push({
    			     tab_id:tab_id,
               sort_order: sort_order,
    			     data: data
    			   });
    			});
    			
    			var general_data = $('#tab-general :input').serialize();
    			saveGeneral(general_data);
    			
    			if(shipping_methods.length > 0) {
			    	showLoader();
			    	saveMethod();
			    }
		});           
       
	    
 });
 
 
 function saveGeneral(data) {
   
   if(data) {
   
  		$.ajax({
			url: 'index.php?route=extension/shipping/xshippingpro/save_general&token=<?php echo $token; ?>',
			dataType: 'json',
        	data:data,
        	type:'POST',
			success: function(json) {		
			  if (json['error']) {
                 alert(json['error']);
                 $('.global-waiting').remove();
               } 
			}
		});
	}
 }
 
 function saveMethod() {
   
   if(shipping_methods.length > 0) {
   
   		var method_data = shipping_methods.pop();
  		$.ajax({
			url: 'index.php?route=extension/shipping/xshippingpro/quick_save&token=<?php echo $token; ?>&tab_id='+method_data.tab_id + '&sort_order='+method_data.sort_order,
			dataType: 'json',
        	data:method_data.data,
        	type:'POST',
			success: function(json) {		
			  if (json['error']) {
                 alert(json['error']);
                 $('.global-waiting').remove();
               }
               else {
               	 if (shipping_methods.length === 0) {
               	   $('.global-waiting').remove();
               	   
               	   if(save_type === 'save_form') {
               	     location = '<?php echo htmlspecialchars_decode($cancel); ?>';
               	   }
               	   	
               	 } else {
               	   saveMethod();
               	 }
               }   
			}
		});
	}
 }
 
 function deleteMethod(tab_id) {
   
   if(tab_id) {
   		showLoader();
  		$.ajax({
			url: 'index.php?route=extension/shipping/xshippingpro/delete&token=<?php echo $token; ?>&tab_id='+tab_id,
			dataType: 'json',
        	type:'GET',
			success: function(json) {		
			  if (json['error']) {
                 alert(json['error']);
               } 
               $('.global-waiting').remove();	  
			}
		});
	}
 }
 
 function showLoader() {
   $('body').append('<div class="global-waiting">Processing...</div>');
   $('.global-waiting').css({top:'50%',left:'50%',marginTop:'-40px',marginLeft:'-75px'});
 }
 
 function enableEvents(no_of_tab){ 
		  /* new */
		  var my_method_area=$('#shipping-'+no_of_tab);
         $('#method-list a.tab'+no_of_tab).trigger('click');
		  $('#language'+no_of_tab+' li:first-child').trigger('click');
		  $('#method-tab-'+no_of_tab+' li:first-child').trigger('click');
		  $("[data-toggle='tooltip']",my_method_area).tooltip(); 
		  $('input[name=\'category\']', my_method_area).autocomplete(auto_complete_cat);
		  $('input[name=\'product\']', my_method_area).autocomplete(auto_complete_prod);
		  $('input[name=\'option\']', my_method_area).autocomplete(auto_complete_option); 
      $('input[name=\'input_manufacturer\']', my_method_area).autocomplete(auto_complete_manufacturer); 
      $('input[name=\'input_location\']').autocomplete(auto_complete_location);
		  $('input.hide-shipping', my_method_area).autocomplete(auto_complete_methods);

      $('.date').datetimepicker({
        pickTime: false
      });
 }
 
 function copyMethod(tabId) {
   
	      var no_of_tab=$('#shipping-container').find('div.shipping').length;
	      no_of_tab=parseInt(no_of_tab)+1;
	      //finding qnique id
	      while($('#shipping-'+no_of_tab).length!=0)
	      {
		   no_of_tab++;
	      }
          
          var tab_item=$('#shipping-'+tabId).clone()
          var tab_html='<div id="shipping-'+no_of_tab+'" class="tab-pane shipping">'+tab_item.html()+'</div>';
          
		   
 		  tab_html = tab_html.replace(/xshippingpro\[([a-z_]+)\]/igm, 'xshippingpro[$1]');
 		  tab_html = tab_html.replace(/_(\d+)/g, '_'+no_of_tab); 
          
		  $('#shipping-container').append(tab_html); 
		  
		  var inputs_text = $('#shipping-'+tabId+' input[type="text"]');
		  var inputs_text_new = $('#shipping-'+no_of_tab+' input[type="text"]');
		  
		  var inputs_checkboxes = $('#shipping-'+tabId+' input[type="checkbox"]');
		  var inputs_checkboxes_new = $('#shipping-'+no_of_tab+' input[type="checkbox"]');
		  
		  var inputs_selects = $('#shipping-'+tabId+' select');
		  var inputs_selects_new = $('#shipping-'+no_of_tab+' select');
		  
		  inputs_text.each(function(index) {
              inputs_text_new.eq(index).val($(this).val());
          });
          
          inputs_selects.each(function(index) {
              inputs_selects_new.eq(index).val($(this).val());
          });
          
          inputs_checkboxes.each(function(index) {
              if($(this).prop('checked')) {
                inputs_checkboxes_new.eq(index).prop('checked','checked');
              } else {
                inputs_checkboxes_new.eq(index).removeAttr('checked');
              }   
          });
	
	
		  $('#method-list').append('<li><a draggable="true" data-toggle="draggable tab" class="draggable tab'+no_of_tab+'" href="#shipping-'+no_of_tab+'">'+$('a.tab'+tabId).html()+'</a></li>');
		  enableEvents(no_of_tab);
		  current_tab=no_of_tab;
 }

 function makedropzone() {
     var dragged;
     $("#method-list").on('drag', function(e) { 
        e.preventDefault();
     });
     $("#method-list").on('dragstart', function(e) { 
        var $this = $(e.target);
        dragged = e.target;
        $this.removeClass('dragging').addClass('dragging');
        $this.css('cursor','move');
     });
     $("#method-list").on('dragend', function(e) { 
        var $this = $(e.target);
        $this.removeClass('dragging');
        $this.css('cursor','default');
     });
     $("#method-list").on('dragover', function(e) { 
        e.preventDefault();
     });
     $("#method-list").on('dragenter', function(e) { 
        e.preventDefault();
        var $this = $(e.target);
        if ( $this.hasClass('draggable') && event.target !== dragged) {
           $this.removeClass('dropable').addClass('dropable');
        }
     });
     $("#method-list").on('dragleave', function(e) { 
        e.preventDefault();
        var $this = $(e.target);
        if ($this.hasClass('draggable') ) {
           $this.removeClass('dropable');
        }
     });
     $("#method-list").on('drop', function(e) {
         var $this = $(e.target);
         if ($this.hasClass('draggable')  && event.target !== dragged) { 
            var from =  $(dragged).closest('li');
            var to =  $(event.target).closest('li');
            var tmp = from.prev('li');
            to.prev('li').after(from);
            tmp.after(to);
            $(this).find('li > a').removeClass('dropable dragging');
         }
     });
 }

 
//--></script>
</div>
<?php echo $footer; ?>