<!-- CSS for quantity button -->
<style>
    input[type=number]::-webkit-inner-spin-button,
  input[type=number]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }

  input[type=number] {
    -moz-appearance: textfield;
  }
</style>

<?global $config;
$model_heads = explode("," , $dt_params['dt_headings'] );
?>
<div class="row">
  <div class="col-md-12">
    <!-- BEGIN VALIDATION STATES-->
    <div class="portlet box green">
      <div class="portlet-title">
        <div class="caption">
          <i class="fa fa-shopping-cart"></i><?=humanize($class_name)?>
              <small>Add Details to <?=humanize($class_name)?></small>

        </div>
        <!--<div class="tools">
          <a href="javascript:;" class="collapse">
          </a>
          <a href="#portlet-config" data-toggle="modal" class="config">
          </a>
          <a href="javascript:;" class="reload">
          </a>
          <a href="javascript:;" class="remove">
          </a>
        </div>-->
      </div>
      <div class="portlet-body form">
        <div class="row note note-danger">
          <p>
            <strong>NOTE:</strong> Please make sure only <strong>one "ADD ORDER" page</strong> is open in your browser at a time. 
            And <strong>Do not Add items to Cart from front-end</strong> while this window is open. Thank You!
          </p>
        </div>
        <!-- BEGIN FORM-->
        <?
        global $config;
        // $form = new Tkd_form_helper($class_name);
        // $form->title = ucfirst(str_replace("_", " ", $table));
        // $form->set_param('form_fields',$form_fields);
        // $form->set_param('form_data',$form_data);
        // $form->extra_content = $this->load->view("admin/order/product_form" , array() , true);
        // $form->prepare_form();
        // $form->render_form();
        ?>
        <form id="admin_order_form" class="form-horizontal" action="javascript:void(0)">
        <div class="table-scrollable product">
	<div class="portlet box red">
		<div class="portlet-title">
			<div class="caption">
			<div class="tools">
			<i class="fa fa-cogs"></i> Add Products
			</div>
			</div>
		</div>
	</div>
	<table class="table table-striped table-hover">
		<thead>
            <th>Product</th>
		</thead>
		<tbody class="data-holder">
		<tr  class="product template cart_container">

			<?// $form = new Tkd_form_helper("oitem"); ?>

            <td class="form-group oitem_product_id">
              <input type="text" id="search-box" data-id="" val="" placeholder="Search By Product Name, SKU or Part No" />
              <div id="suggesstion-box"></div>
              
			      </td>
          

            <td  class="form-group product_id">
            <div class="quantity" >
              <input type="number" id="product-qty-input" min="1" max="50" step="1" value="1">
            </div>
            <!-- <input style="    padding: 10px 0px 10px 0px;text-align: center;
    border: #a8d4b1 1px solid;
    border-radius: 4px;" type="number" id="product-qty-input" data-id="" value="1" min="1" max="10"/> -->

            	<input style="margin-left: 16px;" id="btnadd" type="button" class="btn green add_cart" value="Add to Cart" />
            	<!-- <input id="refresh_cart" type="button" class="btn green load_cart" value="Refresh Cart" /> -->
              <input id="clear_cart" type="button" class="btn green load_cart" value="Clear Cart" />
        	</td>
        	<!-- <div class="input-group inline-group"> -->
            <!-- <div class="input-group-prepend"> -->
              
            <!-- </div> -->
          <!-- </div> -->
		</tr>
		
		</tbody>
    </table>
</div>

<div class="table-scrollable product">
	<div class="portlet box red">
		<div class="portlet-title">
			<div class="caption">
			<div class="tools">
			<i class="fa fa-cogs"></i> Cart Items
			</div>
			</div>
		</div>
	</div>
	<table class="table table-striped table-hover">
		<thead>
			<th><b>SKU</b></th>
			<th><b>Product Name</b></th>
			<th><b>Price</b></th>
			<th><b>Quantity</b></th>
			<th><b>Item Total</b></th>
			<th></th>
		</thead>
		<tbody class="cart_body" id="cart_body">
			<?
			$cart_data = $this->cart->contents();
			$cart_count = count($this->cart->contents());
			$total_weight = 0;
			// debug($cart_data,1);
			foreach($cart_data as $key=>$value):
			$total_product_weight = $value['options']['product_weight']*$value['qty'];
			$total_weight = $total_weight + $total_product_weight;
			?>
				<tr>
					<td><?=$value['options']['product_sku']?></td>
					<td><?=$value['name']?></td>
					<td>$<?=$value['price']?></td>
					<td><?=$value['qty']?></td>
					<td>$<?=$value['subtotal']?></td>
				</tr>
			<?endforeach?>
		</tbody>
    </table>
