<script src="<?php echo site_url(); ?>assets/js/plugin/datatables1_10_7/jquery.dataTables.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.colReorder.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.colVis.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.tableTools.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatable-responsive/datatables.responsive.min.js"></script>

<!-- MAIN PANEL -->
<script src="https://cdn.lordicon.com/libs/mssddfmo/lord-icon-2.1.0.js"></script>
<div id="main p-0" role="main">
	<!-- <div class="header-bg shadow-sm p-0" style="background: url(https://images.pexels.com/photos/1475033/pexels-photo-1475033.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)"> -->
	<!-- <div class="header-bg shadow-lg p-0" style="background: url(https://images.pexels.com/photos/1487834/pexels-photo-1487834.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)"></div> -->
	<div id="content" style=" padding-top: 0px; ">
		<div class="container">
			<div class="row mt-3 justify-content-center">
				<div class="header_invoice_wrapper shadow">
					<div class="fs-1 w-25 text-white d-flex align-items-center"><?php echo $this->lang->line('all_products'); ?></div class="fs-1">
					<div class="new_invoice_btn">
						<a type="button" class="shadow-lg add_invoice_button" href="<?php echo site_url(); ?>admin/products/productForm/" target="_blank" title="<?php echo $this->lang->line('new_product'); ?>">
							<lord-icon src="https://cdn.lordicon.com/mecwbjnp.json" trigger="loop" delay="1000" colors="primary:#356359,secondary:#00bcd4" style="width:32px;height:32px">
							</lord-icon>
							<span class="pt-1 text-dark">
								<?php echo $this->lang->line('new_product'); ?>
							</span>
						</a>
						<a type="button" class="shadow-lg excel_exporter cancel_invoice p-2" style="width: 50px;" id="product_traffic" class="btn btn-danger btn-circle">
							<i class="fa fa-exchange-alt"></i></a>
						<button type="button" class="shadow-lg excel_exporter" data-bs-toggle="modal" data-bs-target="#ExcelExportModal">
							<i class="fas fa-file-excel text-success px-2"></i>
							<!-- <lord-icon src="https://cdn.lordicon.com/nocovwne.json" trigger="loop" delay="1000" colors="primary:#356359,secondary:#00bcd4" style="width:32px;height:32px">
                                </lord-icon> -->
							<!-- <span class="pt-1 text-dark">
                                    Excel
                                </span> -->
						</button> 
						<button type="button" class="shadow-lg excel_exporter view_invoice p-2" style="width: 50px;" data-bs-toggle="modal" id="viewInvoiceButton" rel-invoice="-1" data-bs-target="#DeleteInvoice">
							<i class="pt-1 me-1 fas fa-eye text-success"></i>
						</button>
					</div>
				</div>
				<div class="row">
					<hr class="bg-white mt-5" style="border-radius: 30px; padding: 2px;" />
					<div class="table-responsive card card-body">
						<table class="table table-sm" id="dt_basic2">
							<thead>
							    <tr>
								<th>#</th>
    								<th><?php echo $this->lang->line('name'); ?></th>
    								<th><?php echo $this->lang->line('price'); ?></th>
    								<th><?php echo $this->lang->line('sku'); ?></th>
    								<th><?php echo $this->lang->line('category'); ?></th>
    								<th colspan="2"><?php echo $this->lang->line('quantity'); ?></th>
    								<?php foreach ($warehouse as $house) { ?>
    									<th colspan="2"> <?php echo $house->Name ?></th>
    								<?php }; ?>
    								<th><?php echo $this->lang->line('all'); ?> <?php echo $this->lang->line('warehouses'); ?></th>
    								<th><?php echo $this->lang->line('orders'); ?></th>
    								<th><?php echo $this->lang->line('available'); ?></th>
    								<th><?php echo $this->lang->line('status'); ?></th>
    								<th><?php echo $this->lang->line('producer'); ?></th>
    								<th><?php echo $this->lang->line('picture'); ?></th>
								</tr>
								<tr style=" font-size: 10px; ">
								    <th colspan="5"></th>
								    <th><?php echo $this->lang->line('quantity'); ?></th>
								    <th><?php echo $this->lang->line('cartoon'); ?></th>
								    <?php foreach ($warehouse as $house) { ?>
								        <th><?php echo $this->lang->line('quantity'); ?></th>
								        <th><?php echo $this->lang->line('cartoon'); ?></th>
								    <?php }; ?>
								    <th colspan="6"></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($products as $product) { ?>
									<tr rel-product="<?php echo $product->id; ?>">
										<td><?php echo $product->id; ?></td>
										<td><?php echo $product->name; ?></td>
										<td><?php echo $product->price; ?></td>
										<td><?php echo $product->code; ?></td>
										<td><?php echo $product->category_id; ?></td>
										<td><?php echo $product->quantity; ?></td>
										<td><?php echo ($product->cartoon_quantity != 0 ) ? $product->quantity / $product->cartoon_quantity : 0; ?></td>
										<?php
										$all_stock = $product->quantity;
										foreach ($warehouse as $house) { ?>

											<td>
												<?php if (isset($product->all_units_stores[$house->id])) { ?>
													<?php echo $product->all_units_stores[$house->id]->AllUnits;
													$product_current_unit = $product->all_units_stores[$house->id]->AllUnits;
													$all_stock += $product->all_units_stores[$house->id]->AllUnits; ?>
												<?php } else { ?>
													<?php $product_current_unit = 0; echo 0; ?>
												<?php }; ?>
											</td>
											<td><?php echo ($product->cartoon_quantity > 0 && $product_current_unit > 0) ? $product_current_unit / $product->cartoon_quantity : 0; ?></td>
										<?php }; ?>
										<td><?php echo $all_stock ?></td>
										<td><?php echo $product->all_units->AllUnits; ?></td>
										<?php $left_item = $all_stock - $product->all_units->AllUnits; ?>
										<td><?php echo $left_item ?></td>
										<td><?php echo $product->status; ?></td>
										<td><?php echo $product->manufacturer_id; ?></td>
										<td><?php echo $product->main_image; ?></td>
									</tr>
								<?php }; ?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="ExcelExportModal" tabindex="-1" aria-labelledby="ExcelExportModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content position-relative p-3" style="background-color: #f5f5f5;">
			<!-- <form action="no-action" method="POST" id="search_form"> -->
			<div class="modal-body" style="background-color: #f5f5f5;">
				<div class="close_modal" id="close_ExportModal" rel-export="true">
					<i class="fas fa-times-circle text-danger"></i>
				</div>
				<div class="d-flex flex-column justify-content-center align-items-center w-75 excel-container">
					<div class="excelTitle">
						Filter export by :
					</div>
					<div class="ExcelFilteration">
						<div class="label-excel-wrapper">
							<input type="checkbox" name="exportByCustome" id="exportByCustomer" rel-exportby="customer">
							<label for="exportByCustomer">By Customer:</label>
						</div>
						<div class="exportChoice_wrapper">
							<select name="" id="selectCustomer" class="w-100"></select>
						</div>
					</div>
					<hr class="w-100 mx-auto bg-success">
					<div class="ExcelFilteration">
						<div class="label-excel-wrapper">
							<input type="checkbox" name="exportByInvoice" id="exportByInvoice" rel-exportby="invoice">
							<label for="exportByInvoice">By Invoice:</label>
						</div>
						<div class="exportChoice_wrapper">
							<select name="" id="selectInvoiceFrom" class="w-100 mb-2"></select>
							<select name="" id="selectInvoiceTo" class="w-100"></select>
						</div>
					</div>
					<hr class="w-100 mx-auto bg-success">
					<div class="ExcelFilteration">
						<div class="label-excel-wrapper">
							<input type="checkbox" name="exportByDate" id="exportByDate" rel-exportby="date">
							<label for="exportByDate">By Date:</label>
						</div>
						<div class="exportChoice_wrapper">
							<input type="text" placeholder="From Date" name="invoice_date" id="invoiceDateFrom" class="datepicker form-control" />
							<input type="text" placeholder="To Date" name="invoice_date" id="invoiceDateTo" class="datepicker form-control" />
						</div>
					</div>
					<div class="excel_execute w-100 mt-3">
						<button class="btn btn-primary btn-sm p-1 float-start" id="buildExcel">
							Export
						</button>
					</div>
				</div>
			</div>
			<!-- </form> -->
		</div>
	</div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.5.2/bootbox.min.js" integrity="sha512-RdSPYh1WA6BF0RhpisYJVYkOyTzK4HwofJ3Q7ivt/jkpW6Vc8AurL1R+4AUcvn9IwEKAPm/fk7qFZW3OuiUDeg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css">
