<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->
<link rel="stylesheet" href="<?php echo asset_url(); ?>css/buttons.dataTables.min.css" type="text/css" />
<style type="text/css">
	.dt-button-collection {
		font-size: 1.4em;
	}
	.dt-button-collection .active span:after {
		font-family: FontAwesome;
		content: " \f00c";
		text-align: right
	}
	a.dt-button,
	button.dt-button,
	div.dt-button,
	.dt-buttons {
		font-size: .83em;
		float: right !important;
		/*  background-color:#e9e9e9;
   background-image:linear-gradient(to bottom,#fff 0,#e9e9e9 100%);*/
		margin-left: .3em;
	}
</style>
<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->
<body>
	<!-- start: HEADER -->
	<div class="navbar navbar-inverse navbar-fixed-top">
		<!-- start: TOP NAVIGATION CONTAINER -->
		<div class="container">
			<div class="navbar-header">
				<!-- start: RESPONSIVE MENU TOGGLER -->
				<button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
					<span class="clip-list-2"></span>
				</button>
				<!-- end: RESPONSIVE MENU TOGGLER -->
				<!-- start: LOGO -->
				<?php $this->load->view("common/logo"); ?>
				<!-- end: LOGO -->
			</div>
			<div class="navbar-tools">
				<!-- start: TOP NAVIGATION MENU -->
				<?php $this->load->view("common/notifications.php"); ?>
				<!-- end: TOP NAVIGATION MENU -->
			</div>
		</div>
		<!-- end: TOP NAVIGATION CONTAINER -->
	</div>
	<!-- end: HEADER -->
	<!-- start: MAIN CONTAINER -->
	<div class="main-container">
		<div class="navbar-content">
			<!-- start: SIDEBAR -->
			<?php $this->load->view("common/navigation"); ?>
			<!-- end: SIDEBAR -->
		</div>
		<!-- start: PAGE -->
		<div class="main-content">
			<!-- end: SPANEL CONFIGURATION MODAL FORM -->
			<div class="container">
				<!-- start: PAGE HEADER -->
				<div class="row">
					<div class="col-sm-12">
						<!-- start: PAGE TITLE & BREADCRUMB -->
						<ol class="breadcrumb">
							<li>
								<a href="<?php echo base_url('dashboard'); ?>">
									DASHBOARD
								</a>
							</li>
							<li>
								<a href="#">
									MANUFACTURING
									<?php $cmn = new $this->Common_Model();
									if ($cmn->check_permission(113, $this->session->userdata('ss_group_id'), 1)) {
										echo "S";
									} ?>
								</a>
							</li>
							<li class="active">
								LIST CURRENT MANUFACTURING
							</li>
							<li class="search-box">
								<form class="sidebar-search">
									<div class="form-group">
										<input type="text" placeholder="Start Searching...">
										<button class="submit">
											<i class="clip-search-3"></i>
										</button>
									</div>
								</form>
							</li>
						</ol>
						<div class="page-header">
							<h3>LIST CURRENT MANUFACTURING</h3>
						</div>
						<p>Please use the table below to navigate or filter the results. </p>
					</div>
				</div>
				<!-- end: PAGE HEADER -->
				<!-- start: PAGE CONTENT 
            <!-- start grid -->
				<div class="row">
					<div class="col-md-12">
						<!-- start: DYNAMIC TABLE PANEL -->
						<div class="panel panel-default">
							<div class="panel-heading">
								<i class="fa fa-external-link-square"></i>
								LIST CURRENT MANUFACTURING
							</div>
							<div class="panel-body">
								<div class="col-sm-12">
									<div class="col-sm-3">
										<div class="form-group">
											<label>WAREHOUSE * </label>
											<select id="warehouse_id" class="form-control" name="warehouse_id" onchange="products_load();">
												<option value="">-- Select Warehouse --</option>
												<?php $ss_warehouse_id = $this->session->userdata('ss_warehouse_id');
												foreach ($warehouse_list as $row) {
													$sel = '';
													if ($ss_warehouse_id == $row->id) {
														$sel = ' selected="selected"';
													} ?>
													<option value="<?php echo $row->id; ?>" <?php echo $sel ?>> <?php echo $row->name; ?> </option>
												<?php } ?>
											</select>
										</div>
									</div>
									
									<div class="col-sm-3">
              <div class="form-group">
                <label>DEPARTMENT * </label>
                <select id="department_id" class="form-control" name="department_id" onchange=" products_load();stock_load();">
                  <option value="">-- Select Department --</option>
                  <?php     $ss_warehouse_id='';//$this->session->userdata('ss_warehouse_id'); 
                            foreach ($department_list as $row)
                            {	
                                $sel='';
							    if($ss_warehouse_id==$row->m_department_id){
									$sel=' selected="selected"';
							}?>
                  <option value="<?php echo $row->m_department_id; ?>" <?php echo $sel ?> > <?php echo $row->m_department_name; ?> </option>
                  <?php }?>
                </select>
              </div>
            </div>
            
            
									<div class="col-sm-3">
										<div class="form-group">
											<label>TIME PROFILE * </label>
											<select id="time_profile_id" class="form-control" name="time_profile_id" onchange="products_load();">
												<option value="">-- ALL --</option>
												<?php
												foreach ($time_profile_list as $row) {	?>
													<option value="<?php echo $row['mtp_id']; ?>"> <?php echo $row['mtp_name']; ?> </option>
												<?php } ?>
											</select>
										</div>
									</div>
								<!--	<div class="col-sm-3">
										<div class="form-group">
											<label>DATE * </label>
											<input id="req_date" type="date" class="form-control" name="req_date" value="<?php echo date('Y-m-d'); ?>" onchange="products_load();">
										</div>
									</div>-->
								</div>
								<div id="error"></div>
								<table class="table table-bordered table-condensed table-hover table-striped dataTable" id="products_table">
									<thead>
										<tr>
											<th>NAME</th>
											<th>CODE</th>
											<th>BATCH CODE</th>
											<th>QTY</th>
											<th>DATE</th>
											<th>TIME PROFILE</th>
											<th class="col-sm-6">ACTION</th>
										</tr>
									</thead>
									<tfoot>
										<tr>
											<th>BATCH CODE</th>
											<th>CODE</th>
											<th>NAME</th>
											<th>QTY</th>
											<th>DATE</th>
											<th>TIME PROFILE</th>
											<th>ACTION</th>
										</tr>
									</tfoot>
								</table>
								<div class="col-sm-12">
									<h3>STOCK FOR PRODUCTION</h3>
									<table class="table table-bordered table-condensed table-hover table-striped dataTable" id="stock_table">
										<thead>
											<tr>
												<th>MATERIAL NAME</th>
												<th>MATERIAL CODE</th>
												<th>UOM</th>
												<th>STOCK</th>
											</tr>
										</thead>
										<tfoot>
											<tr>
												<th>MATERIAL NAME</th>
												<th>MATERIAL CODE</th>
												<th>UOM</th>
												<th>STOCK</th>
											</tr>
										</tfoot>
									</table>
								</div>
							</div>
						</div>
						<!-- end: DYNAMIC TABLE PANEL -->
					</div>
				</div>
				<!-- end grid -->
			</div>
			<!-- end: PAGE -->
		</div>
		<!-- end: MAIN CONTAINER -->
		<!-- start: FOOTER -->
		<div class="footer clearfix">
			<div class="footer-inner">
				2014 &copy; clip-one by cliptheme.
			</div>
			<div class="footer-items">
				<span class="go-top"><i class="clip-chevron-up"></i></span>
			</div>
		</div>
		<!-- end: FOOTER -->
		<!-- start: RIGHT SIDEBAR -->
		<!-- end: RIGHT SIDEBAR -->
		<div id="myModal" class="modal fade" role="dialog">
			<div class="modal-dialog">
				<!-- Modal content-->
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal">&times;</button>
						<h4 class="modal-title">Modal Header</h4>
					</div>
					<div class="modal-body">
						<p>Some text in the modal.</p>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					</div>
				</div>
			</div>
		</div>
		<!-- start ajax model -->
		<!-- end ajax model -->
		<!-- start: MAIN JAVASCRIPTS -->
		<?php $this->load->view("common/footer"); ?>
		<!-- end: MAIN JAVASCRIPTS -->
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/jquery.dataTables.min.js"></script>
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/dataTables.bootstrap.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/dataTables.buttons.min.js"></script>
		<script src="<?php echo asset_url(); ?>/plugins/select2/select2.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.print.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.flash.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/jszip.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/pdfmake.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/vfs_fonts.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.html5.min.js"></script>
		<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.print.min.js"></script>
		<script>
			jQuery(document).ready(function() {
				//TableData.init();
				products_load();
				stock_load();
			});
			var req;
			function products_load() {
				var warehouse_id = $('#warehouse_id').val();
				var time_profile_id = $('#time_profile_id').val();
				var department_id=$('#department_id').val();
				var req_date = $('#req_date').val();
				req = $('#products_table').DataTable({
					dom: 'Blfrtip',
					lengthMenu: [
						[10, 25, 50, -1],
						[10, 25, 50, "All"]
					],
					buttons: [{
							extend: 'copy',
							text: '<i class="fa fa-copy fa-2x"></i>',
							footer: true,
							exportOptions: [0, 1, 2, 3]
						},
						{
							extend: 'print',
							text: '<i class="fa fa-print fa-2x"></i>',
							footer: true,
							exportOptions: {
								columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
							},
							title: "<center>Products</center>"
						},
						{
							extend: 'excel',
							text: '<i class="far fa-file-excel fa-2x"></i>Excel',
							footer: true,
							exportOptions: {
								columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
							},
							title: "Products"
						},
						{
							extend: 'pdf',
							text: '<i class="far fa-file-pdf fa-2x"></i>PDF',
							footer: true,
							exportOptions: {
								columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
							},
							title: "Products"
						},
					],
					"serverSide": false,
					"ajax": "<?php echo base_url('manufacture_preparation/getListOngoingProduction?warehouse_id=') ?>" + warehouse_id + "&req_date=" + req_date + "&time_profile_id=" + time_profile_id+"&department_id="+department_id,
					"bDestroy": true,
					"iDisplayLength": 10
				});
			}
			function finish_production(id) {
				jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'manufacture_preparation/finishProduction?id=' ?>" + id,
					cache: false,
					data: {
						id: id
					},
					success: function(response) {
						displayNotice('page', 'Successfully Created !!');
						setTimeout(function() {
							products_load();
						}, 1000);
					}
				});
			}
			function print_stn(id) {
				u = location.href;
				t = document.title;
				window.open('<?php echo base_url() ?>pos/sale_details_stn_duplicate?sale_id=' + id, 'sharer', 'toolbar=0,status=0,width=626,height=700, left=10, top=10,scrollbars=yes');
				return false;
			}
			function approval_qc(id,pbi_qty) {
				//var status = 2;
				var gen_qty = $("#qc_qty_" + id).val();
				var defect_reason = 0;
				if(pbi_qty > gen_qty){
				    bootbox.prompt({
                        title: 'Select defect reason',
                        message: '<p>Please select an option below:</p>',
                        inputType: 'radio',
                        inputOptions: [
                            <?php foreach($defect_list as $l){
						      ?>
						        {
                                    text: '<?php echo $l->defect_desc; ?>',
                                    value: '<?php echo $l->defect_id; ?>'
                                },
						      <?php
                                } 
                              ?>
                        ],
                        callback: function (result) {
                            if(result !== null){
                                defect_reason = result;
                                send_req(id,gen_qty,defect_reason);
                            }
                        }
                    });
				}else{
				    send_req(id,gen_qty,defect_reason);
				}
			}
			function send_req(id,gen_qty,defect_reason){
			    jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'manufacture_preparation/setQcApproval?id=' ?>" + id,
					cache: false,
					data: {
						id: id,
						result: gen_qty,
						defect_reason:defect_reason
					},
					success: function(response) {
						displayNotice('page', 'Successfully Genarated !!');
						//*********************************************
						var obj = jQuery.parseJSON(response);
						products_load();
						if (obj.status == 0) {
							bootbox.alert(obj.validation);
						}
						//*************************************************
					}
				});
			}
			function reject_qc(id) {
				bootbox.confirm({
					title: "Are You Sure? ",
					message: "Do you want to Reject QC Approval ?.",
					buttons: {
						cancel: {
							label: '<i class="fa fa-times"></i> Cancel'
						},
						confirm: {
							label: '<i class="fa fa-check"></i> Yse Confirm'
						}
					},
					callback: function(result) {
						//console.log('This was logged in the callback: ' + result);
						//-----------------------------------------------------------
						if (result == true) {
							jQuery.ajax({
								type: "POST",
								url: "<?php echo base_url() . 'manufacture_preparation/setQcApprovalReject?id=' ?>" + id,
								cache: false,
								data: {
									id: id
								},
								success: function(response) {
									displayNotice('page', 'Successfully Rejected !!');
									//*********************************************
									var obj = jQuery.parseJSON(response);
									products_load();
									if (obj.status == 0) {
										bootbox.alert(obj.validation);
									}
									//*************************************************
								}
							});
						}
						//---------------------------------------------------------- 
					}
				});
			}
			function show_ingredients(id) {
				var data2 = "<h2>This is my message</h2>";
				jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'manufacture_preparation/getIngredients?id=' ?>" + id,
					cache: false,
					data: {
						id: id
					},
					success: function(response) {
						//	displayNotice('page','Successfully Rejected !!');
						//*********************************************
						var obj = jQuery.parseJSON(response);
						data2 = obj.data;
						var dialog = bootbox.dialog({
							title: 'BATCH PROFILE',
							message: data2
						});
						dialog.init(function() {
							setTimeout(function() {
								dialog.find('.bootbox-body').html('I was loaded after the dialog was shown!');
							}, 3000);
						});
						//alert(data2);
						//*************************************************
					}
				});
			}
			function stock_load() {
				var warehouse_id = $('#warehouse_id').val();
				var department_id = $('#department_id').val();
				req = $('#stock_table').DataTable({
					dom: 'Blfrtip',
					lengthMenu: [
						[10, 25, 50, -1],
						[10, 25, 50, "All"]
					],
					buttons: [{
							extend: 'copy',
							text: '<i class="fa fa-copy fa-2x"></i>',
							footer: true,
							exportOptions: [0, 1, 2, 3]
						},
						{
							extend: 'print',
							text: '<i class="fa fa-print fa-2x"></i>',
							footer: true,
							exportOptions: {
								columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
							},
							title: "<center>Products</center>"
						},
						{
							extend: 'excel',
							text: '<i class="far fa-file-excel fa-2x"></i>Excel',
							footer: true,
							exportOptions: {
								columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
							},
							title: "Products"
						},
						{
							extend: 'pdf',
							text: '<i class="far fa-file-pdf fa-2x"></i>PDF',
							footer: true,
							exportOptions: {
								columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
							},
							title: "Products"
						},
					],
					"serverSide": false,
					"ajax": "<?php echo base_url('manufacture_preparation/getProductionStock?warehouse_id=') ?>" + warehouse_id+"&department_id="+department_id,
					"bDestroy": true,
					"iDisplayLength": 50
				});
			}
			function start_production(id) {
				jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'manufacture_preparation/startProduction?id=' ?>" + id,
					cache: false,
					data: {
						id: id
					},
					success: function(response) {
						displayNotice('page', 'Successfully Created !!');
						setTimeout(function() {
							products_load();
							stock_load();
						}, 1000);
					}
				});
			}
			function req_aaddit(id) {
				var data2 = "<h2>This is my message</h2>";
				jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'manufacture_preparation/request_addtional_items?id=' ?>" + id,
					cache: false,
					data: {
						id: id
					},
					success: function(response) {
						//	displayNotice('page','Successfully Rejected !!');
						//*********************************************
						var obj = jQuery.parseJSON(response);
						data2 = obj.data;
						var dialog = bootbox.dialog({
							title: 'ADDITIONAL QTY',
							message: data2
						});
						dialog.init(function() {
							setTimeout(function() {
								dialog.find('.bootbox-body').html('I was loaded after the dialog was shown!');
							}, 3000);
						});
						//alert(data2);
						//*************************************************
					}
				});
			}
			$(document).on('submit', '#form_bulk_items', function(e) {
				e.preventDefault();
				$.ajax({
					type: 'post',
					url: '<?php echo base_url(); ?>manufacture_preparation/save_addtional_request_items',
					data: $('form').serialize(),
					success: function(data) {
						var obj = jQuery.parseJSON(data);
						if (obj.status == 1) {
							displayNotice('page', 'successfully added!');
							//window.location.reload(); 
							$("#form_bulk_items").trigger('reset');
							//$("#req_qty").blur();
							location.reload();
						} else {
							bootbox.alert({
								message: obj.validation,
								className: 'rubberBand animated'
							})
						}
					}
				});
			});
			
			
			
			
			
			function printDiv() 
{

  var divToPrint=document.getElementById('DivIdToPrint');

  var newWin=window.open('','Print-Window');

  newWin.document.open();

  newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');

  newWin.document.close();

  setTimeout(function(){newWin.close();},10);

}
		</script>
</body>
<!-- end: BODY -->
</html>