<?php
	include('common/header.php');	
?>
	<div class="">
		<div class="col-xm-12">
			<div class="widget-box light-border">
				
				<div class="widget-body">
					<div class="widget-main">
						<div class="row">
							<div class="col-sm-12" style="float: none">

								<?php $form_data=array('name'=>'dispense_manage','id'=>'dispense_manage','class'=>'form-horizontal','autocomplete'=>'off','onsubmit' => 'return validate_dispenser_details()','enctype'=>'multipart/form-data'); 
								echo form_open($form_action,$form_data); ?>	
								
 								 <section class="panel panel-default">
                 				 <header class="panel-heading bg-info lt font-bold">Dispenser Details</header>
                  <!-- panel body start -->
                  <div class="panel-body bs-example form-horizontal">
                    <div class="">
                     <div class="hr hr12 dotted"></div>
                       <div class="">
                        <!-- main col-sm-12 -->
                      <div class="col-sm-12" > 
                         <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Customer Name:</label>
                              <div class="col-sm-7">
                                 <label><?php echo $customer_name;?></label>              
                              </div>	
                            </div>
                          </div>

                           <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Sales Person Name:</label>
                              <div class="col-sm-7">
                                <label><?php echo $first_name;?> <?php echo $last_name;?></label>             
                              </div>	
                            </div>
                          </div>

                           <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Product Category:</label>
                              <div class="col-sm-7">
                               <label><?php echo $product_category_name;?></label>            
                              </div>	
                            </div>
                          </div>

                          <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Dispenser Name:</label>
                              <div class="col-sm-7">
                               <label><?php echo $product_name;?></label>            
                              </div>	
                            </div>
                          </div>

                          <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Dispenser Quantity:</label>
                              <div class="col-sm-7">
                               <label><?php echo $dispenser_quantity;?></label>            
                              </div>	
                            </div>
                          </div>

                          <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Sales Type:</label>
                              <div class="col-sm-7">
                               <label><?php echo $sales_type;?></label>            
                              </div>	
                            </div>
                          </div>

                           <div class="col-sm-6">
                           <div class="form-group">
                            <label for="client_code" class="col-sm-5 control-label required">Installation/Sale Date:</label>
                              <div class="col-sm-7">
                               <label><?php echo get_date_dmy_format_wo_time($installation_date);?></label>            
                              </div>	
                            </div>
                          </div>

                         
                       
                            
                         <!-- </div> -->
                         <!-- main col-sm-12  close-->
                       </div> <!-- blank close -->
        </div><!-- blank close -->
      </div>   <!-- panel body close -->
    </section> <!-- section close -->
     <section class="panel panel-default">
                 				 <header class="panel-heading bg-info lt font-bold">Refill Details</header>
                                    <div class="table-responsive">
									<table id="simple-table" class="table  table-bordered table-hover">
											<thead>
												<tr>
													<th>Sr No</th>
													<th>Select Tracking Numbers</th>
													<th>Refill Name</th>
													<th>Programming Duration Days</th>
													<th>Quantity</th>
													<th>Refill Date</th>
													<th>Refill Location</th>
													<th>Comments</th>
													<!-- <th>Return Dispenser</th> -->
													<th id="action">Action</th>
													
													
												</tr>
											</thead>

											<tbody><div class="form-holder-append">
										     <td>1</td>
										      <td> 
											 
													<?php
													
														/* $i=1;
														 for($i; $i <= $stock_quantity; $i++) {
															
															 if($i<=9){
																$tracking_no =$product_prefix.'_00000'.$i;
															}else if($i>9 && $i<=99){
																$tracking_no =$product_prefix.'_0000'.$i;
															}else if($i>99 && $i<=999){
																$tracking_no =$product_prefix.'_000'.$i;
															}else if($i>999 && $i<=9999){
																$tracking_no =$product_prefix.'_00'.$i;
															}else if($i>9999 && $i<=99999){
																$tracking_no =$product_prefix.'_0'.$i;
															}else if($i>99999){
																$tracking_no =$product_prefix.'_'.$i;
															}															 
															 $option .="<option value='".$tracking_no."'";													
															 $option .=' >'.$tracking_no.'</option>';
														 }
															echo $option; */
															
															
														// $tracking_array_data=$tracking_array;
														// $js1 = ' style="width:218px;" id="tracking_no_1" class="form-control" multiple="multiple"';
														// echo $tracking_select=form_dropdown('tracking_no[1][]', $tracking_array_data, $tracking_numbers_id, $js1);
															/*
															Shreyas Vispute 7 August 2019
															Client need autosuggestion
															*/
														$tracking_array_data=$tracking_array;
														$js1 = ' style="width:218px;" id="tracking_no_1" class="js-example-basic-multiple"  multiple="multiple"';
														echo $tracking_select=form_dropdown('tracking_no[1][]', $tracking_array_data, $tracking_numbers_id, $js1);
												?>
												
												<br><span class="red" id="tracking_no_1_error"></span>
                                              </td>
                                             <td>                                         	
												 <?php
												 
													$refill_array_data=$refill_array;
													$js1 = 'id="refill_product_id_1" class="form-control"';
													echo $refill_select=form_dropdown('refill_product_id[1]', $refill_array_data, $refill_product_id, $js1);
												?>
												<br><span class="red" id="refill_product_id_1_error"></span>
                                             </td>
                                             <td>
                                              	
											    <select  style="width:218px;" name="programming_days[1]" id="programming_days_1" >
													<option value="">Select Programming Duration Days</option>
													<option value="7">7</option>
													<option value="10">10</option>
												    <option value="15">15</option>
													<option value="30">30</option>
													<option value="45">45</option>
													<option value="60">60</option>
												</select>
												<br><span class="red" id="programming_days_1_error"></span>
                                             </td>

                                             <td>
                                             	<input type='text' name='refill_quantity[1]' id="refill_quantity_1"  onkeyup = "Validate_number(this)" placeholder='' style="width: 63px;" maxlength="3">
												<br><span class="red" id="refill_quantity_1_error"></span>
												<span class='red' id='refill_quantity_1_error1'></span></td>
                                             </td>

                                             <td>
                                               
                                             	<div class="input-group" style="width:183px;">
                                                    <input name="refill_date[1]" id="refill_date_1" type="text" class="form-control" />
                                                </div>
												<br><span class="red" id="refill_date_1_error"></span>
                                             </td>

                                              <td>
                                             	<div class="form-group">  
															<div class="col-sm-7 control-label" for="form-field-2">	
																<?php	
																	$admin_type_name_data=array('name'=>'refill_location[1]',
																		'id'=>'refill_location_1',
																		'placeholder'=>'Enter Refill Location',
																		'value'=> '',

																		);
																echo form_input($admin_type_name_data,'');
																echo form_error('admin_type_name');
											                    ?>
										                   </div>
									            </div>
                                             </td>


                                              <td>
                                             	<div class="form-group">  
														<div class="col-sm-7 control-label" for="form-field-2">	
															<?php	
																$admin_type_comment_data=array('name'=>'refill_comments[1]',
																		'id'=>'refill_comments_1',
																		'placeholder'=>'Enter comments',
																		'value'=> '',

																		);
																echo form_input($admin_type_comment_data,'');
																echo form_error('admin_type_comment_data');
											                    ?>
										                   </div>
									            </div>
                                             </td>
                                             <td></td>
											

											</tbody>   
                                             
                                    </table>
                                 </div>
                              </section>
									<input type="hidden" name="details_no" id="details_no" value="1">
									<input type="hidden" name="dispenser_sales_id" id="dispenser_sales_id" value="<?php echo $dispenser_sales_id;?>">
									<input type="hidden" name="customer_id" id="customer_id" value="<?php echo $customer_id;?>">
									<input type="hidden" name="company_branch_id" id="company_branch_id" value="<?php echo $company_branch_id;?>">
									<input type="hidden" name="sales_type" id="sales_type" value="<?php echo $sales_type;?>">
											 </div>	
									<input type="hidden" name="checking_quantity" value="<?php echo $dispenser_quantity; ?>" id="checking_quantity">

									<input type="hidden" name="temp_dispenser_sale_id" value="<?php echo $temp_dispenser_sale_id; ?>"> 							                                  							      
								<div class="col-md-offset-12 col-md-12" style="float: right;">
									    <button class="btn btn-sm btn-primary" type="button" id="addrefill" style="float: right;" >
										<span class="glyphicon glyphicon-plus"></span>Add Refill 
									    </button>	
							        </div>
							       
         		               
								</div> <!-- col 12 -->
								<div class="col-md-offset-5 col-md-5">
									<button class="btn btn-info" type="button" onclick="javascript:window.location='<?php echo base_url();?>index.php/gc/dispenser_sale_data/manage_dispenser_sale_data'">
										Back
									</button>
									
									<button type="submit" name="btn_confirm" value="btn_confirm" class="btn btn-info left-icon mr-10">Submit</button>
		</div>
								
							</div><!-- row -->
						</div><!-- widget main -->
					</div> <!-- widget body -->	
				</div><!-- widget light box -->
				
                              
			</div> <!-- main col 12 close -->
 
                                 <!-- </div> -->
                                  
		</div> <!-- blank -->
		<!-- /.col -->
	<!-- </div> -->
	<div class="clearfix form-actions">
		
	</div>	
	<div class="space-4"></div>
		<!-- /.page-row -->
        <!-- /.page-content --> 
    </div>
    <!-- /.main-content --> 
	</div>
    <!-- /#ace-settings-container --> 
	</div>
	<!-- /.main-container-inner --> 


