<?php $this->load->view("common/header"); ?>  
<!-- end: HEAD -->
<style type="text/css">
			label {
					font-weight: 700;
				  }
				.table > thead:first-child > tr:first-child > th, .table > thead:first-child > tr:first-child > td, .table-striped thead tr.primary:nth-child(2n+1) th {
				    background-color: #428bca;
				    border-color: #357ebd;
				    border-top: 1px solid #357ebd;
				    color: white;
				    text-align: center;
				}
				
				.form-horizontal .form-group {
 					 margin-left: 0;
 					 margin-right: 0;
				}
</style>
<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->

<body>

<!-- start: HEADER -->
<div class="navbar navbar-inverse navbar-fixed-top"> 
  <!-- start: TOP NAVIGATION CONTAINER -->
  <div class="container">
    <div class="navbar-header"> 
      <!-- start: RESPONSIVE MENU TOGGLER -->
      <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="clip-list-2"></span> </button>
      <!-- end: RESPONSIVE MENU TOGGLER --> 
      <!-- start: LOGO -->
      <?php $this->load->view("common/logo"); ?>
      <!-- end: LOGO --> 
    </div>
    <div class="navbar-tools"> 
      <!-- start: TOP NAVIGATION MENU -->
      <?php $this->load->view("common/notifications.php"); ?>
      <!-- end: TOP NAVIGATION MENU --> 
    </div>
  </div>
  <!-- end: TOP NAVIGATION CONTAINER --> 
</div>
<!-- end: HEADER --> 
<!-- start: MAIN CONTAINER -->
<div class="main-container">
<div class="navbar-content"> 
  <!-- start: SIDEBAR -->
  <?php $this->load->view("common/navigation"); ?>
  <!-- end: SIDEBAR --> 
</div>
<!-- start: PAGE -->
<div class="main-content">
<!-- end: SPANEL CONFIGURATION MODAL FORM -->
<div class="container">
<!-- start: PAGE HEADER -->
<div class="row">
  <div class="col-sm-12"> 
    <!-- start: PAGE TITLE & BREADCRUMB -->
    <ol class="breadcrumb">
      <li> <a href="#"> Dashboard </a> </li>
      <li> <a href="<?php echo base_url(''); ?>"> Customer Order </a> </li>
      <li class="active"> Add New </li>
      <li class="search-box">
        <form class="sidebar-search">
          <div class="form-group">
            <input type="text" placeholder="Start Searching...">
            <button class="submit"> <i class="clip-search-3"></i> </button>
          </div>
        </form>
      </li>
    </ol>
    <div class="page-header">
      <h3>Customer Order </h3>
    </div>
    <p>Please fill in the information below. The field labels marked with * are required input fields.</p>
  </div>
</div>
<!-- end: PAGE HEADER --> 
<!-- start: PAGE CONTENT 
                    <!-- start grid -->

