<?php $this->load->view("partial/header.php"); ?>
<?php
function rupiah($angka)
{

	$hasil_rupiah = "Rp " . number_format($angka, 2, ',', '.');
	return $hasil_rupiah;
}

function tgl_indo($tanggal)
{
	$bulan = array(
		1 => 'Januari',
		'Februari',
		'Maret',
		'April',
		'Mei',
		'Juni',
		'Juli',
		'Agustus',
		'September',
		'Oktober',
		'November',
		'Desember'
	);
	$pecahkan = explode('-', $tanggal);
	return $pecahkan[2] . ' ' . $bulan[(int) $pecahkan[1]] . ' ' . $pecahkan[0];
}
?>
<script>
	function exportTableToExcel(tableID, filename = '') {
		var downloadLink;
		var dataType = 'application/vnd.ms-excel';
		var tableSelect = document.getElementById(tableID);
		var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

		// Specify file name
		filename = filename ? filename + '.xls' : 'excel_data.xls';

		// Create download link element
		downloadLink = document.createElement("a");

		document.body.appendChild(downloadLink);

		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTML], {
				type: dataType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			// Create a link to the file
			downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

			// Setting the file name
			downloadLink.download = filename;

			//triggering the function
			downloadLink.click();
		}
		event.preventDefault()
	}
</script>

<body style="background-color: #B0B0B0">
	<!-- Start Sidebar -->
	<?php $this->load->view("partial/navbar.php"); ?>
	<div id="wrapper">
		<div id="content-wrapper">
			<div class="container-fluid">
				<div class="row" id="body-row ">
					<div class="col-md-12" style="color: white;padding: 30px">
						<div class="card" style="background-color: #343a40; padding-left: 10px;padding-right: 10px"><br>
							<form method="GET">
								<div class="form-group row">
									<label for="inputEmail3" class="col-sm-1 col-form-label">Tanggal</label>
									<div class="col-sm-2">
										<input type="date" class="form-control" name="tanggalawal" id="tanggalawal">
									</div>S/D
									<!-- <label for="inputEmail3" class="col-sm-1 col-form-label">Tanggal</label> -->
									<div class="col-sm-2">
										<input type="date" class="form-control formku" name="tanggalakhir" id="tanggalakhir">
									</div>
									<div class="col-sm-2">
										<button type="submit" class="btn btn-success">Cari</button>
										<button class="btn btn-warning" onclick="exportTableToExcel('example', 'list_data_in_out')">Export</button>
										<a href="<?= base_url('List') ?>" class="btn btn-danger">Reset</a>
									</div>
								</div><br>
							</form>
							<table class="table text-center" id="example">
								<thead>
									<tr>
										<th>Tanggal</th>
										<th>No. Kendaraan</th>
										<th>Nama Supir</th>
										<th>Bruto</th>
										<th>Tarra</th>
										<th>Kotor</th>
										<th>Pot (3.7%)</th>
										<th>Netto Bayar</th>
										<th>Harga</th>
										<th>Total Bayar</th>
									</tr>
								</thead>
								<tbody>
									<?php
									foreach ($listdata as $val) {
										$tgl = explode(" ", $val['createdate']);
										$tgl_indo = tgl_indo($tgl[0]);
										$potongan = ceil($val['beratmuatan'] * $val['potongan'] / 100);
										$netto = $val['beratmuatan'] - $potongan;
									?>
										<tr>
											<td scope="row"><?= $tgl_indo ?></td>
											<td><?= $val['nopolisi'] ?></td>
											<td><?= $val['namasupir'] ?></td>
											<td><?= $val['berattotal'] ?></td>
											<td><?= $val['beratkosong'] ?></td>
											<td><?= $val['beratmuatan'] ?></td>
											<td><?= ceil($val['beratmuatan'] * $val['potongan'] / 100); ?></td>
											<td><?= $netto ?></td>
											<td><?= $val['harga_tbs']; ?></td>
											<td><?= rupiah($val['harga_tbs'] * $netto) ?></td>
										</tr>
									<?php } ?>
								</tbody>
							</table>


						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script src="<?= base_url() ?>assets/js/jquery.dataTables.min.js"></script>
	<script src="<?= base_url() ?>assets/js/dataTables.bootstrap4.min.js"></script>
	<script>
		$(document).ready(function() {
			$('#example').DataTable({
				"scrollY": "400px",
				"scrollCollapse": true,
			});
		});
	</script>