<script>
	<?php
	$url = '';
	if ($this->session->userdata('site_lang') == 'hebrew') {
		$url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
	} else if ($this->session->userdata('site_lang') == 'arabic') {
		$url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Arabic.json';
	} else if ($this->session->userdata('site_lang') == 'english') {
		$url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json';
	} else {
		$url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
	}
	?>
	$(document).ready(function() {
		$('#dt_basic22222').DataTable({
			"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'dt-buttons col-sm-6 col-xs-12 hidden-xs'l>r>" +
				"t" +
				"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
			"buttons": [],
			"processing": false,
			"bStateSave": false,
			"dom": 'Blfrtip',
			"pagingType": "full_numbers",
			"lengthMenu": [
				[10, 25, 50, -1],
				[10, 25, 50, "הכל"]
			],
			"pageLength": 20,
			"sPaginationType": "full_numbers",
			"iDisplayLength": 10,
			//"sScrollY": 500,
			//"bScrollCollapse": true,
            //"scrollX": true,
            "language": {
                "url": "<?php echo $url; ?>"
			},
			orderCellsTop: true,
			fixedHeader: false,
			searching: false
		});
		$(document).on('click', '#dt_basic2 tbody tr', function() {
			let elem = $(this);
			let rowAttr = elem.attr('rel-product');
			$('tr').css('background-color', "white");
			elem.css('background-color', "#e3edff")
			$('#deleteInvoiceButton').css('border-color', "#e9200c");
			$('#viewInvoiceButton').css('border-color', "#27b980");

			$('#deleteInvoiceButton').attr('rel-product', rowAttr);
			$('#viewInvoiceButton').attr('rel-product', rowAttr);
			$('#product_traffic').attr('rel-product', rowAttr);
		});
		$(document).on('click', '#product_traffic', function() {
			let elem = $(this);
			console.log(elem.attr('rel-product'));
			if (elem.attr("rel-product") === "-1") {
				bootbox.alert("Please Select a product from the table below to continue");
			} else {
				let id = elem.attr('rel-product');
				window.open(`<?php echo site_url(); ?>admin/products/productTraffic/${id}`, "_blank");
			}
		});

        $(document).on('dblclick', '#dt_basic2 tbody tr', function() {
            let elem = $(this);
            let id = elem.attr('rel-product');
            window.open(`<?php echo site_url(); ?>admin/products/productForm/${id}`, "_blank");
        });
	});
</script>
<style>
    table#dt_basic2 {
        width: 100% !important;
    }
</style>