<div class="row">
  <div class="col-md-12"> 
    <!-- start: DYNAMIC TABLE PANEL -->
    <div class="panel panel-default">
      <div class="panel-heading"> <i class="fa fa-plus"></i>Customer Order
        <?php if(isset($sale_rep_issue_id)){echo" (Reference No: ".$sales_rep_issue_details['sales_rep_issue_ref_no'].")"; }?>
      </div>
      <div class="panel-body">
        <div class="alert alert-danger" style="display:none;">
          <button class="close" data-dismiss="alert"> × </button>
          <i class="fa fa-times-circle"></i> <strong></strong> <span class="errortxt"></span> </div>
        <div class="alert alert-success" style="display:none;">
          <button class="close" data-dismiss="alert"> × </button>
          <i class="fa fa-check-circle"></i> <strong></strong><span class="succetxt"></span> </div>
        <form role="form" class="form-horizontal" id="create_sales_form" action="#" method="post">
          <div class="col-md-12"></div>
          <!--col-md-12-->
          <div class="col-md-12">
            <div class="col-md-2">
              <div class="form-group">
                <label>Date *</label>
                <input id="sales_rep_issue_datetime" name="sales_rep_issue_datetime" type='text' class="form-control date" value="						                 <?php if(isset($sale_rep_issue_id))
                          { $source = $sales_rep_issue_details['sales_rep_issue_date'];
							   $date = new DateTime($source);
							   $olddate=$date->format('m.d.Y'); echo $olddate;} ?>" data-bv-field="date"/>
              </div>
            </div>
            <input type='hidden' class="form-control" id="sale_reference_no" name="sale_reference_no" value="<?php if(isset($sale_rep_issue_id))echo $sale_rep_issue_id ?>" readonly/>
            <div class ="col-md-2">
              <div class ="form-group">
                <label>Warehouse *</label>
                <select id="warehouse_id" class="form-control search-select" name ="warehouse_id" onClick="load_dependent()">
                 <!-- <option value="">-Select Warehose-</option>-->
                  <?php	foreach ($warehouse_list as $row){?>
                  <option value="<?php echo $row->id; ?>" <?php  if(isset($sale_rep_issue_id)&&$row->id==$sales_rep_issue_details['wharehouse_id'])echo 'selected="selected"'?>> <?php echo $row->name; ?> </option>
                  <?php }?>
                </select>
              </div>
            </div>
            
            <div class="col-sm-2">
              <div class="form-group">
                <label>Area  * </label>
                <select id="route_id" class="form-control search-select" name="route_id" onChange="load_customer_list(this.value)">
                 <option value="">- Select -</option>
                </select>
              </div>
            </div>
            
            <div class="col-md-2" >
              <div class="form-group">
                <label>Sales Representative*</label>
                <select  id="sales_rep_id" class="form-control search-select" name="sales_rep_id"  >
                 
                </select>
              </div>
            </div>
            
            <div class="col-md-2">
			<div class="form-group">
			<label>Invoice Type * </label>
			<select id="in_type" class="form-control search-select" name="in_type" >
			<?php	foreach ($in_type_list as $itl){?>
                  <option value="<?php echo $itl['invoice_type_id']; ?>" > <?php echo $itl['invoice_type_name']; ?> </option>
                  <?php }?>
			</select>
			</div>
			</div>
            
            <div class="col-sm-4">
              <div class="col-sm-12">
                <div class="form-group">
                  <label for="customer_id">Customer * </label>
                  <select onChange="" id="customer_id" class="form-control search-select" name="customer_id">
                  </select>
                  <a id="modal_ajax_customers_btn" style="cursor:pointer;"><i style="float: left;position: absolute;margin-left: 7px;margin-top: 5px;" class="fa fa-2x fa-plus-circle addIcon" id="addIcon"> </i></a> </div>
              </div>
            </div>
            
          </div>
          <!--col-md-8--> 
          
          <!-- item add box-->
          <div id="sticker" class="col-md-12">
            <div class="well well-sm">
              <div class="form-group"> 
                <!-- auto complete start -->
                <div class="input-group wide-tip">
                  <div style="padding-left: 10px; padding-right: 10px;" class="input-group-addon"> <i class="fa fa-2x fa-barcode addIcon"></i> </div>
                  <input type="text" placeholder="Please add products to order list" id="add_item" class="form-control input-lg" value="" name="add_item" onClick="add_items()" style="border-radius: 6px;font-size: 18px;height: 46px;line-height: 1.33;padding: 10px 16px;">
                </div>
                <!-- end auto complete end --> 
                
              </div>
              <div class="clearfix"></div>
              <div class="control-group table-group">
                <label class="table-label">Order Items</label>
                <div class="controls table-controls">
                  <table class="table items table-striped table-bordered table-condensed table-hover" id="soTable">
                    <thead>
                      <tr>
                        <th class="col-md-1">No</th>
                        <th class="col-md-6">Product Name (Product Code)</th>
                        <th class="col-md-2">Selling Price</th>
                        <th class="col-md-1">Quantity</th>
                        <th class="col-md-1 text-right">Discount </th>
                        <th class="col-md-1 text-right">Subtotal<span class="currency"></span> </th>
                        <th style="width: 30px !important; text-align: center;"><i class="fa fa-trash-o"></i></th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr data-item-id="144680019784" class="row_144680019784" id="row_1446800197032">
                        <td></td>
                        <td class="text-right"></td>
                        <td></td>
                        <td class="text-right"></td>
                        <td class="text-right"></td>
                        <td class="text-center"></td>
                      </tr>
                    </tbody>
                    <tfoot>
                      </tr>
                      
                      <tr class="tfoot active" id="tfoot">
                        <th colspan="4" class="text-right">Total Amount</th>
                        <th class="text-right"><span id="Subtotal">0.00</span></th>
                      </tr>
                    </tfoot>
                  </table>
                  
                 
          
                  
                  <!-- start list --> 
                  <!-- end list --> 
                </div>
              </div>
            </div>
            <!-- end item add box-->
            
            <div id="extras-con" class="row"> </div>
          </div>
          <div class="col-md-12">
           
            <div class="col-md-2">
              <div class="form-group">
                <label>Order Discount</label>
                <br>
                <input type="text" title="" data-original-title="" value="" class="form-control input-tip" id="sale_inv_discount" name="sale_inv_discount" onChange="changeMainDiscount(this.value)">
              </div>
            </div>
            
            <div class="modal-footer" style="margin-bottom:10px;">
              <?php if($type=='A') 
							{
							$save_btn_text='SAVE';
							}
							if($type=='E')
							{							
							$save_btn_text='UPDATE';
							}						
							?>
              <input type="submit" class="btn btn-primary" value="<?php echo $save_btn_text ?>" name="sales_rep_issue" id="sales_rep_issue">
              <button id="reset" class="btn btn-danger" type="button">Reset</button>
            </div>
          </div>
          <input name="sale_inv_discount_amount" type="hidden" id="sale_inv_discount_amount" value="0">
          <input name="sale_total" type="hidden" id="sale_total" value="0">
          <input name="sale_paid" type="hidden" id="sale_paid" value="0">
          <input name="sale_balance" type="hidden" id="sale_balance" value="0">
          <input name="rowCount" type="hidden" id="rowCount" value="0">
          <input name="cost_total" type="hidden" id="cost_total" value="0">
          <input name="cus_credit_limit" type="hidden" id="cus_credit_limit" value="10000">
          <input name="base_url" type="hidden" id="base_url" value="<?php echo base_url();?>">
        </form>
        
        <!-- end: DYNAMIC TABLE PANEL --> 
        
        <!-- footer amount details -->
       <!-- <div style="margin-bottom: 0px; position: fixed; bottom: 0px; width: 1082px; z-index: 50000;" class="well well-sm" id="bottom-total">
          <table style="margin-bottom:0;" class="table table-bordered table-condensed totals">
            <tbody>
              <tr class="warning">
                <td width="20%" class="credit_limit_td_class">Credit Limit <span id="credit_limit_txt" class="credit_limit_val pull-right">0.00</span></td>
                <td class="previous_bill_td_class" style="display:none;">Previous Bill <span id="previous_bill_txt" class="pull-right">0.00</span></td>
                <td width="30%">Total <span id="f_total" class="totals_val pull-right">0.00</span></td>
                <td width="20%">Order Discount <span id="tds" class="totals_val pull-right">0.00</span></td>
                <td>Grand Total <span id="gtotal" class="totals_val pull-right">0.00</span></td>
              </tr>
            </tbody>
          </table>
        </div>-->
        <!-- end footer amount details --> 
        
      </div>
    </div>
    
    <!-- end grid --> 
    
  </div>
  <!-- end: PAGE --> 
