<?php 
	$header_file=APPPATH.'views/include/header.php';
	require($header_file);
	$sidenav_file=APPPATH.'views/include/sidenav.php';
	require($sidenav_file);	
?>
  
    <!-- 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">Damage</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="#">Damage</a>
							</li>
							<li class="breadcrumb-item active">Damage
							</li>
						</ol>
					</div>
				</div>
			</div>
		</div>
        <div class="content-body"><!-- Basic form layout section start -->
			<section id="basic-form-layouts">
				<div class="row justify-content-md-center">
					<form class="form" autocomplete="off" action="<?php echo site_url('MaterialRequest/addRequest');?>" method="post">
						<div class="col-md-12">
							<div class="card">			
								<div class="card-content collapse show">
									<div class="card-body">						
										<div class="row justify-content-md-center cardbody">
											<div class="col-md-6 mleft">
												<div class="form-body">
													<div class="form-group">
													<?php $cur_date = date("d-m-Y"); 
													$branch_id = $_SESSION['branch_id'];
													$branch_name = $_SESSION['branch_name'];
													$full_name = $_SESSION['full_name']; ?>
														<label for="eventInput1">Damage Date</label>
														<input class="form-control" type="text" name="damage_date" id="damage_date" value="<?php echo $cur_date ?>" readonly >
													</div>
												</div>
											</div>
											<div class="col-md-6">
												<div class="form-body">
													<div class="form-group">
														<label for="eventInput1">Damage/Wastage</label>
														<select class="form-control select2" id="ret_type" name="ret_type"  required>
															<option value="">Select Type</option>
															<option value="Damage">Damage</option>
															<option value="Wastage">Wastage</option>	
														</select>
														
														<input type="hidden" id="branch_id" class="form-control"  name="branch_id" value="<?php echo $branch_id;?>">
														<input type="hidden" id="branch_name" class="form-control"  name="branch_name" value="<?php echo $branch_name;?>">
														<input type="hidden" id="full_name" class="form-control"  name="full_name" value="<?php echo $full_name;?>">
														
													</div>
												</div>
											</div>
										</div>						
										<div class="card-content collapse show constrain-movement-container">
											<div class="card-body">								
												<div class="jqueryui-ele-container">
													<div class="tabs-default">
														<ul>
															<li>
																<a href="#raw_material" class="tab-col"> Raw Material</a>
															</li>
															<li>
																<a href="#finished_prdct"  class="tab-col"> Finished Product</a>
															</li>
															<input type="submit" id="insert" class="btn btn-success pull-left" style="border-radius: 10px;      color: #000;float:right;margin-top: -9px;" value="Submit"/>
														</ul>
														<div id="raw_material"> </div>
														<div id="finished_prdct"> </div>
													</div>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</form>
				</div>
			</section>
		</div>
	</div>
</div>
<?php //require('include/theme_customizer.php');?>
<?php 
  	$footer_file=APPPATH.'views/include/footer.php';
	require($footer_file);