<?php

	include('common/footer.php');

?>
<script>
function hide()
{
	document.getElementById("admin_type_code").style.display="none";
}
function show()
{
	document.getElementById("admin_type_code").style.display="";
}
</script>
<script type="text/javascript">
$(document).ready(function() {
    $('form:first *:input[type!=hidden]:first').focus();
});
</script>	
<script type="text/javascript">
$('#refill_date_1').datepicker({dateFormat: 'dd-mm-yy'});
</script>

<script type="text/javascript">
	  $(document).ready(function(){
	  	 // $("#simple-table").hide();
	  	  $("#action").hide();

	  	  });
</script>
<?php

$form = '';
foreach ($refill_array_data as $key => $val)
{
	//$sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
	//$form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>";
	$form .= '<option value="'.$key.'">'.(string) $val."</option>";
}

$tracking_form = '';
foreach ($tracking_array_data as $key1 => $val1)
{
	//$sel1 = (in_array($key1, $selected)) ? ' selected="selected"' : '';
	$tracking_form .= '<option value="'.$key1.'">'.(string) $val1."</option>";
}

	
?>
<script>
function Validate_number(txt) 
{
     txt.value = txt.value.replace(/[^0-9\n\r]+/g, '');
}

	$(document).ready(function(){
		$('.js-example-basic-multiple').select2();
        $("#addrefill").click(function(){
        	$("#simple-table").show();
			
			 $("#action").show();
			 var details_no=$("#details_no").val();
			
            var details_no= parseInt(details_no)+1 ;


            var srno="<td>"+ details_no +"</td>";

            var refill_name = '<td><select name="refill_product_id['+details_no+']" id="refill_product_id_'+details_no+'"  class="form-control"><?php echo $form;	?></select><br><span class="red" id="refill_product_id_'+details_no+'_error"></span></td>'; 

            var programming_days = "<td><select name='programming_days["+details_no+"]' id='programming_days_"+details_no+"'><option value=''>Select Programming Duration Days</option><option value='7'>7</option><option value='10'>10</option><option value='15'>15</option><option value='30'>30</option><option value='45'>45</option><option value='60'>60</option></select><br><span class='red' id='programming_days_"+details_no+"_error'></span></td>"; 

           																							
            var refill_quantity ="<td><input type='text' name='refill_quantity["+details_no+"]' id='refill_quantity_"+details_no+"' style='width: 63px;' maxlength='3'><br><span class='red' id='refill_quantity_"+details_no+"_error'></span><span class='red' id='refill_quantity_"+details_no+"_error1'></span></td>"; 

            var refill_duration = '<td><div class="input-group"style="width:183px;"><input id="refill_date_'+details_no+'"  name="refill_date['+details_no+']" type="text" class="form-control"></div><br><span class="red" id="refill_date_'+details_no+'_error"></td>'; 

        	var location ="<td><input type='text' name='refill_location["+details_no+"]' id='refill_location_"+details_no+"' placeholder='Enter Refill Location'><br><span class='red' id='refill_location_"+details_no+"_error'></span></td>"; 
                                                                                                                                                                                              
            var tracking_no = '<td><select name="tracking_no['+details_no+'][]" id="tracking_no_'+details_no+'" style="width:218px;" class="js-example-basic-multiple" multiple="multiple"><?php echo $tracking_form;?></select><br><span class="red" id="tracking_no_'+details_no+'_error"></span><span class="red" id="tracking_number_'+details_no+'_error"></span></td>';

            var refill_comment  ="<td><input type='text' name='refill_comments["+details_no+"]' id='refill_comments_"+details_no+"' placeholder=''><br><span class='red' id='refill_comments_"+details_no+"_error'></span></td>"; 

            
            // var comments = "<td><div class='col-sm-7'><div class='form-group'><textarea class='form-control' id='comment' style='width:234px; height:45px;'></textarea></div></div></td>";																		
								
            // var markup ="<tr><td><input type='checkbox' name='record'></td>" + Dispenser_type + dispenser_quantity + refill_name + refill_quantity + refill_expiry + "</tr>";
            var markup ="<tr></td>" + srno + tracking_no +refill_name + programming_days + refill_quantity + refill_duration + location + refill_comment + "<td class='center'><label class='pos-rel'><button type='button'  class='btn btn-danger' name='record' onclick='SomeDeleteRowFunction(this)'>Delete</button></label</tr>";

            $("table tbody").append(markup);

            $('#refill_date_'+details_no+'').datepicker({dateFormat: 'dd-mm-yy'});
			$("#details_no").val(details_no);
			$('.js-example-basic-multiple').select2();
        }); 
    });    
