<style type="text/css">
.serial_no_tbl td{
	padding:2px;
	
	
}
.sn_wap{
border: 1px solid #ccc;
height: 200px;
overflow-y: auto;
padding: 3px;

}
textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"] {
  background-color: #ffffff;
  border: 1px solid #d5d5d5;
  border-radius: 0 !important;
  box-shadow: none;
  color: #858585;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.2;
  padding: 1px 4px;
  transition-duration: 0.1s;
}
</style>



<?php 
$config = array('role' =>'form', 'class'=>'form-horizontal','id'=>'create_category_form', 'name'=>'create_category_form');
echo form_open_multipart(base_url("#"),$config);
?>
<?php //print_r($category_details) ?>
            <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Select Serial Numbers</h4>
            <p>ITEM : <?php echo $product_dis_name ?></p>
            
           
            Serial Number <input value="" id="add_sn_fld" class="" name="add_sn_fld">
           
            </div>
            <div class="modal-body">
                <div id="error"></div>
            <div class="row form-group">
 <div class="col-md-12">
 <!--<h4 class="modal-title">Serial Numbers</h4>-->
 <div class="sn_wap">           
 <table width="100%" border="0" class="serial_no_tbl">
 <?php //echo 'qty:'.$qty ?>
 <?php //echo 'test'; ?>
 <?php
 $i=0;
 $sn_count=0;
 $mod_purchase=new Purchases_Model();
 //print_r($sn_list[0]);
  foreach ($sn_list as $row_1){  
  if($row_1->pis_number){
	  
	  //check issued and not sold serial number
	 $avalable=$mod_purchase->check_avalable_sn_for_sale($product_id,$row_1->pis_number);
$avalable=1;
if($avalable){	  
  $i++;
  $sn_count++;
  
  ?>
  <tr>
    <td style=" width:30px"><?php echo sprintf('%02d', $i); ?>. </td>
    <td><input name="sn[]" class="ckeck_box_c" type="checkbox" value="<?php echo $row_1->pis_number;?>" id="cb_<?php echo $row_1->pis_number ?>" onchange="getSerialnumbers();"/> &nbsp <label for="cb_<?php echo $row_1->pis_number ?>"><?php echo $row_1->pis_number;?></label></td>
  </tr>
  <?php 
  }
}
  }?>
  <input type="hidden"  value="" name="snl[]" id="snl_item" onchange="">
</table>
</div>
  </div>             
  <hr/>          
            
             <div class="col-md-12" style="display:none">
             <br/>
   <h4 class="modal-title">Selected Serial Numbers</h4>     
    <div class="sn_wap">          
 <table width="100%" border="0" class="serial_no_tbl" id="sn_selected_tbl">
 <?php //echo 'qty:'.$qty ?>
 <?php for($i=1; $i<($qty+1); $i++){ ?>
  <tr>
    <td style=" width:30px"><?php echo sprintf('%02d', $i); ?>. </td>
    <td><input type="text" class="" value="" name="sn[]" id="sn_item_<?php echo $i;?>" onchange="check_serial_no_valid(this.value,'<?php echo $i;?>')"></td>
  </tr>
  <?php }?>
</table>
</div>
                   
             
                   
            
            </div>
            
                       
            
            </div>
            </div>

 <div class="modal-footer">
            <input type="button" onclick="add_serial_no()" name="add_category" value="<?php echo $btnText; ?>" class="btn btn-primary">
            </div>
  </form>
  <input name="" type="hidden" id="fld_name" value="<?php echo $row;?>"/>
   <input name="" type="hidden" id="alredy_added_count" value="0"/>
  
<?php
$procut_cost_val_avalable=0;
$new_mod_com=new Common_Model();
$procut_cost_val_avalable=$new_mod_com->check_option_valable_by_setting_id(1);
?>  
 
  
<!-- <div onclick="test()">test</div>-->
  

  
  

		<!-- start: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY VALIDATION-->
		
		<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY  VALIDATION-->
 