</div>
<!-- end: MAIN CONTAINER --> 
<!-- start: FOOTER -->
<div class="footer clearfix">
  <div class="footer-inner"> 2014 &copy; clip-one by cliptheme. </div>
  <div class="footer-items"> <span class="go-top"><i class="clip-chevron-up"></i></span> </div>
</div>
<!-- end: FOOTER --> 
<!-- start: RIGHT SIDEBAR --> 
<!-- end: RIGHT SIDEBAR -->
<div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
        <h4 class="modal-title">Event Management</h4>
      </div>
      <div class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn btn-light-grey"> Close </button>
        <button type="button" class="btn btn-danger remove-event no-display"> <i class='fa fa-trash-o'></i> Delete Event </button>
        <button type='submit' class='btn btn-success save-event'> <i class='fa fa-check'></i> Save </button>
      </div>
    </div>
  </div>
</div>
<input name="tmpVal" type="hidden" id="tmpVal" value="0">
<input name="" id="type_sale" type="hidden" value="22">
<!-- start ajax model -->
<div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
<!-- end ajax model --> 

<!-- start: MAIN JAVASCRIPTS -->
<?php $this->load->view("common/footer"); ?>
<script src="<?php echo asset_url(); ?>js/jquery-ui.js" ></script> 
<script src="<?php echo asset_url(); ?>js/moment-with-locales.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootstrap-datetimepicker.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootbox.min.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.min.js"></script> 