</div>
<div class="row">
	<div class="col-xs-4 invoice-block" style="float:right;text-align:right; margin-right: 43px;">
		<ul class="list-unstyled amounts"  id="cart_info">
			<li><strong style="color:#333">Total Products</strong> : <span id="cart_total_items"><?=$cart_count?></span> </li>
			<li><strong style="color:#333">Total Weight</strong> : <span id="cart_total_weight"><?=$total_weight?> oz</span>  </li>
			<li><strong style="color:#333">Total Price</strong> : <span id="cart_total">$<?=$this->cart->total()?></span>  </li>
		</ul>
		<br>
	</div>
  
</div>
          <div class="form-body">
            
            <div id="shipping_details">
                <div>
                    <h4>
                        Shipping Details:
                    </h4>
                </div>

                <div class="form-group ">
                  <label class="control-label col-md-2 ">Existing Customer</label>
                  <div class="col-md-3">
                    <select class="form-control" id="select_customer" name="admin_order[admin_order_user_id]">
                      <option disabled selected value="0">Select</option>
                      <?foreach($users as $key=>$value):?>
                      <option value="<?=$value['signup_id']?>"><?=$value['signup_firstname'].' '.$value['signup_lastname']?></option>
                      <?endforeach?>
                    </select>
                  </div>
                  <div class="col-md-6">
                    <p><span style="background-color: yellow;color:black"><b>NOTE :</b>Please select existing customer carefully.<br/>This option won't be available after this order is saved</span></p>
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Firstname<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_firstname" name="admin_order[admin_order_shipping_firstname]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Lastname<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_lastname" name="admin_order[admin_order_shipping_lastname]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Email<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_email" name="admin_order[admin_order_shipping_email]" type="text" value="">
                  </div>
                </div>

                <div class="form-group ">
                  <label class="control-label col-md-2 ">Company</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_company" name="admin_order[admin_order_shipping_company]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Address<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_address1" name="admin_order[admin_order_shipping_address1]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Country<span class="required">* </span></label>
                  <div class="col-md-3">
                    <!-- <input class=" form-control " id="order-order_shipping_country" name="admin_order[admin_order_shipping_country]" type="text" value=""> -->
                    <select class="form-control" id="order-order_shipping_country" name="admin_order[admin_order_shipping_country]">
                            <?foreach($country as $key => $val):?>
                                <option <?=$val=='USA'?'selected':'' ?> value=<?=$val?>><?=$val?></option>
                            <?endforeach?>
                      </select>
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">State<span class="required">* </span></label>
                  <div class="col-md-3">
                    <select class="form-control" name="admin_order[admin_order_shipping_state]" id="shipping_state">
                            <option disabled selected value="0">Select State</option>
                            <?foreach($states as $key=>$val):?>
                              <option value="<?=$val['state_id']?>"><?=$val['state_name']?></option>
                            <?endforeach?>
                    </select>
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">City<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_city" name="admin_order[admin_order_shipping_city]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Zip Code<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_zip" name="admin_order[admin_order_shipping_zip]" type="text" value="">
                  </div>
                </div> 
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Phone<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_shipping_phone" name="admin_order[admin_order_shipping_phone]" type="text" value="">
                  </div>
                </div>
                
                
            </div>  
            
            <div class="form-group ">
                  <label class="control-label col-md-2 ">Merchant Fee</label>
                  <div class="col-md-3">
                    <!-- <input class=" form-control " id="order-order_billing_country" name="admin_order[admin_order_billing_country]" type="text" value="">                     -->
                    <select class="form-control" id="admin_order[admin_order_merchantfee]" name="admin_order[admin_order_merchantfee]">
                            <option value="0">Free -- 0</option>
                            <option value="4.5">PayPal -- 4.5%</option>
                            <option value="3.5">CreditCard -- 3.5%</option>
                    </select>
                  </div>
            </div>
            
            <div class="form-group ">
                  <label class="control-label col-md-2 ">Custom Charge Fields</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_custom_field" name="admin_order[admin_order_custom_field]" type="text" value="">
                  </div>
                  <div class="col-md-6">
                    <p><b>NOTE :</b> Field values set in here will be added in the final amount.<br> Multiple fields can be added by using <b>','</b> as separator. Use <b>':'</b> to seprate label and value.<span style="background-color: yellow;"><br>For e.g: To add custom field 1 and custom field 2 use this syntax after hypehn - <b>Field1Label:Value1,Field2Label:Value2</b></span></p>
                  </div>
            </div>
              
              <div class="form-group ">
              <label class="control-label col-md-2 ">Select Shipping Method<span class="required">* </span></label>
              <div class="col-md-3">
                <div id="shipping-table">Enter shipping zip code to get available shipping methods
                </div>
              </div>
            </div>

            <div class="form-group ">
              <label class="control-label col-md-2 ">Shipment Price</label>
              <div class="col-md-3">
                <input class=" form-control" readonly="readonly" id="shipping_cost" name="admin_order[admin_order_shipment_price]" type="text" value="">
                <input class=" form-control" readonly="readonly" id="admin_order_shipping" name="admin_order[admin_order_shipping]" type="hidden" value="">

              </div>
            </div>
            
            <div class="form-group ">
              <label class="control-label col-md-2 ">Shipment Tax</label>
              <div class="col-md-3">
                <input class=" form-control " id="order-order_tax" name="admin_order[admin_order_tax]" type="text" value="">
              </div>
              <div class="col-md-6">
                    <p style="margin-top: 5px;color:green"><b><span id="isExempted"></span></b></p>
              </div>
            </div>
            <div class="form-group ">
                  <label style="font-size: 15px;font-weight: bold;padding-top:0px;" for="input-sameasshipping" class="control-label col-md-4 ">
                      Billing and shipping details are same</label>
                  <div class="col-md-5">
                    <input id="input-sameasshipping" type="checkbox" value="true" checked="checked">
                  </div>
            </div> 
            <div class="form-group">
                  <label style="font-size: 15px;font-weight: bold;padding-top:0px;" for="customership" class="control-label col-md-4 ">
                      Use customer shipping account</label>
                  <div class="col-md-5">
                    <input id="customership" type="checkbox" class="custom-control-input">
                  </div>
            </div> 
            
            <div id="billing_details" style="display:none;">
                  <div>
                      <h4>
                          Billing Details:
                      </h4>
                  </div>

                <div class="form-group ">
                  <label class="control-label col-md-2 ">Firstname<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_firstname" name="admin_order[admin_order_billing_firstname]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Lastname<span class="required">* </span></label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_lastname" name="admin_order[admin_order_billing_lastname]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Email</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_email" name="admin_order[admin_order_billing_email]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Company</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_company" name="admin_order[admin_order_billing_company]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Address</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_address" name="admin_order[admin_order_billing_address]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Country</label>
                  <div class="col-md-3">
                    <!-- <input class=" form-control " id="order-order_billing_country" name="admin_order[admin_order_billing_country]" type="text" value="">                     -->
                    <select class="form-control" id="billing_country" name="admin_order[admin_order_billing_country]">
                            <?foreach($country as $key => $val):?>
                                <option <?=$val=='USA'?'selected':'' ?> value=<?=$val?>><?=$val?></option>
                            <?endforeach?>
                     </select>
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">State<span class="required">* </span></label>
                  <div class="col-md-3">
                    <!-- <input class=" form-control " id="order-order_billing_state" name="admin_order[admin_order_billing_state]" type="text" value=""> -->
                    <select class="form-control" name="admin_order[admin_order_billing_state]" id="billing_state">
                            <option disabled selected value="0">Select State</option>
                            <?foreach($states as $key=>$val):?>
                              <option value="<?=$val['state_id']?>"><?=$val['state_name']?></option>
                            <?endforeach?>
                    </select>
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">City</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_city" name="admin_order[admin_order_billing_city]" type="text" value="">
                  </div>
                </div>
                
                <div class="form-group ">
                  <label class="control-label col-md-2 ">Phone</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_phone" name="admin_order[admin_order_billing_phone]" type="text" value="">
                  </div>
                </div>
                
                 <div class="form-group ">
                  <label class="control-label col-md-2 ">Zip Code</label>
                  <div class="col-md-3">
                    <input class=" form-control " id="order-order_billing_zip" name="admin_order[admin_order_billing_zip]" type="text" value="">
                  </div>
                </div>

        
  	          </div>
  	          
            
            <div class="form-actions">
	                <div class="row">
	                    <div class="col-md-offset-3 col-md-9">
	      		            <button type="button" id ="create_admin_order" value="" class="btn green">Save</button>
				        </div>
	                </div>
  	            </div>
          </div>	
        </form>
        <!-- END FORM-->
      </div>
      <!-- END VALIDATION STATES-->
    </div>
    
  </div>
  
