<?= view('admin/layouts/header', ['title' => 'Laporan Penyesuaian Stok']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'laporan-penyesuaian-stok',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?php
$query = array_filter([
    'tanggal_mulai'   => $filters['tanggal_mulai'] ?? '',
    'tanggal_selesai' => $filters['tanggal_selesai'] ?? '',
    'jenis'           => $filters['jenis'] ?? '',
    'status'          => $filters['status'] ?? '',
    'keyword'         => $filters['keyword'] ?? '',
], static fn ($value) => $value !== '');
$pdfUrl = site_url('admin/laporan/penyesuaian-stok/pdf') . ($query ? '?' . http_build_query($query) : '');
$excelUrl = site_url('admin/laporan/penyesuaian-stok/excel') . ($query ? '?' . http_build_query($query) : '');
?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Laporan Penyesuaian Stok',
    'pageSubtitle' => 'Rekap koreksi stok berdasarkan hasil pengecekan stok fisik.',
    'topbarAction' => '<a href="' . $excelUrl . '" class="btn btn-success"><i class="bi bi-file-earmark-excel me-1"></i>Export Excel</a>'
        . '<a href="' . $pdfUrl . '" target="_blank" class="btn btn-dark"><i class="bi bi-file-earmark-pdf me-1"></i>Export PDF</a>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row g-3 mb-4">
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Penyesuaian</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['total_penyesuaian']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Penambahan</div>
                <div class="h4 fw-bold mb-0 text-success"><?= esc((string) $summary['total_penambahan']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Total Pengurangan</div>
                <div class="h4 fw-bold mb-0 text-danger"><?= esc((string) $summary['total_pengurangan']) ?></div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="panel p-3">
                <div class="small text-secondary">Dibatalkan</div>
                <div class="h4 fw-bold mb-0"><?= esc((string) $summary['dibatalkan']) ?></div>
            </div>
        </div>
    </div>

    <div class="panel p-4 mb-4">
        <form action="<?= site_url('admin/laporan/penyesuaian-stok') ?>" method="get" class="row g-3 align-items-end">
            <div class="col-12 col-md-2">
                <label for="tanggal_mulai" class="form-label fw-semibold">Tanggal Mulai</label>
                <input type="date" class="form-control" id="tanggal_mulai" name="tanggal_mulai" value="<?= esc($filters['tanggal_mulai'] ?? '', 'attr') ?>">
            </div>
            <div class="col-12 col-md-2">
                <label for="tanggal_selesai" class="form-label fw-semibold">Tanggal Selesai</label>
                <input type="date" class="form-control" id="tanggal_selesai" name="tanggal_selesai" value="<?= esc($filters['tanggal_selesai'] ?? '', 'attr') ?>">
            </div>
            <div class="col-12 col-md-2">
                <label for="jenis" class="form-label fw-semibold">Jenis</label>
                <select class="form-select" id="jenis" name="jenis">
                    <option value="">Semua jenis</option>
                    <option value="penambahan" <?= ($filters['jenis'] ?? '') === 'penambahan' ? 'selected' : '' ?>>Penambahan</option>
                    <option value="pengurangan" <?= ($filters['jenis'] ?? '') === 'pengurangan' ? 'selected' : '' ?>>Pengurangan</option>
                    <option value="sesuai" <?= ($filters['jenis'] ?? '') === 'sesuai' ? 'selected' : '' ?>>Sesuai</option>
                </select>
            </div>
            <div class="col-12 col-md-2">
                <label for="status" class="form-label fw-semibold">Status</label>
                <select class="form-select" id="status" name="status">
                    <option value="">Semua status</option>
                    <option value="aktif" <?= ($filters['status'] ?? '') === 'aktif' ? 'selected' : '' ?>>Aktif</option>
                    <option value="dibatalkan" <?= ($filters['status'] ?? '') === 'dibatalkan' ? 'selected' : '' ?>>Dibatalkan</option>
                </select>
            </div>
            <div class="col-12 col-md-3">
                <label for="keyword" class="form-label fw-semibold">Pencarian</label>
                <input type="text" class="form-control" id="keyword" name="keyword" value="<?= esc($filters['keyword'] ?? '', 'attr') ?>" placeholder="No, kode, nama barang, alasan">
            </div>
            <div class="col-12 col-md-1">
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-dark flex-fill" title="Filter">
                        <i class="bi bi-funnel"></i>
                    </button>
                    <a href="<?= site_url('admin/laporan/penyesuaian-stok') ?>" class="btn btn-light" title="Reset">
                        <i class="bi bi-arrow-clockwise"></i>
                    </a>
                </div>
            </div>
        </form>
    </div>

    <div class="panel p-4">
        <div class="table-responsive">
            <table class="table table-hover align-middle datatable">
                <thead>
                    <tr>
                        <th>Tanggal</th>
                        <th>No. Penyesuaian</th>
                        <th>Barang</th>
                        <th class="text-end">Stok Sistem</th>
                        <th class="text-end">Stok Fisik</th>
                        <th class="text-end">Selisih</th>
                        <th>Jenis</th>
                        <th>Alasan</th>
                        <th>Status</th>
                        <th>User</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $row) : ?>
                        <?php
                        $selisih = (int) $row['selisih'];
                        $selisihClass = $selisih > 0 ? 'text-success' : ($selisih < 0 ? 'text-danger' : 'text-secondary');
                        $jenisClass = match ($row['jenis']) {
                            'penambahan' => 'success',
                            'pengurangan' => 'danger',
                            default => 'secondary',
                        };
                        ?>
                        <tr>
                            <td><?= esc(date('d/m/Y', strtotime($row['tanggal_penyesuaian']))) ?></td>
                            <td class="fw-semibold"><?= esc($row['no_penyesuaian']) ?></td>
                            <td>
                                <span class="fw-semibold d-block small"><?= esc($row['kode_barang']) ?></span>
                                <?= esc($row['nama_barang']) ?>
                            </td>
                            <td class="text-end"><?= esc((string) $row['stok_sistem']) ?></td>
                            <td class="text-end"><?= esc((string) $row['stok_fisik']) ?></td>
                            <td class="text-end fw-semibold <?= esc($selisihClass, 'attr') ?>"><?= esc((string) $selisih) ?></td>
                            <td><span class="badge text-bg-<?= esc($jenisClass, 'attr') ?>"><?= esc(ucfirst((string) $row['jenis'])) ?></span></td>
                            <td><?= esc($row['alasan']) ?></td>
                            <td>
                                <?php if (($row['status'] ?? '') === 'aktif') : ?>
                                    <span class="badge text-bg-success">Aktif</span>
                                <?php else : ?>
                                    <span class="badge text-bg-secondary">Dibatalkan</span>
                                <?php endif; ?>
                            </td>
                            <td><?= esc($row['nama_user'] ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

<?= view('admin/layouts/footer') ?>
