<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->
<style type="text/css">
	
</style>
<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;
	}
	/**/
	.input-sm {
		width: 70px;
	}
	input:read-only {
		background: inherit;
		border: none;
	}
</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'); ?>">
									DASHBORD
								</a>
							</li>
							<li>
								<a href="#">
									ADMINSTRATION
									<?php $cmn = new $this->common_model();
									if ($cmn->check_permission(113, $this->session->userdata('ss_group_id'), 1)) {
										echo "";
									} ?>
								</a>
							</li>
							<li class="active">
								NEW PRICE
							</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>NEW PRICE</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 PRODUCT
							</div>
						<!--	<form id="mat_form">-->
								<div class="panel-body">
									<div id="error"></div>
									
									<?php if($status==0){  ?>
									<div class="panel-footer text-center">
								    <button class="btn btn-primary" type="button" onclick="create_new_price_list()">NEW</button>
								</div>
								<?php } ?>
								
								
									<?php if($status==1){  ?>
									<table class="table table-bordered table-condensed table-hover table-striped dataTable" id="products_table">
										<thead>
											<tr>
												<th class="col-md-1">Product Code</th>
												<th class="col-md-2">Product Name</th>
												<?php
												foreach ($price_type_list as $sc) {
													echo "<th>" . $sc['pri_type_name'] . "</th>";
												}
												?>
												<th>ACTION</th>
											</tr>
										</thead>
										<tboady>
										    <?php 
										    $count=0;
										    foreach($data_list as $row){
										    $count++;
										    ?>
										    <tr>
										         <form name="save_data" id="save_data_<?php echo $count;?>" role="form"> 
										        <?php 
										        //$row_count=count($row);
										        foreach($row as $r){?>
										           <td><?php print_r($r);?></td> 
										        <?php    
										        }
										        
										        ?>
										        </form>
										        
										        
										    </tr>
										    
										    
										  <?php } ?>  
										</tboady>
										<tfoot>
											<tr>
												<th class="col-md-1">Product Code</th>
												<th class="col-md-2">Product Name</th>
												<?php
												foreach ($price_type_list as $sc) {
													echo "<th>" . $sc['pri_type_name'] . "</th>";
												}
												?>
												<th>ACTION</th>
											</tr>
											</tr>
										</tfoot>
									</table>
									
								
							
							<!--	<div class="panel-footer text-right">
								    <button class="btn btn-default" type="submit">SUBMIT</button>
								   
								    	</div>-->
								    	
								    	 <?php } ?>
								</div>
							<!--</form>-->
						</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="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title">Event Management</h4>
					</div>
					<div class="modal-body"></div>
					<div class="modal-footer">
						<button type="button" data-dismiss="modal" class="btn btn-light-grey">
							Close
						</button>
						<button type="button" class="btn btn-danger remove-event no-display">
							<i class='fa fa-trash-o'></i> Delete Event
						</button>
						<button type='submit' class='btn btn-success save-event'>
							<i class='fa fa-check'></i> Save
						</button>
					</div>
				</div>
			</div>
		</div>
		<!-- start ajax model -->
		<div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
		<!-- 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 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();
				cal_qty();
			});
			var req;
			function products_load() {
				req = $('#products_table').DataTable({
					dom: 'Blfrtip',
					//lengthMenu: [[10, 25, 50, -1 ], [10, 25, 50, "All"]],
					/*"bPaginate": false,*/
					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": true,
					"ajax": "<?php echo base_url('production_process/loadGetProductMaterialListAvailability') ?>",
					"bDestroy": true,
					"iDisplayLength": 10,
					"fnFooterCallback": function(nRow, aaData, iStart, iEnd, aiDisplay) {
						setTimeout(function() {
							$('.total_order').each(function() {
								var parentElement = $(this);
								var value = parentElement.val();
								var class_name = $(this).data('current_qty');
								var total_order_qty = 0;
								$('.' + class_name).each(function() {
									total_order_qty += parseInt($(this).html());
								});
								$(parentElement).html(total_order_qty);
							});
							$('.input-sm').each(function() {
								var input_group = $(this).data('class_name');
								var final_qty = $(this).data('final_qty');
								var finalize_order_qty = 0;
								$('.' + input_group).each(function() {
									if (this.value > 0)
										finalize_order_qty += parseInt(this.value);
								});
								$('#' + final_qty).val(finalize_order_qty);
							});
							/*listners*/
							$('.input-sm').change(function() {
								var input_group = $(this).data('class_name');
								var final_qty = $(this).data('final_qty');
								var finalize_order_qty = 0;
								$('.' + input_group).each(function() {
									if (this.value > 0)
										finalize_order_qty += parseInt(this.value);
								});
								$('#' + final_qty).val(finalize_order_qty);
							});
						}, 200);
					}
				});
			}
			function check_qty(elem,input_id,mat_code) {
			
		
	
		
		
				var check_val = $('#'+input_id).val();
				$.ajax({
                    type: "POST",
                    data: {
                        check_val:check_val,
                        mat_code :mat_code,
                    },
                    dataType: 'json',
                    url: "<?php echo base_url(); ?>product_material/check_qty",
                    success: function(data) {
                        console.log(data);
                        $(elem).parents('tr:first').find('td').css("background-color", "red");
                        /*if (data.success) {
                            //bootbox.alert(data.data.disMsg);
                        } else {
                            //bootbox.alert(data.data.disMsg);
                        }*/
                    },
                    error: function(data) {
                        bootbox.alert(data.responseText);
                        console.log(data.responseText);
                    }
                });
			}
			$("#mat_form").on("submit", function(event) {
				event.preventDefault();
				$.ajax({
                    type: "POST",
                    data: $(this).serialize(),
                    dataType: 'json',
                    url: "<?php echo base_url(); ?>product_material/save_qty",
                    success: function(data) {
                        console.log(data);
                        bootbox.alert(JSON.stringify(data));
                    },
                    error: function(data) {
                        bootbox.alert(data.responseText);
                        console.log(data.responseText);
                    }
                });
			});
			function print_barcode(product_id) {
				window.open('products/single_barcode/' + product_id, 'sma_popup', 'width=900,height=600,scrollbars=yes,menubar=yes,status=no,resizable=yes,screenx=0,screeny=0');
			}
			function print_barcode_test(product_id) {
				window.open('products/single_barcode_test/' + product_id, 'sma_popup2', 'width=900,height=600,scrollbars=yes,menubar=yes,status=no,resizable=yes,screenx=0,screeny=0');
			}
			function product_delete(product_id) {
				return false;
				var r = confirm("Are you sure you want to delete this item !");
				if (r == true) {
					products_load();
					$.ajax({
						url: "<?php echo base_url('products/delete_product'); ?>",
						type: "POST",
						data: {
							product_id: product_id
						},
						success: function(data) {
							var obj = jQuery.parseJSON(data);
							if (obj.status == 0) {
								$('div#error').html('<div class="alert alert-block alert-danger fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4 class="alert-heading"><i class="fa fa-times-circle"></i> Error!</h4>' + obj.validation + '</div>');
							} else {
								set_message('Product notice!', 'Product permanent deleted successfully');
								products_load();
							};
						}
					});
				}
			}
		</script>
		<script>
			function break_product(product_id) {
				//alert(product_id);
				//return false;
				// create the backdrop and wait for next modal to be triggered
				$('body').modalmanager('loading');
				setTimeout(function() {
					$modal.load('<?php echo base_url("bulk_break/load_process"); ?>/' + product_id, '', function() {
						$modal.modal();
					});
				}, 1000);
			}
			function enable_product(prouct_id) {
				jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'product_material/enableProductMaterial?product_id=' ?>" + prouct_id,
					cache: false,
					success: function(response) {
						displayNotice('page', 'Successfully Enabled !!');
						setTimeout(function() {
							//products_load();
							req.ajax.reload();
						}, 1000);
					}
				});
			}
			function disable_product(prouct_id) {
				jQuery.ajax({
					type: "POST",
					url: "<?php echo base_url() . 'product_material/disableProductMaterial?product_id=' ?>" + prouct_id,
					cache: false,
					success: function(response) {
						//		var price = $('#'+count).val();
						//		var twoPlacedFloat = parseFloat(price).toFixed(2)
						//		$('#'+count).val(twoPlacedFloat);
						displayNotice('page', 'Successfully Disabled !!');
						//		setTimeout(resetfun(),30000);
						//location.reload();
						setTimeout(function() {
							req.ajax.reload();
						}, 1000);
						//products_load();
					}
				});
			}
			function create_new_price_list() {
				bootbox.confirm({
					title: "CREATE NEW PRICE LIST",
					message: "Do you want to create new price list.",
					buttons: {
						cancel: {
							label: '<i class="fa fa-times"></i> Cancel'
						},
						confirm: {
							label: '<i class="fa fa-check"></i> 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() . 'price_adminstration/create_new_prodcut_price_list' ?>",
								data:{id:"123"},
								cache: false,
								success: function(response) {
								//	displayNotice('page', 'Successfully Added !!');
									//*********************************************
								    //code block after result came
								    
								    var obj = jQuery.parseJSON( response );
								    if(obj.status==0){
								        bootbox.alert(obj.validation);
								    }else{
								        location.reload();
								    }
									//*************************************************
								}
							});
						}
						//---------------------------------------------------------- 
					}
				});
			}
			
			
			function cal_qty(){
			    	setTimeout(function() {
							$('.total_order').each(function() {
								var parentElement = $(this);
								var value = parentElement.val();
								var class_name = $(this).data('current_qty');
								var total_order_qty = 0;
								$('.' + class_name).each(function() {
									total_order_qty += parseInt($(this).html());
								});
								$(parentElement).html(total_order_qty);
							});
							$('.input-sm').each(function() {
								var input_group = $(this).data('class_name');
								var final_qty = $(this).data('final_qty');
								var finalize_order_qty = 0;
								$('.' + input_group).each(function() {
									if (this.value > 0)
										finalize_order_qty += parseInt(this.value);
								});
								$('#' + final_qty).val(finalize_order_qty);
							});
							/*listners*/
							$('.input-sm').change(function() {
								var input_group = $(this).data('class_name');
								var final_qty = $(this).data('final_qty');
								var finalize_order_qty = 0;
								$('.' + input_group).each(function() {
									if (this.value > 0)
										finalize_order_qty += parseInt(this.value);
								});
								$('#' + final_qty).val(finalize_order_qty);
							});
						}, 200);
			}
			
			
		

$(document).delegate('form', 'submit', function(event) {
    event.preventDefault();
    var $form = $(this);
    var id = $form.attr('id');
    var data = $form.serialize();
    //---------------------------
    bootbox.confirm({
    title: "Are you sure?",
    message: "Do you want to update this product price ?",
    buttons: {
        cancel: {
            label: '<i class="fa fa-times"></i> Cancel'
        },
        confirm: {
            label: '<i class="fa fa-check"></i> Yes'
        }
    },
    callback: function (result) {
        //console.log('This was logged in the callback: ' + result);
        //-----------------------------------------------------------
        if(result==true){
        jQuery.ajax({
		type: "POST",
		url: "<?php echo base_url().'price_adminstration/update_product_price?id=1'?>",
		cache: false,
		data: data,
		success: function(response)
       {
	
		//*********************************************
		var obj = jQuery.parseJSON( response );
			displayNotice('page',obj.validation);
         //loadGrid();  
         //loadGrid2();
		//*************************************************
	    }
		});
        }
       //---------------------------------------------------------- 
    }
});
    //----------------------------
    // ...
});	
			
			
		</script>
</body>
<!-- end: BODY -->
</html>