</div>

<script>
  total_items = 0 + <?=$cart_count?>;
  function add_cart(prod_id)
  {
    var product_id = prod_id;
    var product_qty = $('#product-qty-input').val();
    // alert(prod_id);
    // alert(product_qty);
    // return false;
    if (product_id)
    {
      $.ajax({
        method:'POST',
        url:'<?= base_url() ?>checkout/add_cart',
        data: {product_id:product_id, product_qty:product_qty},
        success: function(response){
          var data = jQuery.parseJSON(response);
          total_items = data.total_items;
          AdminToastr.success("Item successfully added to your Cart !", 'Success');
          console.log(data);
          setTimeout(function(){
            $('#search-box').val('');
            $("#search-box").attr('data-id','');
            // location.reload()
            refresh_cart();
          },1);
        }
      });
    }
    else{
      console.log('productID not exist')
    }
  }

// $('#refresh_cart').click(function(){
  function refresh_cart()
    {
      $.ajax({
        method:'POST',
        url:'<?= base_url() ?>admin/order/refresh_cart',
        data: {},
        success: function(response)
        {
          var data = jQuery.parseJSON(response);
          if (data)
          {
            $('#cart_body').html(data.cart_body);
            $('#cart_info').html(data.cart_info);
          }
        }
      });
    }
