<?php $__env->startSection('content'); ?>

  <div class="page-content row">
    <!-- Page header -->
    <div class="page-header">
      <div class="page-title">
        <h3> <?php echo e($pageTitle); ?> <small><?php echo e($pageNote); ?></small></h3>
      </div>
      <ul class="breadcrumb">
        <li><a href="<?php echo e(URL::to('dashboard')); ?>"><?php echo e(Lang::get('core.home')); ?></a></li>
		<li><a href="<?php echo e(URL::to('materialoutwards?return='.$return)); ?>"><?php echo e($pageTitle); ?></a></li>
        <li class="active"><?php echo e(Lang::get('core.addedit')); ?> </li>
      </ul>
	  	  
    </div>
 
 	<div class="page-content-wrapper">

		<ul class="parsley-error-list">
			<?php foreach($errors->all() as $error): ?>
				<li><?php echo e($error); ?></li>
			<?php endforeach; ?>
		</ul>
<div class="sbox animated fadeInRight">
	<div class="sbox-title"> <h4> <i class="fa fa-table"></i> </h4></div>
	<div class="sbox-content"> 	

		 <?php echo Form::open(array('url'=>'archivematerialoutwards/save?return='.$return, 'class'=>'form-horizontal','files' => true , 'parsley-validate'=>'','novalidate'=>' ')); ?>

<div class="col-md-12">
						<fieldset><legend> Material Outwards</legend>
									
								  <div class="form-group hidethis " style="display:none;">
									<label for="Id" class=" control-label col-md-4 text-left"> Id </label>
									<div class="col-md-6">
									  <?php echo Form::text('id', $row['id'],array('class'=>'form-control', 'placeholder'=>'',   )); ?> 
									 </div> 
									 <div class="col-md-2">
									 	
									 </div>
								  </div> 					
								  <div class="form-group  " >
									<label for="Date" class=" control-label col-md-4 text-left"> Date </label>
									
									<label ><a href="javascript:void(0);" class="printfile" title="Print"><i class="fa fa-download"></i>&nbsp;Print </a></label>

									<div class="col-md-6">
									<?php 
									if(!empty($row['date']))
									{
										$date1 = DateTime::createFromFormat('Y-m-d', $row['date']);

$sitedate = $date1->format('d-m-Y');
									}
									else{
$sitedate = date('d-m-Y');
									}
									?>
									  
				<div class="input-group m-b" style="width:150px !important;">
					<?php echo Form::text('date', $sitedate,array('class'=>'form-control date','id'=>'date')); ?>

					<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
				</div> 
									 </div> 
									 <div class="col-md-2">
									</div>
								  </div> 	


								  <div class="form-group  " >
									<label for="Site" class=" control-label col-md-4 text-left"> Site </label>
									<div class="col-md-6">
									  <select name='site' rows='5' id='site' class='select2 '   ></select> 
									 </div> 
									 <div class="col-md-2">
									 	
									 </div>
								  </div> 					
								   					
								  <div class="form-group  " >
									<label for="Remarks" class=" control-label col-md-4 text-left"> Remarks </label>
									<div class="col-md-6">
									  <?php echo Form::text('remarks', $row['remarks'],array('class'=>'form-control', 'placeholder'=>'', 'id'=>'remarks'  )); ?> 
									 </div> 
									 <div class="col-md-2">
									 	
									 </div>
								  </div>
								  
								  <div class="form-group  " >
									<label for="Amount" class=" control-label col-md-4 text-left"> Amount </label>
									<div class="col-md-6">
									  <?php echo Form::text('tot_amount', $row['amount'],array('class'=>'form-control', 'placeholder'=>'', 'readonly'=>'readonly',  'id'=>'tot_amount' )); ?> 
									 </div> 
									 <div class="col-md-2">
									 	
									 </div>
								  </div>

								   </fieldset>
			</div>

<table class="table table-striped " id="product">
        <thead>
			<tr>
				<th width="5%">S.No</th>			
				<th width="15%">Code No</th>			
				<th width="35%">Description</th>			
				<th width="10%">Qty</th>			
				<th width="15%">Rate</th>
				<th width="15%">Amount</th>
				<th width="5%">Remove</th>
			 </tr>
        </thead>
        <tbody>        						

		<?php if($mat_lookup): ?>
		<?php foreach($mat_lookup as $value): ?>
		   <?php 
