<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="" 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="" 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>
set_old_sn();

function set_old_sn(){
	var sn_fld_name='#product_sn_'+'<?php echo $row;?>';
	var old_sn_details=$(sn_fld_name).val()
	var result=old_sn_details.split(',');
	for(var k=0; k<result.length; k++){
		var cb_fld_name="#cb_"+result[k];
		$(cb_fld_name).prop('checked', true);
	}
}


function getSerialnumbers(){
	var val=[];	
	$(':checkbox:checked').each(function(i){
		if((i+1)<= <?php echo $qty;?>){
			val[i] = $(this).val();
			var tmp_sn=val[i];
		}
		else{
			bootbox.alert("You can't add serial no more than quantity");	
			$(this).prop('checked', false);
		}
	});
}
       


		function add_serial_no(){
			
			
			
			
			setTimeout(
  function() 
  {
	var error=false;
	var error2=false;
	var sn_text='';
	//var txt_array
	var i=0;
	 $(':checkbox:checked').each(function(i){
		 i++;
		 var val= $(this).val();
		 if(i!=1){
			sn_text=sn_text+',';
		 }
		 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;
			}
			
			
			if(!error){
			var row=$('#fld_name').val();
			var sn_fld_name='#product_sn_'+row;
			
			//alert(sn_fld_name);
			$(sn_fld_name).val(sn_text)
			$('div#ajax-modal').modal('hide');
			}else {
				if(error2){
					alert('Serial numbers repeat');	
				}else {
					alert('Please add serial numbers');	
				}
			}
  }, 10);
			
			
		}
		
		
		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);
	});
	
}
		</script>          
