<?= $this->extend('layouts/main.php') ?>

<?= $this->section('content') ?>
<div class="page-wrapper">
	<?php if (session()->get('msg')) : ?>
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        <strong><?= session()->getFlashdata('msg'); ?></strong>
        </div>
    <?php endif; ?>
	<div class="container-fluid">
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Transaction List</h4>
			</div>					
		</div>
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-body">
						<form method='post' >
							<div class="accordion accordion-flush mb-3" id="accordionForm">
								<div class="accordion-item">
									<h2 class="accordion-header" id="headingForm">
									<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseForm" 
										aria-expanded="false" aria-controls="collapseForm" style='padding:5px;'>
										<span class="me-3"> Filter </span>
									</button>
									</h2>
									<div id="collapseForm" class="accordion-collapse collapse show bg-white" aria-labelledby="headingForm">
										<div class="accordion-body ps-0">	
											<div class="row mb-2">
												<div class="col">
													<div class="input-group input-group-sm">
														<span class="input-group-text d-flex align-items-center justify-content-center" style="width: 80px;">Date</span>
														<input class="form-control form-control-sm" type="date" name="date1" value="<?= $date1;?>" onfocus="this.showPicker()"/>
														<span class="input-group-text d-flex align-items-center justify-content-center">-</span>
														<input class="form-control form-control-sm" type="date" name="date2" value="<?= $date2;?>" onfocus="this.showPicker()"/>
													</div>
												</div>
											</div>											
											<button type="submit" class="btn btn-success btn-sm text-white float-end"> <i class="fa fa-check"></i> Submit</button>
										</div>
									</div>
								</div>
							</div>							
						</form>
					</div>
				</div>
			</div>
		</div>
		
		<?php if(isset($invtrans)) { ?>
		<div class="row mt-2">
      		<div class="col-12">
				<div class="card">
					<div class="card-body">
						<button class='btn btn-sm btn-info' onclick='exportToExcel()'>Export to excel</button>
						<div class="table-responsive">
							<table id="myTable" class="table small display border">
								<thead class="table-info">
								</thead>
								<tbody>
								<?php
								$actid1 = '';
								foreach($invtrans as $data) {
									$actid = $data['actid'];
									$subject = $data['subject'];
									$username = $data['username'];
									$catalognumber = $data['catalognumber'];
									$productname = $data['productname'];
									$qty = $data['qty'];
									$unit = $data['unit'];
									$lotnumber = $data['lotnumber'];
									$origin = $data['origin'];
									$dest = $data['dest'];
									if($actid != $actid1) {
										echo "
										<tr><td colspan='7'></td></tr>
										<tr class='border-primary p-3'> <td colspan='6'>$actid - $subject ($username)</td> </tr>";
										$actid1 = $actid;
									} 
									echo "<tr> <td>$catalognumber</td> <td>$productname</td> <td>$qty</td> <td>$unit</td> <td>$lotnumber</td> <td>$origin</td> <td>$dest</td>";
								}
								?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php } ?>
	</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
<!-- SCRIPT DATATABLES -->
$(function () {
	$('#myTable').DataTable({
		"order" : [],
		"pageLength" : 50,
		"lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ]
	});
});

function exportToExcel() {
	var tableElement = document.getElementById("myTable");
	var wb = XLSX.utils.book_new();
	var ws = XLSX.utils.table_to_sheet(tableElement);
	XLSX.utils.book_append_sheet(wb, ws, "My Data");
	XLSX.writeFile(wb, "reportTransactions.xlsx");
}

</script>

<?= $this->endSection() ?>