</script>
<script>
function checkIfArrayIsUnique(myArray) 
    {
        for (var i = 0; i < myArray.length; i++) 
        {
            for (var j = i+1; j < myArray.length; j++) 
            {                  
                    if (myArray[i] == myArray[j]) 
                    {
                        return true; // means there are duplicate values
                    }

            }
        }
        return false; // means there are no duplicate values.
    }
function validate_dispenser_details()
{

	var error=false;
	var details_no_cnt=$('#details_no').val();
	var sales_type=$('#sales_type').val();
	var tracking_no1 = new Array(); 
	var newlist = new Array(); 
	var checking_quantity =$('#checking_quantity').val();
	var sales_type=$('#sales_type').val();
    var quantity_add=0;
 	var refill_quantity_1=$('#refill_quantity_1').val();
    
 	for (var k = 1; k < 100;k++) {
    var refill_quantity=$('#refill_quantity_'+k).val();
    var quantity_add=parseInt(refill_quantity)+quantity_add;
    if (quantity_add > checking_quantity) {

     $("#refill_quantity_"+k+"_error1").html("Please Enter Quantity With in Dispenser Quantity");
	  error=true;
    	// /return false;
    	//
    }
    else{
    	if(document.getElementById("refill_quantity_"+k+"_error1"))
			{
				document.getElementById("refill_quantity_"+k+"_error1").innerHTML="";
			}	
    	}
 	}

   
	for(i=1;i<=details_no_cnt;i++)
	{
		
		var tracking_no=$("#tracking_no_"+i).val();	
		var tracking_numbers=$("#tracking_no_"+i).val();
		
		console.log(tracking_no1);
		var newlist = tracking_no1.concat( tracking_numbers );
		
		var tracking_no1=newlist;
		
		if(sales_type=="FTU")
		{
			
			if(tracking_no=="" || tracking_no==null)
			{
				$("#tracking_no_"+i+"_error").html("Please Select Tracking Number");
				error=true;	
			}
			else
			{
				if(document.getElementById("tracking_no_"+i+"_error"))
				{
					document.getElementById("tracking_no_"+i+"_error").innerHTML="";
				}	
			}
		}
		
		
		var refill_product_id=$("#refill_product_id_"+i).val();	
		if(refill_product_id=="" || refill_product_id==null)
		{
			$("#refill_product_id_"+i+"_error").html("Please Select Refill Name");
			error=true;	
		}
		else
		{
			if(document.getElementById("refill_product_id_"+i+"_error"))
			{
				document.getElementById("refill_product_id_"+i+"_error").innerHTML="";
			}	
		}
		
		var programming_days=$("#programming_days_"+i).val();	
		
		if(programming_days=="" || programming_days==null)
		{
			$("#programming_days_"+i+"_error").html("Please Select Programming Days");
			error=true;	
		}
		else
		{
			if(document.getElementById("programming_days_"+i+"_error"))
			{
				document.getElementById("programming_days_"+i+"_error").innerHTML="";
			}	
		}
		
		var refill_quantity=$("#refill_quantity_"+i).val();	
		if(refill_quantity=="" || refill_quantity==null)
		{
			$("#refill_quantity_"+i+"_error").html("Please Enter Quantity");
			error=true;	
		}
		else
		{
			if(document.getElementById("refill_quantity_"+i+"_error"))
			{
				document.getElementById("refill_quantity_"+i+"_error").innerHTML="";
			}	
		}
		
		var next_refill_date=$("#refill_date_"+i).val();	
		if(next_refill_date=="" || next_refill_date==null)
		{
			$("#refill_date_"+i+"_error").html("Please Enter Refill Date");
			error=true;	
		}
		else
		{
			if(document.getElementById("refill_date_"+i+"_error"))
			{
				document.getElementById("refill_date_"+i+"_error").innerHTML="";
			}	
		}
		
	}
	
	if(sales_type=="FTU")
	{
		if (newlist.length>1) 
		{
			var is_dupliate=checkIfArrayIsUnique(newlist);
			console.log(is_dupliate);
			if(is_dupliate==true)
			{
				$("#tracking_no_1_error").html("Please Select Unique Tracking Number");
				error=true;	
			}
			else
			{;
				$("#tracking_no_1_error").html("");
				
			}
		}
	}
	
	
	if(error==true)
	{
		return false;	
	}
	else
	{
		return true;
	}
}

/* var itemIndex = 2;
var x = 22;
$("#addrefill").click(function (e) { //on add input button click
    e.preventDefault();
    
        x++; //text box increment
          $("table tbody").append('<div class="clonedInput"><input id="data_item_'+ itemIndex++ +'"  type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
    
}); */

	function SomeDeleteRowFunction(btndel) {
    if (typeof(btndel) == "object") {
        $(btndel).closest("tr").remove();
    } else {
        return false;
    }
}
</script>
<script>
$(document).ready(function() {
    $('.js-example-basic-multiple').select2();
});
</script>