// });


  function getProductID(id, name) {
    $("#search-box").val(name);
    $("#search-box").attr('data-id', id);
    $("#suggesstion-box").hide();
  }

  function clear_cart()
  {
    if(confirm("Are you sure to clear all items from cart?"))
    {
      clearCart = true;
      total_items = 0;
      $.ajax({
            method:'POST',
            url:'<?= base_url() ?>admin/order/clear_cart',
            data: {'clear_cart':clearCart},
            success: function(response){
              var data = jQuery.parseJSON(response);
              if(data.status == 1)
              {
                AdminToastr.success(data.msg, 'Success');
                setTimeout(function(){
                  refresh_cart();
                // location.reload()
                },1);
              }
              else
              {
                AdminToastr.error(data.msg, 'Error');
              }
              console.log(data);
            }

          });
      }
  }

function get_shipping_cost()
{
    var zipcode=$("#order-order_shipping_zip").val();
    var fedtoken = '<?=$fedtoken?>';
  //   console.log(fedtoken);
  //   console.log(zipcode);
    if (zipcode.length == 5)
    {
      // Loader.show();
    //   console.log(zipcode);
      $.ajax({
        method:'POST',
        url:'<?= base_url() ?>checkout/fedex_rates',
        data: {"zip":zipcode, "token":fedtoken},
        beforeSend: function() {
          $("#shipping-table").css("background", "#FFF url('<?=base_url()?>assets/admin/img/LoaderIcon.gif') no-repeat 100%");
        },
        success: function(response){
        //   var data = jQuery.parseJSON(response);
          console.log(response);
          if(response)
          {
            // $('#shippin').html('<label>Select Shipping method</label>');
            $("#shipping-table").css("background", "#FFF");
            $('#shipping-table').html(response);
            //   $('#shipping_price').val();
            
              // Loader.hide();
          }
        // setInterval(function(){
        //     location.reload();
            
        //   }, 500);
        }

      });
}
else
{
    return false;
}
}
    
