@{
	Layout = "~/Views/Admin/Transport/SpjDriverUpst/Shared/_Layout.cshtml";
	ViewData["Title"] = "Detail SPJ";
}

<div class="w-full lg:max-w-sm mx-auto bg-gray-50 min-h-screen pb-20">
	<div class="bg-upst text-white px-6 pt-8 pb-16 rounded-b-[40px] shadow-lg relative">
		<div class="flex items-center justify-between relative z-10">
			<a href="@Url.Action("Index", "Home")" class="w-10 h-10 flex items-center justify-center bg-white/10 rounded-xl backdrop-blur-md hover:bg-white/20 transition-all">
				<i class="w-5 h-5" data-lucide="chevron-left"></i>
			</a>
			<h1 class="text-lg font-black uppercase tracking-tight">Detail SPJ</h1>
			<div class="w-10"></div>
		</div>
	</div>

	<div class="px-4 pb-6 relative -mt-10 z-20 space-y-4">
		<div class="bg-white rounded-2xl p-5 shadow-sm border border-gray-100">
			<div class="flex items-start justify-between gap-3 border-b border-gray-100 pb-4 mb-4">
				<div>
					<div class="flex items-center gap-2 mb-2 flex-wrap">
						<div class="bg-green-100 text-green-600 px-3 py-1 rounded-full text-xs font-bold border border-green-200" id="status-badge">-</div>
						<div class="text-xs text-gray-500" id="tanggal-selesai">-</div>
					</div>
					<h3 class="font-bold text-gray-900 text-lg" id="nama-perusahaan">-</h3>
					<p class="text-sm text-gray-700 leading-relaxed mt-1" id="alamat-perusahaan">-</p>
				</div>
				<div class="w-12 h-12 rounded-2xl bg-green-50 flex items-center justify-center text-green-600 border border-green-100 shrink-0">
					<i class="w-6 h-6" data-lucide="badge-check"></i>
				</div>
			</div>

			<div class="grid grid-cols-2 gap-3">
				<div class="rounded-xl bg-gray-50 border border-gray-100 p-3">
					<p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-1">No. SPJ</p>
					<p class="text-sm font-bold text-gray-800 wrap-break-word" id="nomor-spj">-</p>
				</div>
				<div class="rounded-xl bg-gray-50 border border-gray-100 p-3">
					<p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-1">Plat Nomor</p>
					<p class="text-sm font-bold text-gray-800" id="plat-nomor">-</p>
				</div>
				<div class="rounded-xl bg-gray-50 border border-gray-100 p-3">
					<p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-1">No. Pintu</p>
					<p class="text-sm font-bold text-gray-800" id="nomor-pintu">-</p>
				</div>
				<div class="rounded-xl bg-gray-50 border border-gray-100 p-3">
					<p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-1">Petugas</p>
					<p class="text-sm font-bold text-gray-800" id="nama-checker">-</p>
				</div>
			</div>
		</div>

		<div class="bg-white rounded-2xl p-5 shadow-sm border border-gray-100">
			<div class="flex items-center gap-2 mb-4">
				<h3 class="font-bold text-gray-900">Ringkasan Timbangan</h3>
			</div>

			<div class="grid grid-cols-3 gap-2 mb-4">
				<div class="bg-green-50 rounded-xl p-3 border border-green-100">
					<p class="text-[9px] text-green-600 font-bold uppercase tracking-wider">Organik</p>
					<p class="text-sm font-black text-green-700"><span id="total-organik">0,00</span> kg</p>
				</div>
				<div class="bg-blue-50 rounded-xl p-3 border border-blue-100">
					<p class="text-[9px] text-blue-600 font-bold uppercase tracking-wider">Anorganik</p>
					<p class="text-sm font-black text-blue-700"><span id="total-anorganik">0,00</span> kg</p>
				</div>
				<div class="bg-red-50 rounded-xl p-3 border border-red-100">
					<p class="text-[9px] text-red-600 font-bold uppercase tracking-wider">Residu</p>
					<p class="text-sm font-black text-red-700"><span id="total-residu">0,00</span> kg</p>
				</div>
			</div>

			<div class="rounded-xl bg-upst/5 px-4 py-3 border border-upst/10 flex items-center justify-between">
				<p class="text-sm font-bold text-gray-700">Total Keseluruhan</p>
				<p class="text-2xl font-black text-upst"><span id="total-keseluruhan">0,00</span> kg</p>
			</div>
		</div>


	</div>

	<partial name="~/Views/Admin/Transport/SpjDriverUpst/Shared/Components/_Navigation.cshtml" />
</div>

@section Scripts {
	<script>
		document.addEventListener('DOMContentLoaded', async function () {
			const formatWeight = (value) => new Intl.NumberFormat('id-ID', {
				minimumFractionDigits: 2,
				maximumFractionDigits: 2
			}).format(Number(value || 0));
			const formatDateTime = (value) => {
				const date = new Date(value);
				return new Intl.DateTimeFormat('id-ID', {
					day: '2-digit',
					month: 'short',
					year: 'numeric',
					hour: '2-digit',
					minute: '2-digit',
					timeZone: 'Asia/Jakarta'
				}).format(date) + ' WIB';
			};

			try {
				const response = await fetch('/driver/json/selesai-tanpa-tps-data.json');
				const data = await response.json();
				const selesai = data.selesai;

				document.getElementById('status-badge').textContent = selesai.status;
				document.getElementById('tanggal-selesai').textContent = formatDateTime(selesai.tanggalSelesai);
				document.getElementById('nama-perusahaan').textContent = selesai.namaPerusahaan;
				document.getElementById('alamat-perusahaan').textContent = selesai.alamat;
				document.getElementById('nomor-spj').textContent = selesai.nomorSpj;
				document.getElementById('plat-nomor').textContent = selesai.platNomor;
				document.getElementById('nomor-pintu').textContent = selesai.nomorPintu;
				document.getElementById('nama-checker').textContent = selesai.petugas.checker;
				document.getElementById('total-organik').textContent = formatWeight(selesai.timbangan.organik);
				document.getElementById('total-anorganik').textContent = formatWeight(selesai.timbangan.anorganik);
				document.getElementById('total-residu').textContent = formatWeight(selesai.timbangan.residu);
				document.getElementById('total-keseluruhan').textContent = formatWeight(selesai.timbangan.total);
			} catch (error) {
				console.error('Error loading selesai tanpa TPS data:', error);
			}
		});
	</script>
}