//$code_no=stripslashes($value->id);
		   $code_no=stripslashes($value->code_no);
		   		 $description=stripslashes($value->description);
		   ?>

		   <tr id="row<?php echo e($st=$st+1); ?>">
				<td valign="top"><?php echo e($st); ?></td>			
				<td><div class="form-group  " >
									<div class="col-md-12">
									  <?php echo Form::text('code_no[]', $code_no,array('id'=>'product_code'.$st,'class'=>'form-control', 'placeholder'=>'')); ?> 
									 </div> 
									<!--  <div class="col-md-2">
									 	
									 </div> -->
								  </div></td>			
				<td><div class="form-group  " >
									<div class="col-md-12">
									  <!--<select name='description[]' rows='5' id='description<?php echo e($st); ?>' class='select2' data_value="<?php //echo $description;?>"  >
									  </select>-->

									  <input type="hidden" name="description[]" id="description<?php echo e($st); ?>" value="<?php echo $description;?>" />
									  <?php
									  $product_details = DB::table('abserve_products')->where('id', $description)->first();
					$product_name = $product_details->name;
					?>
									  <?php echo Form::text('product_name[]', $product_name,array('id'=>'product_name'.$st,'class'=>'form-control', 'placeholder'=>'', 'readonly'=>'readonly')); ?>

									 </div> 
									</div></td>			
				<td><div class="form-group">
									<div class="col-md-12">
									  <?php echo Form::text('qty[]', $value->qty,array('class'=>'form-control', 'placeholder'=>'','id'=>'qty'.$st,'onfocus'=>'calcamount(event,this,'.$st.')','onkeyup'=>'calcamount(event,this,'.$st.')','onkeypress'=>'mvcursoer(event,this,'.$st.')','data_id'=>$st)); ?> 
									 </div> 
									 </div></td>			
				<td><div class="form-group  " >
									<div class="col-md-12">
									  <?php echo Form::text('rate[]',  $value->rate,array('class'=>'form-control', 'placeholder'=>'','id'=>'rate'.$st,'onkeyup'=>'rate_amount(event,this,'.$st.')','onkeypress'=>'mvcursoer(event,this,'.$st.')','data_id'=>$st     )); ?> 
									 </div> 
									</div></td>	
									<td><div class="form-group  " >
									<div class="col-md-12">
									  <?php echo Form::text('amount[]',  $value->amount,array('class'=>'form-control', 'placeholder'=>'','id'=>'amount'.$st, 'readonly'=>'readonly'  )); ?> 
									 </div> 
									</div></td>
									<td><a onclick="removerow(event,this,<?php echo e($st); ?>)" href="javascript:void(0)" class="tips btn btn-xs btn-remove" title="" data-original-title="Remove"><i class="icon icon-remove "></i></a></td>				 
					
			</tr>
		<?php endforeach; ?>
				
		<?php else: ?>
		<?php endif; ?>

			<tr id="row<?php echo e($st=$st+1); ?>">
				<td valign="top"><?php echo e($st); ?></td>			
				<td><div class="form-group  " >
									<div class="col-md-12">
									   <?php echo Form::text('code_no[]', '',array('id'=>'product_code'.$st,'class'=>'form-control', 'placeholder'=>'')); ?>

									 </div> 
									<!--  <div class="col-md-2">
									 	
									 </div> -->
								  </div></td>			
				<td><div class="form-group  " >
									<div class="col-md-12">
									  <select name='description[]' rows='5' id='description<?php echo e($st); ?>' class='select2 lastfinalrow' data_value='' data_id='<?php echo e($st); ?>' ></select>
									 </div> 
									</div></td>			
				<td><div class="form-group">
									<div class="col-md-12">
									  <?php echo Form::text('qty[]', '',array('class'=>'form-control', 'placeholder'=>'','id'=>'qty'.$st,'onfocus'=>'calcamount(event,this,'.$st.')','onkeyup'=>'calcamount(event,this,'.$st.')','onkeypress'=>'mvcursoer(event,this,'.$st.')','data_id'=>$st,'value'=>'0')); ?> 
									 </div> 
									 </div></td>			
				<td><div class="form-group  " >
									<div class="col-md-12">
									  <?php echo Form::text('rate[]',  '',array('class'=>'form-control', 'placeholder'=>'','id'=>'rate'.$st,'onkeyup'=>'rate_amount(event,this,'.$st.')','onkeypress'=>'mvcursoer(event,this,'.$st.')','data_id'=>$st   )); ?> 
									 </div> 
									</div></td>
									<td><div class="form-group  " >
									<div class="col-md-12">
									  <?php echo Form::text('amount[]','',array('class'=>'form-control', 'placeholder'=>'','id'=>'amount'.$st,'value'=>'0', 'readonly'=>'readonly')); ?> 
									 </div> 
									</div></td>
									<td><a onclick="removerow(event,this,<?php echo e($st); ?>)" href="javascript:void(0)" class="tips btn btn-xs btn-remove" title="" data-original-title="Remove"><i class="icon icon-remove "></i></a></td>					 
					
			</tr>
		</tbody>
		
      