$(document).ready(function() {    
      Metronic.init(); // init metronic core components
      Layout.init(); // init current layout
      QuickSidebar.init(); // init quick sidebar
      Demo.init(); // init demo features
      UIAlertDialogApi.init(); //UI Alert API
      <?if($error)
          echo "AdminToastr.error('".str_replace("\n","",validation_errors('<div>', '</div></br>'))."');";
      ?>

  });

$('#btnadd').click(function(){
	product_id = $("#search-box").attr('data-id');
	add_cart(product_id);
});

$( "#order-order_shipping_zip" ).keyup(function() {
    if($('#customership').is(':checked')){
        // var response = "<tr><td><div class='custom-control custom-radio'><input checked type='radio' name='shipping' class='custom-control-input' value='0' data-type='Free' id='ship-00'><label class='custom-control-label'<td>Free</td><td>Customer Shipping Account</td></tr>"
        // $("#shipping-table").css("background", "#FFF");
        // $('#shipping-table').html(response);
        $('#shipping_cost').attr('readonly',false)
        $('#shipping_cost').val(0)
    }
    else{
        get_shipping_cost();
    }
});

$(document).on('change','input[type=radio][name=shipping]',function(){
  var shippment_price = $("input[name=shipping]:checked").val()
  $.ajax({
    method:'POST',
    url:'<?= base_url() ?>checkout/set_shipping_cost',
    data: {"shipping_cost":shippment_price},
    success: function(response){
      var data = jQuery.parseJSON(response);
      if(data.status == 1)
      {
        //   console.log(data);
          $('#shipping_cost').val(shippment_price);
          $('#admin_order_shipping').val($('input[name=shipping]:checked').attr('data-type'));
          shippment_cost = shippment_price;
        }}
  });
});

$("#create_admin_order").click(function (e) {
  e.preventDefault();
  if(total_items < 1)
  {
      AdminToastr.error("Cart is empty!"); 
      return false;
  }
  $('#create_admin_order').prop('disabled', true);
  // Loader.show();
  
  bill_same_as_shipping();
  
  var obj = $("#admin_order_form");
  var data = jQuery("#admin_order_form").serialize();
  console.log(data);
//   return false;
  // alert(data);
  var url = "<?= base_url()?>admin/admin_order/create_admin_order";
  $.ajax({
    method:'POST',
    url:url,
    data: $("#admin_order_form").serialize(),
    success: function(response){
      var data = jQuery.parseJSON(response);
      if(data.status == 1)
      {
        AdminToastr.success(data.txt,'Success');
        setTimeout(function () {
          window.location.href = "<?=base_url()?>admin/admin_order";
        },500);
      }
      else
      {
        $('#create_admin_order').prop('disabled', false);
        // Loader.hide();
        AdminToastr.error(data.txt,'Error');
      }
    }
  });

});

function get_state_tax()
{
    var state_id = $("#billing_state").val();
    var customer_id = $('#select_customer').val()
    $.ajax({
      method:'POST',
      url:'<?= base_url() ?>checkout/get_state_tax',
      data: {"state_id":state_id, "customer_id":customer_id},
      beforeSend: function() {
          $("#order-order_tax").css("background", "#FFF url('<?=base_url()?>assets/admin/img/LoaderIcon.gif') no-repeat 100%");
        },
      success: function(response){
        var data = jQuery.parseJSON(response);
        $("#order-order_tax").css("background", "#FFF");
      
        if(data.tax_exempted == 1)
        {
          $("#isExempted").text("Exempted");
        }
        else{
          $("#isExempted").text("");
        }

        if(data.status == 1)
        {
              $('#order-order_tax').val(data.taxed_amount);

              // calculate_order_total();
        }
      // setInterval(function(){
      //     location.reload();
          
      //   }, 500);
      }
    });
}

    
$("#billing_state").change(function(){
    // get_state_tax();
    if($('#customership').is(':not(:checked)'))
    {
        get_state_tax();   
    }else{
        $('#order-order_tax').val(0);
    }
});

