<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<link href="<?=base_url();?>/assets/style.css" rel="stylesheet">
	<!-- <script>
        function closeWindow() {
            setTimeout(function() {
                window.close();
            }, 500); // 2000 milliseconds = 2 seconds
        }
    </script> -->
</head>

<body class='bg-white' onload="closeWindow()">

	<h4 class="mt-2 text-center fw-bold"> Data Activity Periode <?=$opendate . ' to ' . $closedate;?> </h4>

	<div class='text-center'>
		<button id='saveButton' class='btn btn-success' onclick="exportToExcel()">Simpan Excel</button>	
	</div>
	
	<div class="row p-5">
		<div class="col-12">	
			<div class="table-responsive">
				<table id="myTable" class="table table-bordered">
					<thead>
						<tr>
							<th scope="col">No</th>
							<th scope="col">Activity ID</th>
							<th scope="col">Owner</th>
							<th scope="col">Activity Type</th>
							<th scope="col">Activity By</th>
							<th scope="col">Site</th>
							<th scope="col">Subject</th>
							<th scope="col">Consumables</th>
							<th scope="col">Transactions</th>
							<th scope="col">Product</th>
							<th scope="col">Software Version</th>
							<th scope="col">Serial Number</th>
							<th scope="col">Media</th>
							<!-- <th scope="col">Reference</th> -->
							<th scope="col">Action By</th>
							<th scope="col">Report From</th>
							<th scope="col">Report Date</th>
							<th scope="col">Open Date</th>
							<th scope="col">Close Date</th>


						</tr>
					</thead>
					<tbody>
						<?php
						// var_dump($export);die(); 
						$no=1;
						foreach ($export as $data) { 
							$actid = $data['actid'];

							if ($data['actby'] == 'P') {
								$actby = "Product";
							} else if ($data['actby'] == 'V') {
								$actby = "Vendor";
							} else if ($data['actby'] == 'C') {
								$actby = "Consumable / Reagent";
							} else if ($data['actby'] == 'O') {
								$actby = "Other";
							} else {
								$actby = "-";
							}

							if($data['action'] == 'r'){
								$action = 'Remote';
							} else if($data['action'] == 'p'){
								$action = 'Phone';
							} else if($data['action'] == 'o'){
								$action = 'On Site';
							} else {
								$action = '-';
							}

							array('WA'=>'Whatsapp', 'PH'=> 'Phone', 'VB' => 'Verbal', 'LT'=>'Letter', 'CRM'=>'Direct CRM', 'SKP'=>'Skype');

							if($data['media'] == 'WA'){
								$media = 'Whatsapp';
							} else if ($data['media'] == 'PH'){
								$media = 'Phone';
							} else if ($data['media'] == 'VB'){
								$media = 'Verbal';
							} else if ($data['media'] == 'LT'){
								$media = 'Letter';
							} else if ($data['media'] == 'CRM'){
								$media = 'Direct CRM';
							} else if ($data['media'] == 'SKP'){
								$media = 'Skype';
							} else {
								$media = '-';
							}
						?>

						<tr>
							<td><?=$no++;?></td>
							<td><?=$data['actid'];?></td>
							<td><?=$data['firstname'];?> <?=$data['lastname'];?></td>
							<td><?=$data['fulltext'];?></td>
							<td><?=$actby?></td>
							<td><?=$data['sitename'];?></td>
							<td><?=$data['subject'];?></td>
							
							<td>
								<?php foreach($consumables[$data['actid']] as $datac) {
									$catalognumber = $datac['catalognumber'];
									$productname = $datac['productname'];
									$lotnumber = $datac['lotnumber'];
								?>
								Catalog Number : <?=$catalognumber?>
								<br>
								Product Name : <?=$productname?>
								<br>
								Lot Number : <?=$lotnumber == '' ? '-' : $lotnumber?>
								<br>
								<br>
								<?php } ?>
							</td>

							<td>
								<?php foreach($invtrans[$data['actid']] as $datai) {
									$catalognumber = $datai['catalognumber'];
									$productname = $datai['productname'];
									$lotnumber = $datai['lotnumber'];
									$qty = $datai['qty'];
									$unit = $datai['unit'];
								?>
								Catalog Number : <?=$catalognumber?>
								<br>
								Product Name : <?=$productname?>
								<br>
								Lot Number : <?=$lotnumber == '' ? '-' : $lotnumber?> 
								<br>
								Qty : <?=$qty == '' ? '-' : $qty?>
								<br>
								Unit : <?=$unit == '' ? '-' : $unit?>
								<br><br>
								<?php } ?>
							</td>

							<td><?=$data['productname'];?></td>
							<td><?=$data['swversion'];?></td>
							<td><?=$data['productnumber'];?></td>
							<td><?=$media;?></td>
							<td><?=$action;?></td>
							<td><?=$data['reportfrom'];?></td>
							<td><?=$data['reportdate'];?></td>
							<td><?=$data['opendate'];?></td>
							<td><?=$data['closedate'];?></td>
						</tr>

						<?php } ?>

					</tbody>
				</table>
			</div>
		</div>
		
	</div>
<script src="<?=base_url();?>/assets/xlsx.mini.min.js"></script>
<script>

window.onload = function() {
    document.getElementById('saveButton').click();
    window.close();
}

function exportToExcel() {
	const data = document.getElementById("myTable");
	var ws = XLSX.utils.table_to_sheet(data);
	var wb = XLSX.utils.book_new();
	XLSX.utils.book_append_sheet(wb, ws, "Sheet 1");
	XLSX.writeFile(wb, "ActivityList.xlsx");
}	
</script>
</body>

</html>