</table> 

<div class="total_box">
	<ul style="list-style:  none;">
		<li><b>Total Qty: <span id="tot_qty"></span></b></li>
		<li><b>Total Amount: Rs .<span id="tot_amt"></span></b></li>
	</ul>
</div>

		
			<div style="clear:both"></div>	
				
					
				  <div class="form-group">
					<label class="col-sm-4 text-right">&nbsp;</label>
					<div class="col-sm-8">	
					<button type="submit" name="apply" class="btn btn-info btn-sm" ><i class="fa  fa-check-circle"></i> <?php echo e(Lang::get('core.sb_apply')); ?></button>
					<button type="submit" name="submit" class="btn btn-primary btn-sm" ><i class="fa  fa-save "></i> <?php echo e(Lang::get('core.sb_save')); ?></button>
					<button type="button" onclick="location.href='<?php echo e(URL::to('archivematerialoutwards?return='.$return)); ?>' " class="btn btn-success btn-sm "><i class="fa  fa-arrow-circle-left "></i>  <?php echo e(Lang::get('core.sb_cancel')); ?> </button>
					</div>	  
			
				  </div> 
		 
		 <?php echo Form::close(); ?>

	</div>
</div>		 
</div>	
</div>
<style>
.form-control{width:100%;}
.select2-container{min-width:100%;}
</style>

<script type="text/javascript">
function onselect(c,i,nc=0){
var $id=i;
var product_code=c;
var data = { 'product_code': product_code};
//alert(product_code);
if(product_code!=null && product_code!=''){
$.ajax({    //create an ajax request to display.php
			        type: "GET",
			        url: "<?php echo e(URL::to('product_details')); ?>",            
			        dataType: "json",                
			        data:data,
			        success: function(response) {                    
		            //alert(response.id);
		             $("#product_code"+$id).val(response.code_no);
		             
		             $("#rate"+$id).val(response.rate);
		             
		            //alert(response);
if(nc!=0){
	$("#qty"+$id).val(1);
	//$('#qty'+$id).select();
	$("#amount"+$id).val(response.rate);
		            var count = $('.table-striped tr').length;
		            $('#product > tbody:last-child').append('<tr><td valign="top">'+count+'</td><td><div class="form-group  "><div class="col-md-12"><input id="product_code'+count+'" class="form-control" placeholder="" name="code_no[]" type="text" value=""></div></div></td><td><div class="form-group  "><div class="col-md-12"><select name="description[]"" rows="5" id="description'+count+'" class="select2" data_value=""></select></div></div></td><td><div class="form-group  "><div class="col-md-12"><input class="form-control" placeholder="" id="qty'+count+'" name="qty[]" type="text" value="0" onfocus="calcamount(event,this,'+count+')" onkeyup="calcamount(event,this,'+count+')" onkeypress="mvcursoer(event,this,'+count+')" data_id="'+count+'" ></div></div></td><td><div class="form-group  "><div class="col-md-12"><input class="form-control" placeholder="" id="rate'+count+'" name="rate[]" type="text" value="" onkeyup="rate_amount(event,this,'+count+')"  onkeypress="mvcursoer(event,this,'+count+')" data_id="'+count+'" ></div></div></td><td><div class="form-group  "><div class="col-md-12"><input class="form-control" placeholder="" id="amount'+count+'" name="amount[]" type="text" value="0" readonly="readonly"></div></div></td><td><a onclick="removerow(event,this,'+count+')" href="javascript:void(0)" class="tips btn btn-xs btn-remove" title="" data-original-title="Remove"><i class="icon icon-remove "></i></a></td></tr>');
		            $("#description"+count).jCombo("<?php echo e(URL::to('materialoutwards/comboselect?filter=abserve_products:id:name')); ?>",
		{  selected_value : $("#description"+count).attr('data_value')},count,1);
		            $("#description"+count).select2();
			    $('#qty'+$id).focus();document.execCommand("selectall",null,false);
}

caltot();
		             }

	        	 });
}
   }
