<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<style>
/* ── PAGE HEADER ─────────────────────────────────────────── */
.ar-page-header { display:flex; justify-content:space-between; align-items:flex-start; flex-wrap:wrap; gap:14px; margin-bottom:24px; }
.ar-header-left h4 { font-size:1.25rem; font-weight:800; color:#0b5a52; margin:0 0 5px; display:flex; align-items:center; gap:11px; letter-spacing:-.3px; }
.ar-header-left h4 .hi { width:40px; height:40px; border-radius:11px; background:linear-gradient(135deg,#0d9488,#14b8a6); display:flex; align-items:center; justify-content:center; color:#fff; font-size:1.05rem; box-shadow:0 4px 12px rgba(13,148,136,0.32); flex-shrink:0; }
.ar-breadcrumb { display:flex; align-items:center; gap:6px; font-size:0.77rem; color:#94a3b8; padding-left:51px; }
.ar-breadcrumb a { color:#0d9488; text-decoration:none; font-weight:600; }
.ar-breadcrumb a:hover { text-decoration:underline; }
.btn-tambah { display:inline-flex; align-items:center; gap:7px; padding:10px 24px; border-radius:50px; border:none; background:#0d9488; color:#fff; font-size:0.85rem; font-weight:700; text-decoration:none; box-shadow:0 3px 12px rgba(13,148,136,0.28); transition:all .2s; white-space:nowrap; }
.btn-tambah:hover { background:#0b7a6e; transform:translateY(-1px); color:#fff; }

/* ── STATUS BADGES ───────────────────────────────────────── */
.badge-status { display:inline-flex; align-items:center; gap:5px; padding:4px 10px; border-radius:50px; font-size:0.75rem; font-weight:700; }
.badge-direncanakan { background:#e2e8f020; color:#64748b; border:1px solid #e2e8f0; }
.badge-disetujui    { background:#dbeafe; color:#1d4ed8; border:1px solid #bfdbfe; }
.badge-terbeli      { background:#dcfce7; color:#166534; border:1px solid #bbf7d0; }
.badge-hibah        { background:#f3e8ff; color:#7e22ce; border:1px solid #e9d5ff; }
.badge-dibatalkan   { background:#fee2e2; color:#991b1b; border:1px solid #fecaca; }
.badge-tinggi  { background:#fee2e2; color:#dc2626; border:1px solid #fecaca; }
.badge-sedang  { background:#fef9c3; color:#ca8a04; border:1px solid #fde68a; }
.badge-rendah  { background:#f0fdf4; color:#16a34a; border:1px solid #bbf7d0; }

/* ── TABLE (desktop) ─────────────────────────────────────── */
.rp-table th { font-size:.78rem; font-weight:700; color:#64748b; text-transform:uppercase; letter-spacing:.5px; background:#f8fafc; border-bottom:2px solid #e2e8f0 !important; }
.rp-table td { vertical-align:middle; font-size:.85rem; border-color:#f1f5f9 !important; }
.rp-table tbody tr:hover { background:#f0fdf9; }

/* ── ACTION BUTTONS ──────────────────────────────────────── */
.btn-aksi { display:inline-flex; align-items:center; gap:5px; padding:5px 12px; border-radius:8px; font-size:.78rem; font-weight:600; text-decoration:none; border:none; cursor:pointer; transition:all .15s; }
.btn-setujui   { background:#dbeafe; color:#1d4ed8; }
.btn-setujui:hover { background:#bfdbfe; }
.btn-realisasi { background:#dcfce7; color:#166534; }
.btn-realisasi:hover { background:#bbf7d0; }
.btn-edit      { background:#fef9c3; color:#854d0e; }
.btn-edit:hover { background:#fde68a; }
.btn-hapus     { background:#fee2e2; color:#991b1b; }
.btn-hapus:hover { background:#fecaca; }
.btn-batal     { background:#f3f4f6; color:#374151; }
.btn-batal:hover { background:#e5e7eb; }

/* ── FILTER TABS ─────────────────────────────────────────── */
.filter-tab { display:inline-flex; align-items:center; gap:6px; padding:7px 16px; border-radius:50px; border:2px solid transparent; font-size:.8rem; font-weight:600; text-decoration:none; transition:all .15s; color:#64748b; background:#f1f5f9; white-space:nowrap; }
.filter-tab:hover { background:#e2e8f0; color:#0f172a; }
.filter-tab.active { border-color:#0d9488; background:#f0fdf9; color:#0d9488; }
.filter-tab .cnt { background:#e2e8f0; border-radius:50px; padding:1px 7px; font-size:.72rem; }
.filter-tab.active .cnt { background:#0d948820; color:#0d9488; }
.filter-tabs-wrap { overflow-x:auto; -webkit-overflow-scrolling:touch; scrollbar-width:none; }
.filter-tabs-wrap::-webkit-scrollbar { display:none; }

/* ── MOBILE CARD ─────────────────────────────────────────── */
.rp-card {
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 1px 8px rgba(0,0,0,.07);
    padding: 16px;
    margin-bottom: 12px;
    border-left: 4px solid #e2e8f0;
    transition: box-shadow .2s;
}
.rp-card:active { box-shadow: 0 4px 16px rgba(0,0,0,.12); }
.rp-card.status-direncanakan { border-left-color: #94a3b8; }
.rp-card.status-disetujui    { border-left-color: #3b82f6; }
.rp-card.status-terbeli      { border-left-color: #16a34a; }
.rp-card.status-hibah        { border-left-color: #7e22ce; }
.rp-card.status-dibatalkan   { border-left-color: #dc2626; }

.rp-card-header { display:flex; justify-content:space-between; align-items:flex-start; margin-bottom:10px; gap:8px; }
.rp-card-kode { font-size:.72rem; font-weight:700; color:#0d9488; font-family:monospace; }
.rp-card-nama { font-size:.95rem; font-weight:800; color:#0f172a; margin-bottom:4px; line-height:1.3; }
.rp-card-ket  { font-size:.75rem; color:#94a3b8; margin-bottom:10px; }

.rp-card-meta { display:flex; flex-wrap:wrap; gap:6px; margin-bottom:12px; }
.rp-card-meta-item { display:flex; align-items:center; gap:4px; font-size:.75rem; color:#64748b; }
.rp-card-meta-item i { color:#0d9488; font-size:.8rem; }

.rp-card-harga { font-size:1rem; font-weight:800; color:#0d9488; }
.rp-card-harga-sub { font-size:.72rem; color:#94a3b8; }

.rp-card-actions { display:flex; gap:6px; flex-wrap:wrap; padding-top:12px; border-top:1px solid #f1f5f9; margin-top:8px; }
.rp-card-actions .btn-aksi { flex:1; justify-content:center; padding:8px 12px; font-size:.8rem; border-radius:10px; }

/* ── RESPONSIVE ──────────────────────────────────────────── */
.view-table { display:block; }
.view-cards { display:none; }

@media(max-width:767.98px) {
    .view-table { display:none !important; }
    .view-cards { display:block !important; }
    .ar-page-header { flex-direction:column; }
    .stats-row > div { flex: 0 0 50%; max-width:50%; }
    .btn-tambah { width:100%; justify-content:center; }
}
</style>

<!-- Header -->
<div class="ar-page-header">
    <div class="ar-header-left">
        <h4>
            <span class="hi"><i class="bi bi-cart-check"></i></span>
            Rencana Pembelian
        </h4>
        <div class="ar-breadcrumb">
            <a href="<?php echo base_url('dashboard'); ?>">Dashboard</a>
            <span>/</span>
            <a href="<?php echo base_url('inventaris'); ?>">Inventaris</a>
            <span>/</span>
            <span>Rencana Pembelian</span>
        </div>
    </div>
    <?php if ($is_admin): ?>
    <a href="<?php echo base_url('rencana-pembelian/tambah'); ?>" class="btn-tambah">
        <i class="bi bi-plus-lg"></i> Tambah Rencana
    </a>
    <?php endif; ?>
</div>

<!-- Stats Cards -->
<div class="row g-3 mb-4 stats-row">
    <div class="col-6 col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius:12px; border-left:4px solid #64748b !important;">
            <div class="card-body py-3 d-flex align-items-center gap-3">
                <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0" style="width:44px;height:44px;background:#64748b20;">
                    <i class="bi bi-list-ul fs-5" style="color:#64748b;"></i>
                </div>
                <div>
                    <div class="text-muted small fw-semibold">Total</div>
                    <div class="fw-bold fs-4 lh-1"><?php echo $stats['total']; ?></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius:12px; border-left:4px solid #f59e0b !important;">
            <div class="card-body py-3 d-flex align-items-center gap-3">
                <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0" style="width:44px;height:44px;background:#f59e0b20;">
                    <i class="bi bi-hourglass-split fs-5" style="color:#f59e0b;"></i>
                </div>
                <div>
                    <div class="text-muted small fw-semibold">Pending</div>
                    <div class="fw-bold fs-4 lh-1"><?php echo $stats['direncanakan'] + $stats['disetujui']; ?></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius:12px; border-left:4px solid #16a34a !important;">
            <div class="card-body py-3 d-flex align-items-center gap-3">
                <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0" style="width:44px;height:44px;background:#16a34a20;">
                    <i class="bi bi-check-circle fs-5" style="color:#16a34a;"></i>
                </div>
                <div>
                    <div class="text-muted small fw-semibold">Terealisasi</div>
                    <div class="fw-bold fs-4 lh-1"><?php echo $stats['terbeli'] + $stats['hibah']; ?></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card border-0 shadow-sm" style="border-radius:12px; border-left:4px solid #0dcaf0 !important;">
            <div class="card-body py-3 d-flex align-items-center gap-3">
                <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0" style="width:44px;height:44px;background:#0dcaf020;">
                    <i class="bi bi-cash-stack fs-5 text-info"></i>
                </div>
                <div>
                    <div class="text-muted small fw-semibold">Est. Pending</div>
                    <div class="fw-bold lh-1" style="font-size:.88rem;">Rp <?php echo number_format($stats['total_estimasi_pending'], 0, ',', '.'); ?></div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Filter Tabs -->
<div class="d-flex gap-2 mb-3 filter-tabs-wrap">
    <?php
    $tabs = [
        ''             => ['label' => 'Semua',        'cnt' => $stats['total']],
        'direncanakan' => ['label' => 'Direncanakan', 'cnt' => $stats['direncanakan']],
        'disetujui'    => ['label' => 'Disetujui',    'cnt' => $stats['disetujui']],
        'terbeli'      => ['label' => 'Terbeli',      'cnt' => $stats['terbeli']],
        'hibah'        => ['label' => 'Hibah',        'cnt' => $stats['hibah']],
        'dibatalkan'   => ['label' => 'Dibatalkan',   'cnt' => $stats['dibatalkan']],
    ];
    foreach ($tabs as $val => $tab):
        $active = ($filter_status === $val || ($val === '' && $filter_status === null)) ? 'active' : '';
        $url    = base_url('rencana-pembelian') . ($val ? '?status=' . $val : '');
    ?>
    <a href="<?php echo $url; ?>" class="filter-tab <?php echo $active; ?>">
        <?php echo $tab['label']; ?>
        <span class="cnt"><?php echo $tab['cnt']; ?></span>
    </a>
    <?php endforeach; ?>
</div>

<!-- Flashdata -->
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success border-0 rounded-3 d-flex align-items-center gap-2 mb-3">
    <i class="bi bi-check-circle-fill"></i> <?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<?php if ($this->session->flashdata('error')): ?>
<div class="alert alert-danger border-0 rounded-3 d-flex align-items-center gap-2 mb-3">
    <i class="bi bi-x-circle-fill"></i> <?php echo $this->session->flashdata('error'); ?>
</div>
<?php endif; ?>

<?php
$badge_status = [
    'direncanakan' => ['class' => 'badge-direncanakan', 'icon' => 'bi-pencil-square',  'label' => 'Direncanakan'],
    'disetujui'    => ['class' => 'badge-disetujui',    'icon' => 'bi-hand-thumbs-up', 'label' => 'Disetujui'],
    'terbeli'      => ['class' => 'badge-terbeli',      'icon' => 'bi-bag-check',      'label' => 'Terbeli'],
    'hibah'        => ['class' => 'badge-hibah',        'icon' => 'bi-gift',           'label' => 'Hibah'],
    'dibatalkan'   => ['class' => 'badge-dibatalkan',   'icon' => 'bi-x-circle',       'label' => 'Dibatalkan'],
];
$badge_prioritas = [
    'tinggi' => 'badge-tinggi',
    'sedang' => 'badge-sedang',
    'rendah' => 'badge-rendah',
];
?>

<!-- ════════════════════════════════════════
     DESKTOP: TABLE VIEW
════════════════════════════════════════ -->
<div class="view-table">
    <div class="card border-0 shadow-sm" style="border-radius:12px; overflow:hidden;">
        <div class="table-responsive">
            <table class="table rp-table mb-0">
                <thead>
                    <tr>
                        <th style="width:120px;">Kode</th>
                        <th>Nama Barang</th>
                        <th style="width:110px;">Kategori</th>
                        <th style="width:80px;">Jml</th>
                        <th style="width:130px;">Est. Harga</th>
                        <th style="width:90px;">Prioritas</th>
                        <th style="width:110px;">Tgl Rencana</th>
                        <th style="width:110px;">Status</th>
                        <?php if ($is_admin): ?><th style="width:200px;">Aksi</th><?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                <?php if (empty($rencana)): ?>
                    <tr>
                        <td colspan="<?php echo $is_admin ? 9 : 8; ?>" class="text-center py-5 text-muted">
                            <i class="bi bi-inbox fs-2 d-block mb-2"></i>
                            Belum ada data rencana pembelian
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($rencana as $r):
                        $bs = $badge_status[$r->status] ?? ['class' => 'badge-direncanakan', 'icon' => 'bi-question', 'label' => $r->status];
                        $bp = $badge_prioritas[$r->prioritas] ?? 'badge-rendah';
                    ?>
                    <tr>
                        <td><code style="color:#0d9488;font-size:.8rem;"><?php echo isset($r->kode_rencana) ? $r->kode_rencana : '#'.$r->id; ?></code></td>
                        <td>
                            <div class="fw-semibold text-dark"><?php echo htmlspecialchars($r->nama_barang); ?></div>
                            <?php if (!empty($r->keterangan)): ?>
                            <small class="text-muted d-block" style="font-size:.75rem; max-width:220px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">
                                <?php echo htmlspecialchars($r->keterangan); ?>
                            </small>
                            <?php endif; ?>
                        </td>
                        <td><span class="badge rounded-pill bg-light text-secondary border"><?php echo ucfirst($r->kategori); ?></span></td>
                        <td><?php echo $r->jumlah . ' ' . (isset($r->satuan) ? $r->satuan : 'unit'); ?></td>
                        <td>
                            <?php if ($r->estimasi_harga > 0): ?>
                            <span class="fw-semibold" style="color:#0d9488;">Rp <?php echo number_format($r->estimasi_harga, 0, ',', '.'); ?></span>
                            <small class="d-block text-muted" style="font-size:.72rem;">Total: Rp <?php echo number_format($r->estimasi_harga * $r->jumlah, 0, ',', '.'); ?></small>
                            <?php else: ?><span class="text-muted">—</span><?php endif; ?>
                        </td>
                        <td><span class="badge-status <?php echo $bp; ?>"><?php echo ucfirst($r->prioritas); ?></span></td>
                        <td class="text-muted" style="font-size:.8rem;">
                            <?php echo isset($r->tanggal_rencana) && $r->tanggal_rencana ? date('d/m/Y', strtotime($r->tanggal_rencana)) : '—'; ?>
                        </td>
                        <td>
                            <span class="badge-status <?php echo $bs['class']; ?>">
                                <i class="bi <?php echo $bs['icon']; ?>"></i> <?php echo $bs['label']; ?>
                            </span>
                            <?php if (in_array($r->status, ['terbeli','hibah']) && !empty($r->inventaris_id)): ?>
                            <a href="<?php echo base_url('inventaris/detail/'.$r->inventaris_id); ?>" class="d-block mt-1 text-muted" style="font-size:.72rem;">
                                <i class="bi bi-box-seam me-1"></i>Lihat inventaris
                            </a>
                            <?php endif; ?>
                        </td>
                        <?php if ($is_admin): ?>
                        <td>
                            <div class="d-flex flex-wrap gap-1">
                                <?php if ($r->status === 'direncanakan'): ?>
                                    <a href="<?php echo base_url('rencana-pembelian/setujui/'.$r->id); ?>" class="btn-aksi btn-setujui" onclick="return confirm('Setujui rencana ini?')"><i class="bi bi-hand-thumbs-up"></i> Setujui</a>
                                    <a href="<?php echo base_url('rencana-pembelian/edit/'.$r->id); ?>" class="btn-aksi btn-edit"><i class="bi bi-pencil"></i></a>
                                    <button class="btn-aksi btn-hapus" onclick="confirmHapus(<?php echo $r->id; ?>, '<?php echo htmlspecialchars($r->nama_barang, ENT_QUOTES); ?>')"><i class="bi bi-trash"></i></button>
                                <?php elseif ($r->status === 'disetujui'): ?>
                                    <a href="<?php echo base_url('rencana-pembelian/realisasi/'.$r->id); ?>" class="btn-aksi btn-realisasi"><i class="bi bi-check2-circle"></i> Realisasi</a>
                                    <a href="<?php echo base_url('rencana-pembelian/edit/'.$r->id); ?>" class="btn-aksi btn-edit"><i class="bi bi-pencil"></i></a>
                                    <button class="btn-aksi btn-batal" onclick="confirmBatal(<?php echo $r->id; ?>, '<?php echo htmlspecialchars($r->nama_barang, ENT_QUOTES); ?>')"><i class="bi bi-x-lg"></i></button>
                                <?php elseif (in_array($r->status, ['terbeli','hibah'])): ?>
                                    <span class="text-muted small"><i class="bi bi-check-all"></i> Selesai</span>
                                <?php else: ?>
                                    <a href="<?php echo base_url('rencana-pembelian/edit/'.$r->id); ?>" class="btn-aksi btn-edit"><i class="bi bi-pencil"></i></a>
                                    <button class="btn-aksi btn-hapus" onclick="confirmHapus(<?php echo $r->id; ?>, '<?php echo htmlspecialchars($r->nama_barang, ENT_QUOTES); ?>')"><i class="bi bi-trash"></i></button>
                                <?php endif; ?>
                            </div>
                        </td>
                        <?php endif; ?>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- ════════════════════════════════════════
     MOBILE: CARD VIEW
════════════════════════════════════════ -->
<div class="view-cards">
    <?php if (empty($rencana)): ?>
    <div class="card border-0 shadow-sm text-center py-5" style="border-radius:14px;">
        <div class="card-body">
            <div class="rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width:72px;height:72px;background:#f0fdf9;">
                <i class="bi bi-cart-x fs-2" style="color:#0d9488;"></i>
            </div>
            <h6 class="text-muted fw-semibold">Belum ada rencana pembelian</h6>
            <?php if ($is_admin): ?>
            <a href="<?php echo base_url('rencana-pembelian/tambah'); ?>" class="btn-tambah d-inline-flex mt-3">
                <i class="bi bi-plus-lg"></i> Tambah Rencana
            </a>
            <?php endif; ?>
        </div>
    </div>
    <?php else: ?>
        <?php foreach ($rencana as $r):
            $bs = $badge_status[$r->status] ?? ['class' => 'badge-direncanakan', 'icon' => 'bi-question', 'label' => $r->status];
            $bp = $badge_prioritas[$r->prioritas] ?? 'badge-rendah';
        ?>
        <div class="rp-card status-<?php echo $r->status; ?>">

            <!-- Baris atas: kode & status -->
            <div class="rp-card-header">
                <div>
                    <div class="rp-card-kode"><i class="bi bi-upc me-1"></i><?php echo isset($r->kode_rencana) ? $r->kode_rencana : '#'.$r->id; ?></div>
                    <div class="rp-card-nama"><?php echo htmlspecialchars($r->nama_barang); ?></div>
                    <?php if (!empty($r->keterangan)): ?>
                    <div class="rp-card-ket"><?php echo htmlspecialchars(mb_strimwidth($r->keterangan, 0, 60, '...')); ?></div>
                    <?php endif; ?>
                </div>
                <div class="d-flex flex-column align-items-end gap-1 flex-shrink-0">
                    <span class="badge-status <?php echo $bs['class']; ?>">
                        <i class="bi <?php echo $bs['icon']; ?>"></i> <?php echo $bs['label']; ?>
                    </span>
                    <span class="badge-status <?php echo $bp; ?>" style="font-size:.7rem;">
                        <?php echo ucfirst($r->prioritas); ?>
                    </span>
                </div>
            </div>

            <!-- Meta info -->
            <div class="rp-card-meta">
                <div class="rp-card-meta-item">
                    <i class="bi bi-tags"></i>
                    <span><?php echo ucfirst($r->kategori); ?></span>
                </div>
                <div class="rp-card-meta-item">
                    <i class="bi bi-box"></i>
                    <span><?php echo $r->jumlah . ' ' . (isset($r->satuan) ? $r->satuan : 'unit'); ?></span>
                </div>
                <?php if (isset($r->tanggal_rencana) && $r->tanggal_rencana): ?>
                <div class="rp-card-meta-item">
                    <i class="bi bi-calendar3"></i>
                    <span><?php echo date('d/m/Y', strtotime($r->tanggal_rencana)); ?></span>
                </div>
                <?php endif; ?>
            </div>

            <!-- Harga -->
            <?php if ($r->estimasi_harga > 0): ?>
            <div class="d-flex align-items-center justify-content-between mb-1">
                <span class="text-muted" style="font-size:.75rem;">Estimasi Harga</span>
                <div class="text-end">
                    <div class="rp-card-harga">Rp <?php echo number_format($r->estimasi_harga, 0, ',', '.'); ?></div>
                    <div class="rp-card-harga-sub">Total: Rp <?php echo number_format($r->estimasi_harga * $r->jumlah, 0, ',', '.'); ?></div>
                </div>
            </div>
            <?php endif; ?>

            <!-- Link inventaris jika sudah terealisasi -->
            <?php if (in_array($r->status, ['terbeli','hibah']) && !empty($r->inventaris_id)): ?>
            <a href="<?php echo base_url('inventaris/detail/'.$r->inventaris_id); ?>"
               class="d-inline-flex align-items-center gap-1 mt-1 mb-1"
               style="font-size:.75rem; color:#0d9488; font-weight:600; text-decoration:none;">
                <i class="bi bi-box-seam"></i> Lihat barang di inventaris
            </a>
            <?php endif; ?>

            <!-- Aksi (admin only) -->
            <?php if ($is_admin): ?>
            <div class="rp-card-actions">
                <?php if ($r->status === 'direncanakan'): ?>
                    <a href="<?php echo base_url('rencana-pembelian/setujui/'.$r->id); ?>"
                       class="btn-aksi btn-setujui" onclick="return confirm('Setujui rencana ini?')">
                        <i class="bi bi-hand-thumbs-up"></i> Setujui
                    </a>
                    <a href="<?php echo base_url('rencana-pembelian/edit/'.$r->id); ?>" class="btn-aksi btn-edit">
                        <i class="bi bi-pencil"></i> Edit
                    </a>
                    <button class="btn-aksi btn-hapus"
                            onclick="confirmHapus(<?php echo $r->id; ?>, '<?php echo htmlspecialchars($r->nama_barang, ENT_QUOTES); ?>')">
                        <i class="bi bi-trash"></i>
                    </button>

                <?php elseif ($r->status === 'disetujui'): ?>
                    <a href="<?php echo base_url('rencana-pembelian/realisasi/'.$r->id); ?>" class="btn-aksi btn-realisasi">
                        <i class="bi bi-check2-circle"></i> Realisasi
                    </a>
                    <a href="<?php echo base_url('rencana-pembelian/edit/'.$r->id); ?>" class="btn-aksi btn-edit">
                        <i class="bi bi-pencil"></i> Edit
                    </a>
                    <button class="btn-aksi btn-batal"
                            onclick="confirmBatal(<?php echo $r->id; ?>, '<?php echo htmlspecialchars($r->nama_barang, ENT_QUOTES); ?>')">
                        <i class="bi bi-x-lg"></i> Batal
                    </button>

                <?php elseif (in_array($r->status, ['terbeli','hibah'])): ?>
                    <span class="text-muted small w-100 text-center py-1">
                        <i class="bi bi-check-all me-1"></i>Rencana telah selesai direalisasi
                    </span>

                <?php else: ?>
                    <a href="<?php echo base_url('rencana-pembelian/edit/'.$r->id); ?>" class="btn-aksi btn-edit">
                        <i class="bi bi-pencil"></i> Edit
                    </a>
                    <button class="btn-aksi btn-hapus"
                            onclick="confirmHapus(<?php echo $r->id; ?>, '<?php echo htmlspecialchars($r->nama_barang, ENT_QUOTES); ?>')">
                        <i class="bi bi-trash"></i> Hapus
                    </button>
                <?php endif; ?>
            </div>
            <?php endif; ?>

        </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<!-- Modal Hapus -->
<div class="modal fade" id="modalHapus" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-sm">
        <div class="modal-content border-0 shadow" style="border-radius:14px; overflow:hidden;">
            <div class="modal-header border-0 pb-0 pt-4 px-4">
                <div class="d-flex align-items-center gap-2">
                    <div class="rounded-circle d-flex align-items-center justify-content-center flex-shrink-0" style="width:36px;height:36px;background:#fee2e220;">
                        <i class="bi bi-trash text-danger"></i>
                    </div>
                    <h6 class="mb-0 fw-bold">Hapus Rencana?</h6>
                </div>
            </div>
            <div class="modal-body px-4 py-3">
                <p class="text-muted small mb-1">Hapus rencana <strong id="namaHapus"></strong>?</p>
                <p class="text-danger mb-0" style="font-size:.78em;"><i class="bi bi-info-circle me-1"></i>Data tidak dapat dikembalikan.</p>
            </div>
            <div class="modal-footer border-0 pt-0 px-4 pb-4 gap-2">
                <button class="btn btn-sm btn-outline-secondary flex-fill fw-semibold" style="border-radius:8px;" data-bs-dismiss="modal">Batal</button>
                <a href="#" id="btnHapus" class="btn btn-sm btn-danger flex-fill fw-semibold text-white" style="border-radius:8px;">Ya, Hapus</a>
            </div>
        </div>
    </div>
</div>

<!-- Modal Batalkan -->
<div class="modal fade" id="modalBatal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-sm">
        <div class="modal-content border-0 shadow" style="border-radius:14px; overflow:hidden;">
            <div class="modal-header border-0 pb-0 pt-4 px-4">
                <div class="d-flex align-items-center gap-2">
                    <div class="rounded-circle d-flex align-items-center justify-content-center flex-shrink-0" style="width:36px;height:36px;background:#f3f4f6;">
                        <i class="bi bi-x-circle text-secondary"></i>
                    </div>
                    <h6 class="mb-0 fw-bold">Batalkan Rencana?</h6>
                </div>
            </div>
            <div class="modal-body px-4 py-3">
                <p class="text-muted small mb-0">Batalkan rencana <strong id="namaBatal"></strong>? Status akan berubah menjadi <em>dibatalkan</em>.</p>
            </div>
            <div class="modal-footer border-0 pt-0 px-4 pb-4 gap-2">
                <button class="btn btn-sm btn-outline-secondary flex-fill fw-semibold" style="border-radius:8px;" data-bs-dismiss="modal">Tutup</button>
                <a href="#" id="btnBatal" class="btn btn-sm btn-secondary flex-fill fw-semibold text-white" style="border-radius:8px;">Ya, Batalkan</a>
            </div>
        </div>
    </div>
</div>

<script>
function confirmHapus(id, nama) {
    document.getElementById('namaHapus').textContent = nama;
    document.getElementById('btnHapus').href = '<?php echo base_url('rencana-pembelian/hapus/'); ?>' + id;
    new bootstrap.Modal(document.getElementById('modalHapus')).show();
}
function confirmBatal(id, nama) {
    document.getElementById('namaBatal').textContent = nama;
    document.getElementById('btnBatal').href = '<?php echo base_url('rencana-pembelian/batalkan/'); ?>' + id;
    new bootstrap.Modal(document.getElementById('modalBatal')).show();
}
</script>