$("#customership").change(function(){
        if(this.checked){
            alert('!!!NOTE!!! Please verify ZIP Code, State, Tax and shippment charges before continuing. Calculation of tax and shippment price is disabled with this option enabled.')
            $('#shipping_cost').attr('readonly',false)
            $('#shipping_cost').val(0)
            $('#order-order_tax').val(0);
            $('#order-order_shipping_zip').val('')
            $('#order-order_billing_zip').val('')
            var response = "<tr><td><div class='custom-control custom-radio'><input checked type='radio' name='shipping' class='custom-control-input' value='0' data-type='Free' id='ship-00'><label class='custom-control-label'<td>Free</td><td>Customer Shipping Account</td></tr>"
            $("#shipping-table").css("background", "#FFF");
            $('#shipping-table').html(response);
        }else{
            $('#shipping_cost').attr('readonly',true)
            $('#shipping-table').html('Enter shipping zip code to get available shipping methods');
        }
});

$("#shipping_state").change(function(){
    if ($('#input-sameasshipping').is(':checked'))
    {
        $("#billing_state").val($("#shipping_state").val());
        if($('#customership').is(':not(:checked)'))
        {
            get_state_tax();   
        }else{
            $('#order-order_tax').val(0);
        }
    }
  });


$(document).ready(function() {

	$("#search-box").keyup(function() {
		$.ajax({
			type: "POST",
			url: "<?=base_url()?>product/search_product",
			data: {'keyword':$(this).val()},
			beforeSend: function() {
				$("#search-box").css("background", "#FFF url('<?=base_url()?>assets/admin/img/LoaderIcon.gif') no-repeat 100%");
			},
			success: function(data) {
				$("#suggesstion-box").show();
				$("#suggesstion-box").html(data);
				$("#search-box").css("background", "#FFF");
			}
		});
	});
	
// 	if ($('#input-sameasshipping').is(':checked'))
//     {
//         $('#shipping_details').show();
//     }

// $("[type='number']").keypress(function (evt) {
//     evt.preventDefault();
// });



});

$('#clear_cart').click(function(){
  clear_cart();
});

$(document).click(function(e) { 
    // Check for left button
    if (e.button == 0) {
      $("#suggesstion-box").hide();
    }
});

  </script>

<script>
  $(document).ready(function () {
  jQuery('<div class="quantity-nav"><button class="quantity-button quantity-up">&#xf106;</button><button class="quantity-button quantity-down">&#xf107</button></div>').insertAfter('.quantity input');
  jQuery('.quantity').each(function () {
    var spinner = jQuery(this),
        input = spinner.find('input[type="number"]'),
        btnUp = spinner.find('.quantity-up'),
        btnDown = spinner.find('.quantity-down'),
        min = input.attr('min'),
        max = input.attr('max');

    btnUp.click(function () {
      var oldValue = parseFloat(input.val());
      if (oldValue >= max) {
        var newVal = oldValue;
      } else {
        var newVal = oldValue + 1;
      }
      spinner.find("input").val(newVal);
      spinner.find("input").trigger("change");
    });

    btnDown.click(function () {
      var oldValue = parseFloat(input.val());
      if (oldValue <= min) {
        var newVal = oldValue;
      } else {
        var newVal = oldValue - 1;
      }
      spinner.find("input").val(newVal);
      spinner.find("input").trigger("change");
    });

  });
});

function bill_same_as_shipping()
{
    if ($('#input-sameasshipping').is(':checked'))
    {
        $('#billing_details').hide();
        $("#order-order_billing_firstname").val($("#order-order_shipping_firstname").val());
        $("#order-order_billing_lastname").val($("#order-order_shipping_lastname").val());
        // $("#order-order_shipping_country").val($("#order-order_billing_country").val());
        $("#order-order_billing_address").val($("#order-order_shipping_address1").val());
        $("#order-order_billing_zip").val($("#order-order_shipping_zip").val());
        $("#order-order_billing_city").val($("#order-order_shipping_city").val());
        $("#billing_state").val($("#shipping_state").val());
        $("#order-order_billing_email").val($("#order-order_shipping_email").val());
        $("#order-order_billing_company").val($("#order-order_shipping_company").val());
        $("#order-order_billing_phone").val($("#order-order_shipping_phone").val());
        
    }
}