<!-- end: MAIN JAVASCRIPTS --> 

<!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY VALIDATION--> 
<script src="<?php echo asset_url(); ?>js/form-validation-create_lot_manage.js"></script> 
<script src="<?php echo asset_url(); ?>js/lot_manage.1.js"></script> 
<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY  VALIDATION--> 

<script>


jQuery(document).ready(function() {

load_dependent();
				
});

function load_dependent()
{	
load_areas();
load_salse_reps();	
}

function load_areas()
{	
 var x = document.getElementById("route_id");
x.remove(x.selectedIndex);  
$.post("<?php echo  base_url(); ?>area/get_all_area_by_warehouse_id?WH="+$("#warehouse_id").val(), function(data) {
var sel = $("#route_id");   
sel.empty();
$("#route_id").append('<option value="" > Select Area</option>');
$.each(data, function(i, item){
$("#route_id").append('<option value="' + i + '">'+ item + '</option>');
}) 
},"json");	
}

function load_salse_reps()
{	
 var x = document.getElementById("sales_rep_id");
x.remove(x.selectedIndex);    
	$.post("<?php echo  base_url(); ?>sales_representative_C/get_list_salse_rep_list?WH="+$("#warehouse_id").val(), function(data) {
var sel = $("#sales_rep_id");   
sel.empty();
$("#sales_rep_id").append('<option value="" > Select Rep</option>');
$.each(data, function(i, item){
$("#sales_rep_id").append('<option value="' + i + '">'+ item + '</option>');
}) 
},"json");	
}
var rowcount=1;
function addextraprice(){
	calculateTotal();
}

clearForm();
$('#add_item').keypress(function(e){	
    if ( e.which == 13 ) return false;
    //or...
    if ( e.which == 13 ) e.preventDefault();
 });

function changeQtyByProductID(qty,nxtCount)
{
	calculateTotal();
}

function getNextRefNo(){	
$.post( "<?php echo base_url();?>customer_order_process/get_next_ref_no").done(function( data ) 
{
var obj = jQuery.parseJSON(data);
$('#sales_rep_issue_next_ref_number').val(obj.sales_rep_issue_next_ref_number);		 
});	
}



function insertSalesData(){	
var type='A';
var sale_reference_no=$('#sale_reference_no').val();
var fields = $("#create_sales_form").serialize();
var rowCount=$('#rowCount').val();
	  if(rowCount!=0){
		  $("#sales_rep_issue").prop("disabled", true);
		  $("#sales_rep_issue").val('Please wait...');
		$.post( "<?php echo base_url();?>customer_order_process/save_cus_order", fields)
	  .done(function( data ) {
		  var obj = jQuery.parseJSON(data);
			if(obj.error==1){				
				$('.alert-success').hide();
				$('.alert-danger').show();
				$( ".errortxt" ).text( obj.disMsg );
				window.scrollTo(500, 0);
				//empty item table			
			}
			if(obj.error==0){				
				window.scrollTo(500, 0);				
				$("#soTable > tbody").empty();
				displayNotice('page','Sample successfully added!');
				clearForm();				
				if(obj.sales_rep_issue_id!=0)
				{
				sendUrl='customer_order_process/cus_order_details/'+obj.id;
				}
				else
				{
				sendUrl='customer_order_process/cus_order_details/'+obj.id;
				}
				window.location.href = "<?php echo base_url();?>"+sendUrl;				
			}			
	  });
return false;

	  }else  {
		 bootbox.alert('Please add products.', function () {
                        $('#add_item').focus();
                    });
	  }  
}