<script>
function getSerialnumbers(){
var val=[];	

$('.ckeck_box_c:checked').each(function(i){
if((i+1)<= <?php echo $qty;?>){
val[i] = $(this).val();
var tmp_sn=val[i];

//alert(<?php //echo check_option_valable_by_setting_id(1);?>);

//Start check serial no cost, Product cost greater than selling price
//alert('<?php echo $each_price;?>');

$.post( "<?php echo base_url();?>sales/get_product_cost_by_serial_no_id", {pis_number:tmp_sn})
.done(function( data ) {
	var obj = $.parseJSON(data);
	//displayNotice('Issued','Successfully Updated');
	//alert(obj.pis_cost_price);
	var prouct_price=parseFloat('<?php echo $each_price;?>');
	
	
	//alert(obj.pis_cost_price);
	//alert(prouct_price);
	if('<?php echo $procut_cost_val_avalable;?>'){
	//if (obj.pis_cost_price>prouct_price) 
	{
		if('sale'==<?php echo $page;?>){
		bootbox.alert(tmp_sn+" Emi no product cost greater than selling price. Can not add to the invoicce. Please contact site administrator");
		}
		var fld="#cb_"+tmp_sn;
		
		//$(fld).prop('checked', false);
		
		
		
	}
	//else
	{
		
		$(this).is(':checked');
		$("#snl_item").val(val);	
	}
	
	//set cost value if avalable grn item serial
	var fld_item_cost="#item_cost_"+'<?php echo $row;?>';
	
	//alert(obj.pis_cost_price);
	if(obj.pis_cost_price){
		$(fld_item_cost).val(obj.pis_cost_price);
	}
	
	}else{
		
		
	}
	
	
	//set array
	//
	$(this).is(':checked');
	$("#snl_item").val(val);
	
	
	
	
	
	
	
	
	
	//	setTimeout(location.reload.bind(location), 500);
});
//alert('2');

//End check serial no cost, Product cost greater than selling price
error=true;

}
else
{
bootbox.alert("You can't add serial no more than quantity");	
$(this).prop('checked', false);
}
});


}

function set_cur_point(){
	$('#add_sn_fld').trigger('focus');
	//$("#add_sn_fld").focus('');
	//alert();
}
  
  set_alredy_added_count();
 
function set_alredy_added_count(){
	var sn_fld_name='#product_sn_'+'<?php echo $row;?>';
		var old_sn_details=$(sn_fld_name).val()
		var result=old_sn_details.split(',');
		var alredy_added_count=result.length; 
		if(alredy_added_count==1) alredy_added_count=0;
		$("#alredy_added_count").val(alredy_added_count);
		//alert(alredy_added_count);
}

$(window).load(function(){
  // alert();
})

$('#add_sn_fld').on('keypress', function(e) {
	
	var add_sn_fld=this.value;
	
    var code = e.keyCode || e.which;
    if(code==13){
		//alert('press enter');
		var sn_fld_name='#product_sn_'+'<?php echo $row;?>';
		var old_sn_details=$(sn_fld_name).val()
		var result=old_sn_details.split(',');
		//var alredy_added_count=result.length;
		var alredy_added_count=parseInt($("#alredy_added_count").val());
		var nxt_item=alredy_added_count+1;
		
		
		//check alredy login
		//var error=check_serial_no_valid(add_sn_fld,'');
		
		
		var sm_item_fld="#sn_item_"+nxt_item;
		
		$(sm_item_fld).val(add_sn_fld);
		$("#alredy_added_count").val(nxt_item);
		
		//check select box
		var cb_fld_name="#cb_"+add_sn_fld;
		$(cb_fld_name).prop('checked', true);
		
		//set array value
		$("#snl_item").val(add_sn_fld);
		
		
		
		
		$("#add_sn_fld").val('');
		//alert();
		//$('#add_sn_fld').trigger('focus');
		
		
		setTimeout(
  function() 
  {
    //do something special
	//alert();
	set_cur_point();
	
  }, 500);
  
		return false;

/*       $('#sn_selected_tbl > tbody:last-child').append(
            '<tr>'// need to change closing tag to an opening `<tr>` tag.
            +'<td>'+nxt_item+'.</td>'
            +'<td><input class="" value="'+add_sn_fld+'" name="sn[]" id="sn_item_1" onchange="check_serial_no_valid(this.value,"1")" type="text"></td>'
            +'</tr>');*/
    }
});

