<?php
defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;

/** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $this->document->getWebAssetManager();
$wa->useScript('com_grafik_keuangan.stats');
$wa->useStyle('com_grafik_keuangan.styles');

// Helper for formatting currency
$formatRp = function ($val) {
	return 'Rp ' . number_format($val, 2, ',', '.');
};

?>
<div class="com-grafik-keuangan container-fluid">
	<div class="d-flex justify-content-between align-items-center mb-4">
		<h1>Dashboard Realisasi Anggaran</h1>

		<form action="<?php echo Route::_('index.php?option=com_grafik_keuangan'); ?>" method="get"
			class="d-flex gap-2 align-items-center">
			<select name="year" class="form-select">
				<?php foreach ($this->years as $year): ?>
					<option value="<?php echo $year; ?>" <?php echo $this->activeYear == $year ? 'selected' : ''; ?>>
						<?php echo $year; ?>
					</option>
				<?php endforeach; ?>
			</select>

			<select name="month" class="form-select">
				<?php for ($m = 1; $m <= 12; $m++): ?>
					<option value="<?php echo $m; ?>" <?php echo $this->activeMonth == $m ? 'selected' : ''; ?>>
						<?php echo \DateTime::createFromFormat('!m', $m)->format('F'); ?>
					</option>
				<?php endfor; ?>
			</select>

			<button type="submit" class="btn btn-primary">Terapkan</button>
		</form>
	</div>

	<?php if (empty($this->item)): ?>
		<div class="alert alert-warning">
			Data tidak tersedia untuk periode ini.
		</div>
	<?php else: ?>

		<!-- SECTION 1: Akun DIPA -->
		<div class="card mb-4 shadow-sm">
			<div class="card-header bg-light">
				<h3 class="card-title m-0">Realisasi DIPA (Akun 51, 52, 53)</h3>
			</div>
			<div class="card-body">
				<div class="row">
					<div class="col-md-8">
						<div class="chart-container" style="height: 300px;">
							<canvas id="chartDipa"></canvas>
						</div>
					</div>
					<div class="col-md-4 d-flex flex-column justify-content-around">
						<div style="height: 100px; width: 100%"><canvas id="donut51"></canvas></div>
						<div style="height: 100px; width: 100%"><canvas id="donut52"></canvas></div>
						<div style="height: 100px; width: 100%"><canvas id="donut53"></canvas></div>
					</div>
				</div>

				<h5 class="mt-4">Ringkasan DIPA</h5>
				<div class="table-responsive">
					<table class="table table-bordered table-striped">
						<thead class="table-dark">
							<tr>
								<th>Akun</th>
								<th class="text-end">Pagu</th>
								<th class="text-end">Realisasi</th>
								<th class="text-end">Sisa</th>
								<th class="text-center">%</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$accounts = [51, 52, 53];
							foreach ($accounts as $acc):
								$pagu = $this->item->{'pagu_' . $acc};
								$real = $this->item->{'realisasi_' . $acc};
								$sisa = $pagu - $real;
								$perc = $pagu > 0 ? ($real / $pagu) * 100 : 0;
								?>
								<tr>
									<td>Akun <?php echo $acc; ?></td>
									<td class="text-end"><?php echo $formatRp($pagu); ?></td>
									<td class="text-end"><?php echo $formatRp($real); ?></td>
									<td class="text-end"><?php echo $formatRp($sisa); ?></td>
									<td class="text-center"><?php echo number_format($perc, 1); ?>%</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>
		</div>

		<!-- SECTION 2: Kegiatan -->
		<div class="card mb-4 shadow-sm">
			<div class="card-header bg-light">
				<h3 class="card-title m-0">Anggaran Kegiatan Khusus</h3>
			</div>
			<div class="card-body">
				<div class="row">
					<div class="col-md-8">
						<div class="chart-container" style="height: 300px;">
							<canvas id="chartKegiatan"></canvas>
						</div>
					</div>
					<div class="col-md-4 d-flex flex-column justify-content-around">
						<div style="height: 120px; width: 100%"><canvas id="donutPosbakum"></canvas></div>
						<div style="height: 120px; width: 100%"><canvas id="donutProdeo"></canvas></div>
					</div>
				</div>

				<h5 class="mt-4">Ringkasan Kegiatan</h5>
				<div class="table-responsive">
					<table class="table table-bordered table-striped">
						<thead class="table-dark">
							<tr>
								<th>Kegiatan</th>
								<th class="text-end">Pagu</th>
								<th class="text-end">Realisasi</th>
								<th class="text-end">Sisa</th>
								<th class="text-center">%</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$activities = ['posbakum' => 'POSBAKUM', 'prodeo' => 'PRODEO'];
							foreach ($activities as $key => $label):
								$pagu = $this->item->{'pagu_' . $key};
								$real = $this->item->{'realisasi_' . $key};
								$sisa = $pagu - $real;
								$perc = $pagu > 0 ? ($real / $pagu) * 100 : 0;
								?>
								<tr>
									<td><?php echo $label; ?></td>
									<td class="text-end"><?php echo $formatRp($pagu); ?></td>
									<td class="text-end"><?php echo $formatRp($real); ?></td>
									<td class="text-end"><?php echo $formatRp($sisa); ?></td>
									<td class="text-center"><?php echo number_format($perc, 1); ?>%</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>
		</div>

	<?php endif; ?>
</div>