<?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 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 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">
														    <?php /*foreach($rcat as $row1) {
												    ?>
														    <a style="width: 100%;text-align:left;" href="#demo<?php echo $row1->category_id; ?>" class="btn btn-primary category_list" data-toggle="collapse"><?php echo $row1->category_name; ?></a>
														    					        <div id="demo<?php echo $row1->category_id; ?>" class="collapse">
														        <table border="1" class="uk-table">
										    <tr style="text-align:center;">
										        <th>Item Name</th>
										        <th>UOM</th>
										        <th style="width:100px;">Qty</th>
										        <th>Remarks</th>
										    </tr>
										    <?php
												    $rc_id = $row1->category_id;
												    $rcq = $this->db->query("SELECT T.item_name,T.uom,T.stock_item,T.item_id,T.itm_id,T.category_id FROM item_creation T inner join category C on C.category_id=T.category_id where C.category_id='$rc_id' and T.branch_id='$branch_id' order by T.item_name");
												    $ritems = $rcq->result_array();
												    foreach($ritems as $rirow)
    								    {
												    ?>
										    <tr style="text-align:center;">
										        <td style="width: 45%!important;"><?php echo $rirow['item_name'] ?><input type="hidden" name="item_name[]" id="item_name" class="form-control" value="<?php echo $rirow['item_name'] ?>"></td>
										        <td><?php echo $rirow['uom'] ?><input type="hidden" name="uom[]" id="uom" class="form-control" value="<?php echo $rirow['uom'] ?>"></td>
										        <td><input type="text" name="dmg_qty[]" 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="<?php echo $rirow['stock_item']; ?>"><input type="hidden" name="item_id[]" id="item_id" class="form-control" value="<?php echo $rirow['item_id']; ?>"><input type="hidden" name="cat_id[]" id="cat_id" class="form-control" value="<?php echo $rirow['category_id']; ?>"><input type="hidden" name="itm_id[]" id="itm_id" class="form-control" value="<?php echo $rirow['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>
										    <?php } ?>
										</table>
										</div>
										<?php }*/ ?>-->
										
										</div>
														<div id="finished_prdct">
														    <?php foreach($pcat as $row2) {
												    ?>
														    <a style="width: 100%;text-align:left;" href="#demo<?php echo $row2->pr_cat_id; ?>" class="btn btn-primary category_list" data-toggle="collapse"><?php echo $row2->pr_cat_name; ?></a>
														    								        <div id="demo<?php echo $row2->pr_cat_id; ?>" class="collapse">
												<table border="1" class="uk-table">
										    <tr style="text-align:center;">
										        <th>Item Name</th>
										        <th>UOM</th>
										        <th style="width:100px;">Qty</th>
										        <th>Remarks</th>
										    </tr>
										    <?php
												    $prc_id = $row2->pr_cat_id;
												    $pcq = $this->db->query("SELECT R.rname,R.uom,R.production_stock,R.daily_stk_update,R.recipe_id,R.recipe_no,R.pr_cat_id FROM recipe R inner join product_category PC on PC.pr_cat_id=R.pr_cat_id where PC.pr_cat_id='$prc_id' and R.branch_id='$branch_id' and R.status='Enabled' order by R.rname");
												    $pitems = $pcq->result_array();
												    foreach($pitems as $pirow)
    								    {
												    ?>
										    <tr style="text-align:center;">
										        <td style="width: 45%!important;"><?php echo $pirow['rname'] ?><input type="hidden" name="item_name[]" id="item_name" class="form-control" value="<?php echo $pirow['rname'] ?>"></td>
										        <td><?php echo $pirow['uom'] ?><input type="hidden" name="uom[]" id="uom" class="form-control" value="<?php echo $pirow['uom'] ?>"></td>
										        <td><input type="text" name="dmg_qty[]" 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="<?php echo $pirow['production_stock']; ?>"><input type="hidden" name="item_id[]" id="item_id" class="form-control" value="<?php echo $pirow['recipe_id']; ?>"><input type="hidden" name="cat_id[]" id="cat_id" class="form-control" value="<?php echo $pirow['pr_cat_id']; ?>"><input type="hidden" name="itm_id[]" id="itm_id" class="form-control" value="<?php echo $pirow['recipe_no']; ?>"><input type="hidden" name="item_type[]" id="item_type" class="form-control" value="Finished Product"></td>
										        <td><input type="text" name="remarks[]" id="remarks" placeholder="Remarks" class="form-control"></td>
										    </tr>
										    <?php } ?>
										</table>
										</div>
										<?php } ?>
														</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 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>