<?php 
	$header_file=APPPATH.'views/include/header.php';
	require($header_file);
	$sidenav_file=APPPATH.'views/include/sidenav.php';
	require($sidenav_file);	
?>
<style>
	.card {
	margin-bottom: 0.475rem!important;
	border: none!important;
	box-shadow: 0 2px 18px 1px rgb(49 53 72 / 10%)!important;
	border-radius: .45rem!important;
	}
	form .form-group {
	margin-bottom: 0.4rem!important;
	}
</style> 
<!-- BEGIN: Content-->
<div class="app-content content">
	<div class="content-overlay"></div>
	<div class="content-wrapper">
		<div class="content-header row">
			<div class="content-header-left col-md-12 col-12 mb-2 breadcrumb-new">
				<h3 class="content-header-title mb-0 d-inline-block">Issues </h3>
				<div class="row breadcrumbs-top d-inline-block">
					<div class="breadcrumb-wrapper col-12">
						<ol class="breadcrumb">
							<li class="breadcrumb-item"><a href="#">Home</a>
							</li>
							<li class="breadcrumb-item"><a href="#">Issues</a>
							</li>
							<li class="breadcrumb-item active">Issues 
							</li>
						</ol>
					</div>
				</div>
			</div>
		</div>
		<div class="content-body"><!-- Basic form layout section start -->
			<section id="basic-form-layouts">
				<form class="form" autocomplete="off" action="<?php echo site_url('MaterialRequest/addIssue');?>" method="post" enctype="multipart/form-data">
					<div class="row justify-content-md-center">
						<div class="col-md-12">
							<div class="card">
								<div class="card-content collapse show">
									<div class="card-body">
									<?php 	
										foreach($mat_req as $mreq) 
									?>
										<div class="row justify-content-md-right  card_margin">
											<div class="col-md-3">
												<div class="form-group">
													<label>RO.NO</label>
													<input type="text" name="request_no" id="request_no" class="form-control" value="<?php echo $mreq->mat_req_no; ?>" readonly />
												</div>
											</div>
											<div class="col-md-2">
												<div class="form-group">
													<label>RO DATE</label>
													<input type="text" name="req_date"  id="req_date" class="form-control" readonly="readonly" value="<?php echo $mreq->request_date; ?>"/>
												</div>
											</div>
											<div class="col-md-2">
												<div class="form-group">
													<label>RO Time</label>
													
													<input class="form-control" type="text" name="issue_date"  id="issue_date" value="<?php echo $mreq->request_time; ?>" readonly >
												</div>
											</div>
											<div class="col-md-2">
												<div class="form-group">
													<label>Issue DATE</label>
													<?php $cur_date = date("d-m-Y"); ?>
													<input class="form-control" type="text" name="issue_date"  id="issue_date" value="<?php echo $cur_date; ?>" readonly >
												</div>
											</div>
											
											<div class="col-md-3">
												<div class="form-group">
													<label>Outlet Name</label>
													<input class="form-control" type="text" name="outlet" id="outlet" value="<?php echo $mreq->username; ?>" >
													<input class="form-control" type="hidden" name="outlet_branch" id="outlet_branch" value="<?php echo $mreq->branchname; ?>" >
													<input class="form-control" type="hidden" name="req_id" id="req_id" value="<?php echo $mreq->request_id; ?>" >
													<input type="hidden" name="branchname" id="branchname" class="form-control" value="<?php echo $_SESSION['branch_name']?>"/>
													<input type="hidden" name="branch_id" id="branch_id" class="form-control" value="<?php echo $_SESSION['branch_id']?>"/>
													<input type="hidden" name="uname" id="uname" class="form-control" value="<?php echo $_SESSION['username']?>"/>
													<input type="hidden" name="user_name" id="user_name" class="form-control" value="<?php echo $_SESSION['full_name']?>"/>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="row ">						
								<?php
								$count = 1;
								foreach($mat_req_item as $rqitem)
								{
								?>
								<div class="col-md-5">
									<div class="card">
										<div class="card-content collapse show">
											<div class="card-body" >
												<div class="row card_margin"  style="margin-top: -12px!important;margin-left: -16px!important;">
													<div class="col-md-7">
														<div class="form-group">
															<input class="form-control" type="text" name="iname[]" id="iname" value="<?php echo $rqitem->item_name; ?>" readonly style="    width: 270px!important;" />
														</div>
													</div>
													<div class="col-md-3">
														<div class="form-group">
															<input class="form-control" type="text" name="rqty[]"  id="rqty" value="<?php echo $rqitem->required_qty; ?>" readonly />
														</div>
													</div>
													<div class="col-md-2">
														<div class="form-group">
															<input class="form-control" type="text" name="rmk[]" id="rmk" value="<?php echo $rqitem->remarks; ?>" disabled />
														</div>
													</div>
												</div>
											</div>
										</div>
									</div>
								</div>
								<div class="col-md-7">
									<div class="card">
										<div class="card-content collapse show">
											<div class="card-body">
											<?php
												if($rqitem->required_qty == $rqitem->recieved_qty) 
												{
											?>
												<div class="row card_margin">
													<div class="col-md-3">
														<div class="form-group">
															<input class="form-control" type="text" name="iqty"  id="iqty[]" value="<?php echo $rqitem->required_qty; ?>" placeholder ="Qty" disabled />
														</div>
													</div>
													<div class="col-md-1">
														<div class="form-group">
															<img src="<?php echo base_url(); ?>app-assets/images/right.png" alt="" class="img_main"/>
														</div>
													</div>
													<div class="col-md-2">
														<div class="form-group" style="    margin-bottom: 0.4rem!important;">
															<span>Issued</span>
														</div>
													</div>
												</div>
											<?php
												} else {
											?>
												<div class="row card_margin">
													<input class="form-control col-md-1 qty comm" type="text" name="is_quantity[]"  id="is_quantity"  placeholder="Qty"   data-uk-tooltip="{pos:'bottom'}" title=" Remaining Qty :  <?php echo $rqitem->item_balance_qty; ?>"  />
													
													<input class="form-control col-md-1 stk comm" type="text" name="pr_stock[]" id="pr_stock" value="<?php echo $rqitem->stock_item; ?>" data-uk-tooltip="{pos:'bottom'}" title="Present Stock" readonly />
													
													<input class="form-control col-md-1 comm" type="text" name="issued_qty[]" id="issued_qty" value="<?php echo $rqitem->recieved_qty; ?>" data-uk-tooltip="{pos:'bottom'}" title="Issued Qty" readonly />
													<?php 
														$price = $rqitem->item_price;
														$tax = $rqitem->item_tax;
														$price1 = round(((($price*$tax)/100)+$price),2);
													?>
													<input class="form-control col-md-2 price comm" type="text" name="is_price[]"  id="is_price[]" value="<?php echo $rqitem->item_price; ?>" data-uk-tooltip="{pos:'bottom'}" title="Item Price" readonly  />

													<input class="form-control col-md-1 tax comm" type="text" name="tax[]"  id="tax[]" value="<?php echo $rqitem->item_tax; ?>" data-uk-tooltip="{pos:'bottom'}" title="Item Tax" readonly  />

													<input class="form-control col-md-2 grand" type="text" name="total_amount[]" id="total_amount[]"  placeholder="Amount" readonly />

													<input class="form-control col-md-2 comm" type="text" name="remarks[]" id="remarks[]" placeholder="Remarks"/>

													<input class="form-control  tax_amt" type="hidden" name="tax_amt[]"  id="tax_amt[]" readonly  />

													<input class="form-control total" type="hidden" name="total_price[]" id="total_price[]" />

													<input class="form-control qty_pending" type="hidden" name="balance_qty[]" id="balance_qty" value="<?php echo $rqitem->item_balance_qty; ?>"/>

													<input class="form-control" type="hidden" name="req_item_id[]" id="req_item_id" value="<?php echo $rqitem->request_item_id; ?>">

													<input class="form-control" type="hidden" name="item_name[]" id="item_name" value="<?php echo $rqitem->item_name; ?>" readonly />

													<input class="form-control" type="hidden" name="uom[]" id="uom" value="<?php echo $rqitem->uom; ?>" readonly />

													<input class="form-control" type="hidden" name="req_qty[]"  id="req_qty" value="<?php echo $rqitem->required_qty; ?>" readonly />

													<input class="form-control" type="hidden" name="item_id[]" id="item_id" value="<?php echo $rqitem->item_id; ?>">

													<input class="form-control" type="hidden" name="itm_id[]" id="itm_id" value="<?php echo $rqitem->itm_id; ?>">

													<input class="form-control" type="hidden" name="cat_id[]" id="cat_id" value="<?php echo $rqitem->category_id; ?>">
												</div>
											<?php
											}
											?>	
											</div>
										</div>
									</div>
								</div>
								<?php 
									$count++; 
								} ?>
							</div>	
							
							<div class="card">
								<div class="card-content collapse show">
									<div class="card-body">
										<div class="text-right">
											<div class="row card_margin">
												<div class="col-md-2 mleft">
													<div class="form-group">
														<label>Total Amount</label>
														<input class="form-control" type="text" name="total_amt" id="total_amt" value="0" readonly />
													</div>
												</div>
												<div class="col-md-8 mleft">
													<div class="form-group">
													<!--	<button type="submit" id="submit" class="btn btn-primary" onclick="return confirm('Are you sure you want to submit?')">
														<i class="la la-check-square-o"></i> Add Issue
														</button>-->
														
														<input type="submit" class="btn btn-sm btn-primary submit" onclick="this.disabled=true;this.value='Sending, please wait...';this.form.submit();confirm('Are you sure you want to submit this form?');" >
													</div>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</form>
					<br/>
					<?php
						$req_id = $mreq->request_id;
						$q1 = $this->db->query("SELECT *,M.status as store_status FROM material_request M left join material_request_item MI on M.request_id=MI.request_id where M.request_id='$req_id'");
						$count1 = 0;
						$count2 = 0;
						$status;
						$result3 = $q1->result();
						foreach($result3 as $row3)
						{
							$status = $row3->mclose_status;
							if(($row3->recieved_qty == $row3->receipt_qty))
							{	
								$count2++;	
							}
						$count1++;
						}
						if($count1 == $count2) 
						{
					?>
					<div class="col-lg-4 col-sm-4 col-12">
						<div class="form-group ">
						<!--	<button class="btn btn-lg btn-success font-medium-1 grid-form-dialog-btn mt-1">Close Manual</button>-->
							<button type="button" class="btn btn-warning grid-form-dialog-btn">Close Manual</button>
							<p class="font-small-3 mt-1"></p>
							<div class="grid-form-dialog" title="Manual Issues Close">
								<div class="container">
									<form action="" class="uk-form-stacked"  method="post" autocomplete="off">
										<div class="form-group row">
											<label for="inputEmail3" class="col-sm-2 col-form-label">Enter Remarks</label>
											<div class="col-sm-10">
												<input name="request_id" id="request_id" type="hidden" class="textbox" value="<?php echo $mreq->request_id; ?>" >
												<input type="text" class="form-control"name="req_remark" id="req_remark">
											</div>
										</div>
										<div class="form-group row">
											<div class="offset-sm-2 col-sm-10">
												<button type="button" class="btn btn-warning uk-modal-close">Close</button>
												<input type="button" class="btn btn-info" value="Confirm" name="submit3" id="submit3">
											</div>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
					<?php  } else if($status == 'Approved') { ?>
					<div class="col-lg-4 col-sm-4 col-12">
						<div class="form-group text-center">
							<button class="btn btn-lg btn-success font-medium-1 grid-form-dialog-btn mt-1">Close Manual</button>
							<p class="font-small-3 mt-1"></p>
							<div class="grid-form-dialog" title="Manual Issues Close">
								<div class="container">
									<form action="" class="uk-form-stacked"  method="post" autocomplete="off">
										<div class="form-group row">
											<label for="inputEmail3" class="col-sm-2 col-form-label">Enter Remarks</label>
											<div class="col-sm-10">
												<input name="request_id" id="request_id" type="hidden" class="textbox" value="<?php echo $mreq->request_id; ?>" >
												<input type="text" class="form-control"name="req_remark" id="req_remark">
											</div>
										</div>
										<div class="form-group row">
											<div class="offset-sm-2 col-sm-10">
												<button type="button" class="md-btn md-btn-flat uk-modal-close">Close</button>
												<input type="button" class="md-btn md-btn-flat md-btn-flat-primary" value="Confirm" name="submit3" id="submit3">
											</div>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>			
			</div>
		</div>
	</div>
</section>
<!-- // Basic form layout section end -->
</div>
</div>
</div>
<?php //require('include/theme_customizer.php');?>

<?php 
	$footer_file=APPPATH.'views/include/footer.php';
	require($footer_file);
?>
<script type='text/javascript'>//<![CDATA[
	//calculation
	$('.qty').each(function() { $(this).on( {'keyup': function() {
		var qty = $(this).parent().find('.qty').val();
		var price = $(this).parent().find('.price').val();
		var tax = $(this).parent().find('.tax').val();
		//alert(price);
		var tot_basic = (parseFloat(qty) * parseFloat(price)).toFixed(2);
		var tot_tax = ((parseFloat(tot_basic) * parseFloat(tax))/100).toFixed(2);
		var total_price = (parseFloat(tot_basic) + parseFloat(tot_tax)).toFixed(2);
		$(this).parent().find('.total').val(tot_basic);
		$(this).parent().find('.tax_amt').val(tot_tax);
		$(this).parent().find('.grand').val(total_price);
		}
		})
	})
</script>
<script>
	$(".qty").each( function(){ $(this).on( {'keyup': function() { 
		var q1 = $(this).parent().find('.qty').val();
		var q2 = $(this).parent().find('.qty_pending').val();
		var grand = $(this).parent().find('.grand').val();
		var tot = $("#total_amt").val();
		var tot_amt = 0;
		//alert(q2);
		if(q1 != '')
		{
			if(parseFloat(q1) <= parseFloat(q2)){
			//alert("sucess");
			}
			else{
				alert("You cant enter more than RO balance quantity");
				tot_amt = parseFloat(tot) - parseFloat(grand);
				//$('#inward_quantity').focus();
				$(this).parent().find('.qty').val('0');
				$(this).parent().find('.total').val('0');
				$(this).parent().find('.tax_amt').val('0');
				$(this).parent().find('.grand').val('0');
				$("#total_amt").val(tot_amt.toFixed(2));
			}
		}
		else
		{
			$(this).parent().find('.total').val('0');
			$(this).parent().find('.tax_amt').val('0');
			$(this).parent().find('.grand').val('0');
		}
		}
		})
	})
</script>
<script>
	$(".qty").each( function(){ $(this).on( {'keyup': function() { 
	var q1 = $(this).parent().find('.qty').val();
	var q2 = $(this).parent().find('.stk').val();
	var grand = $(this).parent().find('.grand').val();
	var tot = $("#total_amt").val();
	var tot_amt = 0;
	if(q1 != '')
	{
		if(parseFloat(q1) <= parseFloat(q2)){
		//alert("sucess");
		}
		else{
			alert("You cant enter more than the Present Stock");
			tot_amt = parseFloat(tot) - parseFloat(grand);
			//$('#inward_quantity').focus();
			$(this).parent().find('.qty').val('0');
			$(this).parent().find('.grand').val('0');
			$(this).parent().find('.total').val('0');
			$(this).parent().find('.tax_amt').val('0');
			$("#total_amt").val(tot_amt.toFixed(2));
		}
	}
	else
	{
		$(this).parent().find('.total').val('0');
		$(this).parent().find('.tax_amt').val('0');
		$(this).parent().find('.grand').val('0');
	}
	}
	})
	})
</script>
<script>
	$(".qty").each( function(){ $(this).on( {'keyup': function() { 
		var q1 = $(this).parent().find('.qty').val();
		var q2 = $(this).parent().find('.qty_pending').val();
		var grand = $(this).parent().find('.grand').val();
		var tot = $("#total_amt").val();
		var tot_amt = 0;
		if(q1 != '')
		{
			if(parseFloat(tot) < 49000)
			{
			//alert("success");
			}
			else
			{
				alert("Bill amount is greater than 49000");
				tot_amt = parseFloat(tot) - parseFloat(grand);
				//$('#inward_quantity').focus();
				$(this).parent().find('.qty').val('0');
				$(this).parent().find('.grand').val('0');
				$(this).parent().find('.total').val('0');
				$(this).parent().find('.tax_amt').val('0');
				$("#total_amt").val(tot_amt.toFixed(2));
			}
		}
		else
		{
			$(this).parent().find('.total').val('0');
			$(this).parent().find('.tax_amt').val('0');
			$(this).parent().find('.grand').val('0');
		}
	}
	})
	})
</script>
<script type="text/javascript">
	$('.qty').keyup(function () {
		var sum1 = 0;
		// we use jQuery each() to loop through all the textbox with 'price' class
		// and compute the sum for each loop
		$('.grand').each(function() {
			sum1 += Number($(this).val());
		});
		var t_sum = (sum1).toFixed(2);
		// set the computed value to 'totalPrice' textbox
		$('#total_amt').val(t_sum);
	});
</script>
<script>
	$('#submit3').click(function() {
		var req_remark = $('#req_remark').val();
		var req_id = $('#request_id').val();
		if(req_remark == '')
		{
			alert("Please Enter Remarks");
			return false;
		}
		else
		{
			$.ajax({
			type: "POST",
			dataType:'json',
			data : { req_id : req_id, req_remark : req_remark },
			url: "<?php echo base_url();?>index.php/MaterialRequest/requestRmkUpdate",
			success: function(data) { 
			setTimeout(function() {
				window.location.href = '<?php echo site_url('MaterialRequest/issueClosedList');?>';
			}, 200);
			}
			});
		}
	});
</script>
<script>
	$(document).ready(function () {
		$(".submit").attr('disabled', 'disabled');
		$(".submit").css("background-color","#dcdcdc");
		//$("form").keyup(function() {
		$("form").on('keyup change', function() {
			var qty = $('input[name="is_quantity[]"]').map(function(){ return this.value }).get();
			var qcount = qty.length;
			var qc =0;
			//alert(qcount);
			for(var i=0;i<qcount;i++)
			{
				if(qty[i] != '' || qty[i] != 0)
				{
					qc = qc + 1;
				}
			}
			//alert(qc);
			if(parseInt(qc) < 1)
			{
				$(".submit").attr('disabled', 'disabled');
				$(".submit").css("background-color","#dcdcdc");
			}
			else
			{
				$(".submit").removeAttr('disabled');
				$(".submit").css("background-color","#543729");
			}
		});
	});
</script>
<script>
	$(window).on('beforeunload', function () {
		//$("input[type=submit], input[type=button]").prop("disabled", "disabled");
		$("input[type=submit]").prop("disabled", "disabled");
		$("#submit").css("background-color","#dcdcdc");
	});

	$(document).ready(function() {
		$(window).keydown(function(event){
			if(event.keyCode == 13) {
				event.preventDefault();
				return false;
			}
		});
	});
</script>
</body>
</html>