?>
<script>
/*	$(document).ready(function() {
		var branch_id = $('#branch_id').val();
		//alert(branch_id);
		$.ajax({
			type :'POST',
			dataType:'json',
			data : { branch_id : branch_id },
			url: '<?php // echo base_url();?>index.php/Damage/itemDetails',
			cache: false,
            error: function (xhr, ajaxOptions, thrownError) {
               // alert(xhr.responseText);
                //alert(thrownError);
            },
            xhr: function () {
                var xhr = new window.XMLHttpRequest();
                //Download progress
                xhr.addEventListener("progress", function (evt) {
                    console.log(evt.lengthComputable);
                    if (evt.lengthComputable) {
                        var percentComplete = evt.loaded / evt.total;
                        console.log(percentComplete );
                    }
                }, false);
                return xhr;
            },
            beforeSend: function () {
              // before send
            }
		});
	}); */
	
	$(document).ready(function() {
		var branch_id = $('#branch_id').val();
		$.ajax({
			type :'POST',
			dataType:'json',
			data : { branch_id : branch_id },
			url: '<?php echo base_url();?>index.php/Damage/itemDetails',
		});
	});
	$(function() {
		var item = [];
		var items = "", item_name = "";
		var fproduct = "";
		var count = 1;
		$.getJSON('https://gsrgroup.biz/inv_demo/item_list.json',function(data) {
		//    alert(count);
			for(i in data.products)
			{
			    items += "<a style='width: 100%;text-align:left;' href='#demo"+data.products[i].category_id +"' class='btn btn-primary category_list' data-toggle='collapse'>" + data.products[i].category + "</a>";
				items += "<div id='demo"+data.products[i].category_id +"' class='collapse'>";
				items += "<table border='1' class='uk-table'>";
				items += "<tr style='text-align:center;'><th>Item Name</th><th>UOM</th><th style='width:100px'>Qty</th><th>Remarks</th></tr>";
				for(j in data.products[i].items)
				{
					//	$.each(data.products[i].items, function(j,e) {
					items += "<tr style='text-align:center;'>" + "<td style='width: 45%!important;'>" + data.products[i].items[j].item_name + "<input type='hidden' name='item_name[]' id='item_name' class='form-control' value='"+data.products[i].items[j].item_name+"'></td>" + "<td>" + data.products[i].items[j].uom + "<input type='hidden' name='uom[]' id='uom' class='form-control' value='"+data.products[i].items[j].uom+"'></td>"  + "<td>" + "<input type='text' name='dmg_qty[]' onkeypress='javascript:return isNumber(event)' id='dmg_qty' placeholder='Qty' class='form-control dmg_qty'><input type='hidden' name='pr_stk[]' id='pr_stk' class='form-control pr_stk' value='"+data.products[i].items[j].prsnt_stk+"'><input type='hidden' name='item_id[]' id='item_id' class='form-control' value='"+data.products[i].items[j].item_id+"'><input type='hidden' name='cat_id[]' id='cat_id' class='form-control' value='"+data.products[i].items[j].cat_id+"'><input type='hidden' name='itm_id[]' id='itm_id' class='form-control' value='"+data.products[i].items[j].itm_id+"'><input type='hidden' name='item_type[]' id='item_type' class='form-control' value='Raw Material'>" + "</td>" + "<td>" + "<input type='text' name='remarks[]' id='remarks' placeholder='Remarks' class='form-control'></td>" + "</tr>"
				}
				count++;
				items += "</table>"; 
				items += "</div>";
			}
			//alert(items);
			$('#raw_material').append(items);
			//$(items).appendTo("#item_table tbody");
		});
		
		$.getJSON('https://gsrgroup.biz/inv_demo/product_list.json',function(data) {
			for(i in data.finished_products)
			{
				fproduct += "<a style='width: 100%;text-align:left;' href='#demo"+data.finished_products[i].pr_cat_id +"' class='btn btn-primary category_list' data-toggle='collapse'>" + data.finished_products[i].prod_cat + "</a>";
				fproduct += "<div id='demo"+data.finished_products[i].pr_cat_id +"' class='collapse'>";
				fproduct += "<table border='1' class='uk-table'>";
				fproduct += "<tr style='text-align:left;'><th>Item Name</th><th>UOM</th><th style='width:100px'>Qty</th><th>Remarks</th></tr>";
				for(j in data.finished_products[i].product)
				{
					//	$.each(data.products[i].items, function(j,e) {
					fproduct += "<tr style='text-align:center;'>" + "<td style='width: 45%!important;'>" + data.finished_products[i].product[j].product_name + "<input type='hidden' name='item_name[]' id='item_name' class='form-control' value='"+data.finished_products[i].product[j].product_name+"'></td>" + "<td>" + data.finished_products[i].product[j].uom + "<input type='hidden' name='uom[]' id='uom' class='form-control' value='"+data.finished_products[i].product[j].uom+"'></td>"  + "<td>" + "<input type='text' name='dmg_qty[]' onkeypress='javascript:return isNumber(event)' id='dmg_qty' placeholder='Qty' class='form-control dmg_qty'><input type='hidden' name='pr_stk[]' id='pr_stk' class='form-control pr_stk' value='"+data.finished_products[i].product[j].prsnt_stk+"'><input type='hidden' name='item_id[]' id='item_id' class='form-control' value='"+data.finished_products[i].product[j].recipe_id+"'><input type='hidden' name='cat_id[]' id='cat_id' class='form-control' value='"+data.finished_products[i].product[j].cat_id+"'><input type='hidden' name='itm_id[]' id='itm_id' class='form-control' value='"+data.finished_products[i].product[j].recipe_no+"'><input type='hidden' name='item_type[]' id='item_type' class='form-control' value='Finished Product'>" + "</td>" + "</td>" + "<td>" + "<input type='text' name='remarks[]' id='remarks' placeholder='Remarks' class='form-control'></td>" + "</tr>"
				}
				count++;
				fproduct += "</table>"; 
				fproduct += "</div>";
			}
			//alert(fproduct);
			$('#finished_prdct').append(fproduct);
			//$(items).appendTo("#item_table tbody");
		});
	});
	/*function isNumber(evt) {
	    var iKeyCode = (evt.which) ? evt.which : evt.keyCode
	    if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
		return false;
	    return true;
	}*/
	$('.dmg_qty').each(function() { $(this).on( {'keyup': function() {
			var q1 = $(this).parent().find('.dmg_qty').val();
			var q2 = $(this).parent().find('.pr_stk').val();
				alert(q1);
			if(parseFloat(q1) <= parseFloat(q2)){
				//alert("sucess");
			}
			else{
				alert("You cant enter more than PO balance quantity");
			}
		}
		})
	})