//jQuery(window).load(function() {
var h=<?php echo $sn_count;?>;
for(var x=1; x<=h; x++){
	//alert(x);
}
//});
set_old_sn();
function set_old_sn(){
	var sn_fld_name='#product_sn_'+'<?php echo $row;?>';
	var old_sn_details=$(sn_fld_name).val();
	//alert(old_sn_details);
	//set old added
	$('#snl_item').val(old_sn_details);
	
	var result=old_sn_details.split(',');
	for(var k=0; k<result.length; k++){
	var fld_name='#sn_item_'+(k+1);
		$(fld_name).val(result[k])
		var cb_fld_name="#cb_"+result[k];
		$(cb_fld_name).prop('checked', true);
	}
}
//alert(result[0]);
//alert(result[1]);
//alert(result[2]);


function check_serial_no_valid(sn,id){
	//alert(sn);
	var product_id='';
	var row='<?php echo $this->uri->segment(3);?>';
	var product_id_fld="#product_id_"+row;
	product_id=$(product_id_fld).val();
	//alert(product_id);
	if(sn){
	var url="<?php echo base_url();?>sales/check_serial_no_valid/"+sn+"/"+product_id;
		$.post(url)
	  .done(function( data ) {
		  var obj = jQuery.parseJSON(data);
		  if(!obj.status){
		  var fld='#sn_item_'+id;
		  $(fld).val('');
		  alert('Invalid Serial No.:'+sn);
		  return true;
		  }
	  });
	}
	
}

</script>
                       
		<script>
		
$('input').on("keypress", function(e) {
	
            /* ENTER PRESSED*/
            if (e.keyCode == 13) {
				//alert();
                /* FOCUS ELEMENT */
                var inputs = $(this).parents("form").eq(0).find(":input");
                var idx = inputs.index(this);

                if (idx == inputs.length - 1) {
                    inputs[0].select()
                } else {
                    inputs[idx + 1].focus(); //  handles submit buttons
                    inputs[idx + 1].select();
                }
                return false;
            }
        });		

		
		
function add_serial_no(){
	//alert('add_serial_no');
	
	var check_box_val_list='';		
	$(':checkbox:checked').each(function(i){
		
		var val = $(this).val();
		
	//	alert(val);
		
		if(check_box_val_list) check_box_val_list=check_box_val_list+',';
		check_box_val_list=check_box_val_list+val;
		//alert(val);
	 });
	 
	 
	// alert(check_box_val_list);	
			
			
			
	setTimeout(
  function() 
  {
    //do something special
	var error=false;
	var error2=false;
			var sn_text='';
			//var txt_array
			
			
			var i=0;
		//	$('input[name^="sn"]').each(function() {
			//	i++;
				
				//alert($(this).val());
				
			//	if(i!=1){
				//	sn_text=sn_text+',';
			//	}
    			//alert($(this).val());
			//	if($(this).val()==''){
				//	error=true;
			//	}
			//	sn_text=sn_text+$(this).val();
		//	});

//alert(sn_text);

			
			//$('#999999').val(111);
			//alert($('#sn_999999').val());
			
			
			//check_allredy_added_this_invoice
			
			
			if(check_allredy_added_this_invoice()){
				error2=true;
				error=true;
			}
			
		//	alert(error);
			
			if(!error){
			var row=$('#fld_name').val();
			var sn_fld_name='#product_sn_'+row;
			
			//alert(sn_fld_name);
			
		//	alert(check_box_val_list);
			$(sn_fld_name).val(check_box_val_list);
			
			//calculate  totamount
			//alert(1);
			calculateTotal();
			//alert(2);
			
			
			$('div#ajax-modal').modal('hide');
			jQuery('.close').click();
			//alert(3);
			}else {
				if(error2){
					alert('Serial numbers repeat');	
				}else {
					alert('Please add serial numbers');	
				}
			}
  }, 1000);
			
			
		}
		
function check_allredy_added_this_invoice(){
	var sn_array = new Array();
	$('input[name^="sn"]').each(function() {
		var snthis=$(this).val();
		
    	
			//alert('does not exist');
		
		//if(sn_array.indexOf(snthis)==-1){
			sn_array.push(snthis);
			
			
		//}
		
		//alert(sn_array);
	});
	
}
		
			jQuery(document).ready(function() {
				FormValidator.init();
			});


       

		</script>          
