<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laporan PMG</title>
<style>
body{font-family:Arial,Helvetica,sans-serif;margin:20px;color:#16202e;}
.toolbar{margin-bottom:12px;}
.toolbar input,.toolbar select,.toolbar button,.toolbar a{padding:6px 8px;border:1px solid #bbb;border-radius:6px;font-size:12px;background:#fff;text-decoration:none;}
.kpis{display:flex;gap:10px;flex-wrap:wrap;margin:12px 0;}
.kpi{border:1px solid #d7dee8;border-radius:10px;padding:8px 14px;min-width:110px;background:#f8fbf9;}
.kpi .val{font-size:20px;font-weight:800;}
.kpi .lbl{font-size:11px;font-weight:700;color:#5b6b7d;}
table{border-collapse:collapse;width:100%;font-size:11.5px;}
th,td{border:1px solid #9aa7b5;padding:5px 7px;vertical-align:top;text-align:left;}
th{background:#eef3f7;}
h1{font-size:18px;margin:0 0 4px;}
.sub{font-size:11.5px;color:#5b6b7d;margin-bottom:10px;}
@media print {.no-print{display:none !important;}}
</style>
</head>
<body>
<div class="toolbar no-print">
	<form method="get" style="display:flex;gap:8px;flex-wrap:wrap;align-items:center;">
		<label>Dari <input type="date" name="date_1" value="<?php echo htmlspecialchars($q1); ?>"></label>
		<label>s/d <input type="date" name="date_2" value="<?php echo htmlspecialchars($q2); ?>"></label>
		<label>Outlet
			<select name="idoutlet">
				<option value="">Semua Outlet</option>
				<?php foreach ($outlets as $o): ?>
				<option value="<?php echo (int)$o->idoutlet; ?>" <?php echo ((string)$idoutlet === (string)$o->idoutlet) ? 'selected' : ''; ?>><?php echo htmlspecialchars($o->outlet_name); ?></option>
				<?php endforeach; ?>
			</select>
		</label>
		<button type="submit">Tampilkan</button>
		<button type="button" onclick="window.print()">&#128424; Cetak</button>
		<a href="<?php echo app_url(); ?>pmg/admin_list">Ke Persetujuan</a>
	</form>
</div>

<h1>&#128204; LAPORAN PMG — PENGAJUAN MARKER PENGGANTI</h1>
<div class="sub">Periode <?php echo htmlspecialchars($q1); ?> s/d <?php echo htmlspecialchars($q2); ?> · Dicetak: <?php echo $tanggal_cetak; ?> · Khusus administrator</div>

<div class="kpis">
	<div class="kpi"><div class="val"><?php echo $g['total']; ?></div><div class="lbl">TOTAL PENGAJUAN</div></div>
	<div class="kpi"><div class="val"><?php echo $g['menunggu']; ?></div><div class="lbl">MENUNGGU</div></div>
	<div class="kpi"><div class="val"><?php echo $g['disetujui']; ?></div><div class="lbl">DISETUJUI</div></div>
	<div class="kpi"><div class="val"><?php echo $g['ditolak']; ?></div><div class="lbl">DITOLAK</div></div>
	<div class="kpi"><div class="val"><?php echo $g['dicetak']; ?></div><div class="lbl">DICETAK</div></div>
	<div class="kpi"><div class="val"><?php echo $g['num']; ?></div><div class="lbl">MARKER (NUM) DIAJUKAN</div></div>
	<div class="kpi"><div class="val"><?php echo $g['hilang']; ?></div><div class="lbl">ALASAN: HILANG</div></div>
	<div class="kpi"><div class="val"><?php echo $g['tidak_tampak']; ?></div><div class="lbl">ALASAN: TIDAK TAMPAK</div></div>
</div>

<table>
	<thead>
		<tr><th>Kode</th><th>Bill / Pelanggan</th><th>Outlet</th><th>Num & Kode Marker</th><th>Alasan</th><th>Foto</th><th>Pengaju</th><th>Persetujuan</th><th>Cetak</th><th>Status</th></tr>
	</thead>
	<tbody>
		<?php foreach ($rows as $r): ?>
		<tr>
			<td><b><?php echo htmlspecialchars($r->pmg_code); ?></b></td>
			<td><?php echo htmlspecialchars($r->bill_code); ?><br><?php echo htmlspecialchars($r->customer_name); ?></td>
			<td><?php echo htmlspecialchars($r->outlet_name); ?></td>
			<td>
				<?php foreach ($r->details as $d): ?>
					<?php echo htmlspecialchars($d->num . '/' . $d->num_total); ?>
					<?php if (strlen((string)$d->pmg_marker_code) > 0): ?> <small>(<?php echo htmlspecialchars($d->pmg_marker_code); ?>)</small><?php endif; ?><br>
				<?php endforeach; ?>
			</td>
			<td><?php echo ($r->reason == 'HILANG') ? 'Lepas & hilang' : 'Tidak tampak 95%'; ?></td>
			<td>
				<?php if (count($r->photos) > 0): ?>
					<?php foreach ($r->photos as $i => $ph): ?><a href="<?php echo $photo_base . $ph->photo; ?>" target="_blank">&#128247;<?php echo $i + 1; ?></a> <?php endforeach; ?>
				<?php else: ?>-<?php endif; ?>
			</td>
			<td><?php echo htmlspecialchars($r->createdby); ?><br><small><?php echo date('d/m/Y H:i', strtotime($r->created)); ?></small></td>
			<td><?php if ($r->approved_at): ?><?php echo date('d/m/Y H:i', strtotime($r->approved_at)); ?><br><small>via <?php echo ($r->approved_via == 'telegram') ? 'Telegram' : 'form admin'; ?></small><?php else: ?>-<?php endif; ?></td>
			<td><?php if ($r->printed_at): ?><?php echo date('d/m/Y H:i', strtotime($r->printed_at)); ?><?php else: ?>-<?php endif; ?></td>
			<td>
				<?php if ((int)$r->status === 0): ?>MENUNGGU
				<?php elseif ((int)$r->status === 1): ?>DISETUJUI<?php if ((int)$r->printed === 1): ?> + DICETAK<?php endif; ?>
				<?php else: ?>DITOLAK<?php endif; ?>
			</td>
		</tr>
		<?php endforeach; ?>
		<?php if (count($rows) == 0): ?><tr><td colspan="10" align="center">Tidak ada data pada periode ini.</td></tr><?php endif; ?>
	</tbody>
</table>

<?php if (count($by_out) > 0): ?>
<h3 style="font-size:13px;margin-top:14px;">Rekap per Outlet</h3>
<table style="width:auto;min-width:320px;">
	<tr><th>Outlet</th><th>Pengajuan</th><th>Marker (num)</th></tr>
	<?php foreach ($by_out as $on => $v): ?>
	<tr><td><?php echo htmlspecialchars($on); ?></td><td><?php echo $v['n']; ?></td><td><?php echo $v['num']; ?></td></tr>
	<?php endforeach; ?>
</table>
<?php endif; ?>
</body>
</html>