function calcamount(event,$this,$id)
	{
		var qty=$('#qty'+$id).val();
		var rate=$('#rate'+$id).val();
		//alert(qty);
		$('#amount'+$id).val(qty*rate);
		caltot();
	}
	function rate_amount(event,$this,$id)
	{
		var qty=$('#qty'+$id).val();
		var rate=$('#rate'+$id).val();
		//alert(qty);
		$('#amount'+$id).val(qty*rate);
		caltot();
	}
	function removerow(event,$this,$id){
		$('#row'+$id).remove();
		caltot();
	}
	function caltot(){
		var count = $('.table-striped tr').length;
var tot_qty=0,tot_amt=0,qty,amt;
for (var i = 1; i < count; i++) {
	qty=$('#qty'+i).val();
amt=$('#amount'+i).val();
if(isNaN(qty)|| qty==''){qty=0;}
if(isNaN(amt) || amt==''){amt=0;}
if(qty!=undefined){
			tot_qty = parseInt(tot_qty) + parseInt(qty);
}
if(amt!=undefined){
	tot_amt = parseInt(tot_amt) + parseInt(amt);
}

}
	$('#tot_amount').val(tot_amt);
	$('#tot_amt').text(tot_amt);
	$('#tot_qty').text(tot_qty);
//$('#qty'+$id).select();
	}
function mvcursoer(event,$this,$id){
	var key = event.which;
	if (key == 13) // the enter key code
	    {	//alert();	
	    	$id= parseInt($id)+1;
	    	event.preventDefault();
	    	 $('#description'+$id).focus();
	    	 $('#description'+$id).select2('open');
	    	 
	    }
}
$(document).on('focus', '.select2', function (e) {
  if (e.originalEvent) {
    $(this).siblings('select').select2('open');    
  } 
});
	$(document).ready(function() { 
		
		
		$("#site").jCombo("<?php echo e(URL::to('archivematerialoutwards/comboselect?filter=tb_site:id:site_name')); ?>",
		{  selected_value : '<?php echo e($row["site"]); ?>' });
		 
var count = $('.table-striped tr').length;
var nc,tot_qty=0,tot_amt=0,qty,amt;
for (var i = 1; i < count; i++) {
	nc=0;
	if(i==(count-1)){
		nc=1;
	}
	if($("#description"+i).hasClass('lastfinalrow')){
		$("#description"+i).jCombo("<?php echo e(URL::to('archivematerialoutwards/comboselect?filter=abserve_products:id:name')); ?>",
			{  selected_value : $("#description"+i).attr('data_value')},i,nc);
	}
qty=$('#qty'+i).val();
amt=$('#amount'+i).val();
if(isNaN(qty)|| qty==''){qty=0;}
if(isNaN(amt) || amt==''){amt=0;}
if(qty!=undefined){
			tot_qty = parseInt(tot_qty) + parseInt(qty);
}
if(amt!=undefined){
	tot_amt = parseInt(tot_amt) + parseInt(amt);
}

}
	$('#tot_amount').val(tot_amt);
	$('#tot_amt').text(tot_amt);
	$('#tot_qty').text(tot_qty);
		$('.removeCurrentFiles').on('click',function(){
			var removeUrl = $(this).attr('href');
			$.get(removeUrl,function(response){});
			$(this).parent('div').empty();	
			return false;
		});		
		
	});

	function printData()
	{
		

		var product=document.getElementById("product");
		var date1=document.getElementById("date");
		var site=document.getElementById("select2-chosen-1");
		var remarks=document.getElementById("remarks");
		var amount=document.getElementById("tot_amount");

		$('#product').find('input:text, input:password, select')
		.each(function () {
			$(this).closest( "td" ).html($(this).val());
			$(this).remove();
		});

		newWin= window.open("");
		newWin.document.write('Date: '+date1.value+'<BR>Site: '+site.innerHTML+'<BR>Remarks: '+remarks.value+'<BR>Total Amount: '+amount.value+'<BR><BR>'+product.outerHTML);
		newWin.print();

		newWin.close();
		window.location.reload();
	}
	$(document).ready(function () {
		jQuery('.printfile').click( function() {
			printData();
		});
	});

	</script>		 
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>