</script>
<script type="text/javascript">
	//inserting values to the database
	$(document).ready(function()
	{
		$("#insert").click(function() {
			var branch = $('#branch_name').val();
			var branch_id = $('#branch_id').val();
			var ret_type = $('#ret_type').val();
			var item_type = $('input[name="item_type[]"]').map(function(){ return this.value }).get();
			var item_name = $('input[name="item_name[]"]').map(function(){ return this.value }).get();
			var uom=$('input[name="uom[]"]').map(function() { return this.value }).get();
			var prsnt_stk=$('input[name="pr_stk[]"]').map(function() { return this.value }).get();
			var dmg_qty=$('input[name="dmg_qty[]"]').map(function() { return this.value }).get();
			var item_id=$('input[name="item_id[]"]').map(function() { return this.value }).get();
			var itm_id=$('input[name="itm_id[]"]').map(function() { return this.value }).get();
			var cat_id=$('input[name="cat_id[]"]').map(function() { return this.value }).get();
			var remarks=$('input[name="remarks[]"]').map(function() { return this.value }).get();
			var dataString="ret_type="+ret_type+"&item_type="+item_type+"&item_name="+item_name+"&uom="+uom+"&prsnt_stk="+prsnt_stk+"&dmg_qty="+dmg_qty+"&item_id="+item_id+"&cat_id="+cat_id+"&itm_id="+itm_id+"&branch="+branch+"&branch_id="+branch_id+"&remarks="+remarks+"&insert=";
			//alert(ret_type);
			//alert(item_name);
			/*for(i=0;i<item_name.length;i++)
				{
				alert(item_name[i]);
			}*/
			if($.trim(ret_type).length>0 & $.trim(item_type).length>0 & $.trim(item_name).length>0 & $.trim(uom).length>0 & $.trim(prsnt_stk).length>0 & $.trim(dmg_qty).length>0 & $.trim(item_id).length>0)
			//if($.trim(item_type).length>0)
			{
				//alert(dataString);
				if (confirm('Are you sure you want to submit?')) {
					$.ajax({
						type: "POST",
						url: '<?php echo base_url();?>index.php/Damage/insertDamageItem',
						data: dataString,
						crossDomain: true,
						cache: false,
						success: function(data){
							//alert(data);
							if(data)
							{
								//alert("Damage Added Successfully");
								//alert(data);
								//$("#insert").val('Submit');
								window.location = '<?php echo base_url();?>index.php/Damage/damageWasteReceipt/'+data;
							}
							else if(data=="error")
							{
								alert("error");
							}
						}
					});
				}
			}return false;
		});
	}); 
</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");
	});
</script>
</body>
</html>