function bill_same_as_shipping1()
{
    if ($('#input-sameasshipping').is(':checked'))
    {
        $('#billing_details').hide();
        $("#order-order_billing_firstname").val($("#order-order_shipping_firstname").val());
        $("#order-order_billing_lastname").val($("#order-order_shipping_lastname").val());
        // $("#order-order_shipping_country").val($("#order-order_billing_country").val());
        $("#order-order_billing_address").val($("#order-order_shipping_address1").val());
        $("#order-order_billing_zip").val($("#order-order_shipping_zip").val());
        $("#order-order_billing_city").val($("#order-order_shipping_city").val());
        $("#billing_state").val($("#shipping_state").val());
        $("#order-order_billing_email").val($("#order-order_shipping_email").val());
        $("#order-order_billing_company").val($("#order-order_shipping_company").val());
        $("#order-order_billing_phone").val($("#order-order_shipping_phone").val());
        
    }
    else
    {
        $('#billing_details').show();
        $("#order-order_billing_firstname").removeAttr('value');
        $("#order-order_billing_lastname").removeAttr('value');
        $("#order-order_billing_address").removeAttr('value');
        $("#order-order_billing_zip").removeAttr('value');
        $("#order-order_billing_city").removeAttr('value');
        $('#billing_state option[value="0"]').attr("selected", "selected");
        $("#order-order_billing_email").removeAttr('value');
        $("#order-order_billing_company").removeAttr('value');
        $("#order-order_billing_phone").removeAttr('value');

    }
}

$('#input-sameasshipping').change(function(){
    bill_same_as_shipping1();
});

$('#select_customer').change(function(){
  var customer_id = $('#select_customer').val()
  $.ajax({
    method:'POST',
    url:'<?=base_url()?>admin/admin_order/get_customer_detail',
    data: {customer_id : customer_id},
    beforeSend: function() {
          $("#select_customer").css("background", "#FFF url('<?=base_url()?>assets/admin/img/LoaderIcon.gif') no-repeat 100%");
        },     
    success: function(response){
      $("#select_customer").css("background", "#FFF");
      var data = jQuery.parseJSON(response);
      // console.log(data);

      if(data.status == 1)
      {
        load_customer_details(data.customer)
        AdminToastr.success(data.txt,'Success');
      }
      else
      {
        // Loader.hide();
        AdminToastr.error(data.txt,'Error');
      }
    }
  });
});

function load_customer_details(data)
{
  // console.log(data);

  $('#input-sameasshipping').removeAttr('checked');
  $('#input-sameasshipping').val(false);
  $('#uniform-input-sameasshipping').find('span').removeClass('checked');
  $('#billing_details').show();

  $("#order-order_billing_firstname").val(data.signup_firstname);
  $("#order-order_billing_lastname").val(data.signup_lastname);
  $("#billing_country").val(data.signup_country);
  $("#order-order_billing_address").val(data.signup_address);
  $("#order-order_billing_zip").val(data.signup_zip);
  $("#order-order_billing_city").val(data.signup_city);
  $("#billing_state").val(data.signup_state);
  $("#order-order_billing_email").val(data.signup_email);
  $("#order-order_billing_company").val(data.signup_company);
  $("#order-order_billing_phone").val(data.signup_contact);

  $("#order-order_shipping_firstname").val(data.signup_shipping_firstname);
  $("#order-order_shipping_lastname").val(data.signup_shipping_lastname);
  $("#order-order_shipping_address1").val(data.signup_shipping_address1);
  $("#order-order_shipping_zip").val(data.signup_shipping_zip);
  $("#order-order_shipping_city").val(data.signup_shipping_city);
  $("#shipping_state").val(data.signup_shipping_state);
  $("#order-order_shipping_country").val(data.signup_shipping_country);
  $("#order-order_shipping_email").val(data.signup_shipping_email);
  $("#order-order_shipping_company").val(data.signup_shipping_company);
  $("#order-order_shipping_phone").val(data.signup_shipping_phone);
  get_state_tax(); 
  get_shipping_cost();
}


</script>