function add_items(){	        
        
		var check_warehose=document.getElementById("warehouse_id").value;		
        $("#add_item").autocomplete(
		{
            source: '<?php echo base_url();?>customer_order_process/get_product_suggestion?t='+$("#in_type").val(),
            minLength: 1,
            autoFocus: false,
            delay: 5,
            response: function (event, ui) {			
				if (ui.content.length == 1 && ui.content[0].product_id != 0)
				{					
				}else if(ui.content.length ==0){
					var noResult = { value:"",label:"No matching result found! Product might be out of stock in the selected warehouse." };
            		ui.content.push(noResult);
				}else {
				}			
            },
            select: function (event, data) {
				//alert( "You selected: " + data.item.product_id );
				if(data.item.value){					
					//check is added prodcut
					rowCount=parseInt($('#rowCount').val());
					var tmp='';
					if(rowCount){
	  					tmp=isAddedProduct(data.item.product_id);
					}
	   				if(!tmp){
				addProductToListByID(rowcount,data.item.product_id,data.item.product_name,data.item.product_code,data.item.product_price,data.item.product_part_no,data.item.product_oem_part_number,data.item.item_cost,data.item.qty);
				rowcount=rowcount+1;
				$( '#in_type option:not(:selected)' ).prop( 'disabled', true );
				//$('#sales_rep_id option:not(:selected)').prop('disabled', true);
				//$('#warehouse_id option:not(:selected)').prop('disabled', true);				
					}else {
						calculateTotal();
					}				
				$("#add_item").val('');
					 return false;
				}
				$(this).autocomplete('close');
                    $(this).removeClass('ui-autocomplete-loading');				
            }
        });	
}		
		
		
$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
      
	   $('#bottom-total').css({
        position: 'static',
		width: '100%'
    });
   }else {
	    $('#bottom-total').css({
        position: 'fixed',
		width: '1082px'
    });
   }
});

//function to initiate bootstrap-datepicker
$(function () {
	var currentDate = new Date();	
	$('#sales_rep_issue_datetime').datetimepicker({
		defaultDate: currentDate,
		format:"YYYY-MM-DD"		
		});	
	$("#warehouse_id").select2();	
	$("#route_id").select2();	
	$("#sales_rep_id").select2();
	$("#customer_id").select2();
	$("#in_type").select2();	
});	

$(document).ready(function() {	
	 $('#in_type').focus();	 
	 calculateTotal();
$(".noEnterSubmit").on('keyup', function (e) {   
});

  $(window).keydown(function(event){
	 $(":focus").each(function() {
    
	focus_id=this.id;
	
	  var rowCount=$('#rowCount').val();
	  var fld='quantity_'+rowCount;
	  if(focus_id==fld){
		if(event.keyCode == 16) {
			  $('#add_item').focus();
		}
	  }
    if(event.keyCode == 18) {
		calculateTotal();
		
	setTimeout(function(){
  
	  $("#create_sales_form").submit()
    //do something special
  }, 1000);
    }		  
    if(event.keyCode == 13) {
      event.preventDefault();
     return false;
    }
  });
  
  });
});





window.onload = function () {
    if (! localStorage.justOnce) {
        localStorage.setItem("justOnce", "true");
        window.location.reload();
    }
}

function load_customer_list(loading_id) {
					var x = document.getElementById( "customer_id" );
					x.remove( x.selectedIndex );
					$.post( "<?php echo  base_url(); ?>sales/get_list_customer_by_area_id?area_id=" +loading_id, function ( data ) {
						var sel = $( "#customer_id" );
						sel.empty();
						$( "#customer_id" ).append( '<option value="" >-SELECT-</option>' );
						$.each( data, function ( i, item ) {
							$( "#customer_id" ).append( '<option value="' + i + '">' + item + '</option>' );
						})
					}, "json" );
}



</script>
</body>
<!-- end: BODY -->
</html>