<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../classes/Branch.php';

requireRole('super_admin');
$pageTitle = 'Keuangan Cabang';

$db = db();
$branches = Branch::all();

// ============================================================================
// HANDLE ACTIONS
// ============================================================================
if ($_SERVER['REQUEST_METHOD'] === 'POST' && verifyCsrf($_POST['csrf_token'] ?? '')) {
    $action = $_POST['action'] ?? '';
    
    try {
        // APPROVE PENARIKAN
        if ($action === 'approve_withdrawal') {
            $wdId = (int)($_POST['withdrawal_id'] ?? 0);
            $notes = trim($_POST['review_notes'] ?? '');
            
            $stmt = $db->prepare("SELECT * FROM branch_withdrawals WHERE id = ? AND status = 'pending'");
            $stmt->execute([$wdId]);
            $wd = $stmt->fetch();
            
            if (!$wd) throw new Exception('Pengajuan tidak ditemukan atau sudah diproses');
            
            $stmt = $db->prepare("SELECT available_balance FROM branch_balances WHERE branch_id = ?");
            $stmt->execute([$wd['branch_id']]);
            $balance = $stmt->fetch();
            
            if ($balance['available_balance'] < $wd['amount']) {
                throw new Exception('Saldo cabang tidak mencukupi');
            }
            
            $db->beginTransaction();
            
            $stmt = $db->prepare("
                UPDATE branch_withdrawals 
                SET status = 'approved', reviewed_by = ?, reviewed_at = NOW(), review_notes = ?
                WHERE id = ?
            ");
            $stmt->execute([currentUserId(), $notes, $wdId]);
            
            $stmt = $db->prepare("
                UPDATE branch_balances 
                SET available_balance = available_balance - ?,
                    total_withdrawn = total_withdrawn + ?,
                    last_updated_at = NOW()
                WHERE branch_id = ?
            ");
            $stmt->execute([$wd['amount'], $wd['amount'], $wd['branch_id']]);
            
            $db->commit();
            
            logActivity(currentUserId(), 'approve_withdrawal', 
                "Approve penarikan #$wdId - Rp " . number_format($wd['amount']) . 
                " untuk cabang #" . $wd['branch_id']);
            
            redirect('/admin/branch_finance.php', '✅ Pengajuan penarikan berhasil diapprove');
        }
        
        // REJECT PENARIKAN
        if ($action === 'reject_withdrawal') {
            $wdId = (int)($_POST['withdrawal_id'] ?? 0);
            $notes = trim($_POST['review_notes'] ?? '');
            
            if (empty($notes)) throw new Exception('Alasan penolakan wajib diisi');
            
            $stmt = $db->prepare("
                UPDATE branch_withdrawals 
                SET status = 'rejected', reviewed_by = ?, reviewed_at = NOW(), review_notes = ?
                WHERE id = ? AND status = 'pending'
            ");
            $stmt->execute([currentUserId(), $notes, $wdId]);
            
            logActivity(currentUserId(), 'reject_withdrawal', "Reject penarikan #$wdId");
            redirect('/admin/branch_finance.php', '✅ Pengajuan penarikan ditolak');
        }
        
        // MARK AS PROCESSED
        if ($action === 'mark_processed') {
            $wdId = (int)($_POST['withdrawal_id'] ?? 0);
            
            $stmt = $db->prepare("
                UPDATE branch_withdrawals 
                SET status = 'processed', processed_at = NOW()
                WHERE id = ? AND status = 'approved'
            ");
            $stmt->execute([$wdId]);
            
            redirect('/admin/branch_finance.php', '✅ Penarikan ditandai sudah ditransfer');
        }
        
    } catch (\Throwable $e) {
        if (isset($db) && $db->inTransaction()) $db->rollBack();
        $_SESSION['flash_error'] = '❌ ' . $e->getMessage();
    }
}

$flashError = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_error']);

// ============================================================================
// LOAD DATA
// ============================================================================
$filterBranch = $_GET['branch'] ?? '';

$summary = $db->query("
    SELECT 
        COALESCE(SUM(pending_balance), 0) as total_pending,
        COALESCE(SUM(available_balance), 0) as total_available,
        COALESCE(SUM(total_withdrawn), 0) as total_withdrawn,
        COALESCE(SUM(total_income), 0) as total_income
    FROM branch_balances
")->fetch();

$stmt = $db->prepare("
    SELECT bb.*, b.name as branch_name, b.code as branch_code,
        (SELECT COUNT(*) FROM branch_withdrawals WHERE branch_id = bb.branch_id AND status = 'pending') as pending_withdrawals
    FROM branch_balances bb
    LEFT JOIN branches b ON bb.branch_id = b.id
    ORDER BY b.name ASC
");
$stmt->execute();
$branchBalances = $stmt->fetchAll();

$sql = "
    SELECT w.*, b.name as branch_name, u.name as requester_name, r.name as reviewer_name
    FROM branch_withdrawals w
    LEFT JOIN branches b ON w.branch_id = b.id
    LEFT JOIN users u ON w.requested_by = u.id
    LEFT JOIN users r ON w.reviewed_by = r.id
";
$params = [];
if ($filterBranch) {
    $sql .= " WHERE w.branch_id = ?";
    $params[] = $filterBranch;
}
$sql .= " ORDER BY w.requested_at DESC LIMIT 100";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$withdrawals = $stmt->fetchAll();

$sql = "
    SELECT bt.*, b.name as branch_name
    FROM branch_transactions bt
    LEFT JOIN branches b ON bt.branch_id = b.id
";
$params = [];
if ($filterBranch) {
    $sql .= " WHERE bt.branch_id = ?";
    $params[] = $filterBranch;
}
$sql .= " ORDER BY bt.created_at DESC LIMIT 50";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$recentTransactions = $stmt->fetchAll();

require __DIR__ . '/../includes/header.php';
require __DIR__ . '/../includes/sidebar.php';
?>

<style>


/* === MAIN CONTENT === */


.page-header {
    margin-bottom: 25px;
}

.page-title {
    font-size: 1.8rem;
    font-weight: 700;
    color: #003D7A;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.page-title i {
    font-size: 2rem;
    color: #0066CC;
}

.page-subtitle {
    color: #666;
    font-size: 0.95rem;
    margin: 0;
}

/* === SUMMARY CARDS === */
.summary-card {
    background: white;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    margin-bottom: 16px;
    position: relative;
    overflow: hidden;
    transition: all 0.3s ease;
    border: none;
}

.summary-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.12);
}

.summary-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
}

.summary-card.warning::before { background: linear-gradient(135deg, #FF9900 0%, #FFB84D 100%); }
.summary-card.success::before { background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%); }
.summary-card.primary::before { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.summary-card.info::before { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }

.summary-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.summary-card-title {
    color: #666;
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 6px;
}

.summary-card-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1.2rem;
}

.summary-card.warning .summary-card-icon { background: linear-gradient(135deg, #FF9900 0%, #FFB84D 100%); }
.summary-card.success .summary-card-icon { background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%); }
.summary-card.primary .summary-card-icon { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.summary-card.info .summary-card-icon { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }

.summary-card-value {
    font-size: 1.6rem;
    font-weight: 800;
    margin-bottom: 8px;
    letter-spacing: -0.5px;
}

.summary-card.warning .summary-card-value { color: #FF9900; }
.summary-card.success .summary-card-value { color: #00C853; }
.summary-card.primary .summary-card-value { color: #0066CC; }
.summary-card.info .summary-card-value { color: #00B8D4; }

.summary-card-footer {
    font-size: 0.8rem;
    color: #999;
    padding-top: 10px;
    border-top: 1px dashed #e8f4f8;
}

/* === CARD MODERN === */
.card-modern {
    background: white;
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    overflow: hidden;
    margin-bottom: 20px;
    border: none;
}

.card-header-modern {
    color: white;
    padding: 18px 24px;
    font-size: 1.1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.card-header-modern.primary { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.card-header-modern.warning { background: linear-gradient(135deg, #FF9900 0%, #FFB84D 100%); }
.card-header-modern.info { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }

.card-header-modern .title-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.card-body-modern {
    padding: 24px;
}

/* === FILTER === */
.filter-form {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 12px;
    align-items: end;
}

.form-group-filter {
    margin-bottom: 0;
}

.form-label-filter {
    display: block;
    font-weight: 600;
    color: #003D7A;
    margin-bottom: 6px;
    font-size: 0.85rem;
}

.form-select-modern {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid #e8f4f8;
    border-radius: 10px;
    font-size: 0.9rem;
    transition: all 0.3s ease;
    background: #f8fafc;
}

.form-select-modern:focus {
    outline: none;
    border-color: #0066CC;
    background: white;
    box-shadow: 0 0 0 4px rgba(0, 102, 204, 0.1);
}

.btn-filter {
    padding: 10px 18px;
    border: none;
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    text-decoration: none;
    white-space: nowrap;
}

.btn-filter.primary {
    background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%);
    color: white;
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.3);
}

.btn-filter.primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 102, 204, 0.4);
}

.btn-filter.outline {
    background: white;
    color: #666;
    border: 2px solid #e8f4f8;
}

.btn-filter.outline:hover {
    background: #f8fafc;
    border-color: #0066CC;
    color: #0066CC;
}

/* === TABLE MODERN === */
.table-modern {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.9rem;
}

.table-modern thead {
    background: #f8fafc;
}

.table-modern th {
    padding: 14px 16px;
    text-align: left;
    font-weight: 700;
    color: #003D7A;
    border-bottom: 2px solid #e8f4f8;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.table-modern td {
    padding: 14px 16px;
    border-bottom: 1px solid #f0f0f0;
    vertical-align: middle;
}

.table-modern tbody tr {
    transition: all 0.2s ease;
}

.table-modern tbody tr:hover {
    background: #f8fafc;
}

.table-modern tbody tr:last-child td {
    border-bottom: none;
}

.text-end { text-align: right; }
.text-center { text-align: center; }

.data-value {
    font-size: 0.95rem;
    color: #333;
    font-weight: 600;
}

.data-value.amount {
    font-weight: 800;
}

.data-value.warning { color: #FF9900; }
.data-value.success { color: #00C853; }
.data-value.primary { color: #0066CC; }
.data-value.info { color: #00B8D4; }

.data-sub {
    font-size: 0.8rem;
    color: #999;
    margin-top: 2px;
}

/* === BADGE MODERN === */
.badge-modern {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 12px;
    border-radius: 20px;
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.badge-modern.pending {
    background: #fff3cd;
    color: #856404;
    border: 1px solid #ffe69c;
}

.badge-modern.approved, .badge-modern.released {
    background: #d1e7dd;
    color: #0f5132;
    border: 1px solid #a3cfbb;
}

.badge-modern.rejected, .badge-modern.cancelled {
    background: #f8d7da;
    color: #842029;
    border: 1px solid #f1aeb5;
}

.badge-modern.processed {
    background: #cfe2ff;
    color: #084298;
    border: 1px solid #b6d4fe;
}

.badge-modern.danger-count {
    background: #f8d7da;
    color: #842029;
    padding: 4px 10px;
    font-size: 0.75rem;
}

.badge-modern.secondary {
    background: #e9ecef;
    color: #6c757d;
    border: 1px solid #dee2e6;
}

/* === ACTION BUTTONS === */
.action-buttons {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.btn-action {
    padding: 8px 14px;
    border: none;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 0.85rem;
    font-weight: 600;
    text-decoration: none;
}

.btn-action.approve {
    background: #d1e7dd;
    color: #0f5132;
}

.btn-action.approve:hover {
    background: #00C853;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 200, 83, 0.3);
}

.btn-action.reject {
    background: #f8d7da;
    color: #842029;
}

.btn-action.reject:hover {
    background: #DC3545;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(220, 53, 69, 0.3);
}

.btn-action.primary {
    background: #cfe2ff;
    color: #084298;
}

.btn-action.primary:hover {
    background: #0066CC;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.3);
}

/* === MOBILE CARD VIEW === */
.mobile-cards {
    display: none;
}

.mobile-card {
    background: white;
    border-radius: 14px;
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.06);
    border: 1px solid #f0f0f0;
}

.mobile-card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px dashed #e8f4f8;
    gap: 10px;
}

.mobile-card-title {
    flex: 1;
    min-width: 0;
}

.mobile-card-title-main {
    font-weight: 700;
    color: #003D7A;
    font-size: 1rem;
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mobile-card-title-sub {
    font-size: 0.75rem;
    color: #999;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.mobile-card-amount {
    font-size: 1.1rem;
    font-weight: 800;
    color: #0066CC;
    text-align: right;
    white-space: nowrap;
}

.mobile-card-amount small {
    display: block;
    font-size: 0.7rem;
    color: #999;
    font-weight: 500;
    margin-top: 2px;
}

.mobile-card-body {
    display: grid;
    gap: 10px;
    margin-bottom: 12px;
}

.mobile-card-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
    font-size: 0.85rem;
}

.mobile-card-row .label {
    color: #999;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
    min-width: 80px;
}

.mobile-card-row .value {
    color: #333;
    font-weight: 600;
    text-align: right;
    flex: 1;
    min-width: 0;
    word-break: break-word;
}

.mobile-card-footer {
    display: flex;
    gap: 8px;
    padding-top: 12px;
    border-top: 1px dashed #e8f4f8;
    flex-wrap: wrap;
}

.mobile-card-footer .btn-action {
    flex: 1;
    min-width: 100px;
    padding: 10px;
}

.bank-info-mobile {
    background: #f8fafc;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 0.8rem;
    line-height: 1.5;
    border-left: 3px solid #0066CC;
    margin-top: 4px;
}

.bank-info-mobile strong {
    color: #003D7A;
}

/* === EMPTY STATE === */
.empty-state {
    text-align: center;
    padding: 50px 20px;
}

.empty-state-icon {
    width: 70px;
    height: 70px;
    margin: 0 auto 16px;
    background: #f8fafc;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    color: #ccc;
}

.empty-state-title {
    color: #666;
    font-weight: 600;
    margin-bottom: 6px;
    font-size: 1rem;
}

.empty-state-subtitle {
    color: #999;
    font-size: 0.85rem;
    margin: 0;
}

/* === ALERT MODERN === */
.alert-modern {
    padding: 16px 20px;
    border-radius: 12px;
    margin-bottom: 20px;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    font-size: 0.95rem;
    border: 2px solid;
}

.alert-modern.success {
    background: #e8f5e9;
    border-color: #a5d6a7;
    color: #2e7d32;
}

.alert-modern.danger {
    background: #ffebee;
    border-color: #ef9a9a;
    color: #c62828;
}

.alert-modern.info {
    background: #e3f2fd;
    border-color: #90caf9;
    color: #1565c0;
}

.alert-modern i {
    font-size: 1.3rem;
    margin-top: 2px;
}

/* === MODAL MODERN === */
.modal-modern .modal-content {
    border: none;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}

.modal-modern .modal-header {
    color: white;
    padding: 18px 24px;
    border: none;
}

.modal-modern .modal-header.success {
    background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%);
}

.modal-modern .modal-header.danger {
    background: linear-gradient(135deg, #DC3545 0%, #FF6B7A 100%);
}

.modal-modern .modal-title {
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.1rem;
}

.modal-modern .modal-body {
    padding: 24px;
}

.modal-modern .modal-footer {
    padding: 16px 24px;
    border-top: 1px solid #f0f0f0;
    background: #f8fafc;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.form-group-modern {
    margin-bottom: 16px;
}

.form-label-modern {
    display: block;
    font-weight: 600;
    color: #003D7A;
    margin-bottom: 8px;
    font-size: 0.95rem;
}

.form-control-modern {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid #e8f4f8;
    border-radius: 12px;
    font-size: 0.95rem;
    transition: all 0.3s ease;
    background: #f8fafc;
    font-family: inherit;
}

.form-control-modern:focus {
    outline: none;
    border-color: #0066CC;
    background: white;
    box-shadow: 0 0 0 4px rgba(0, 102, 204, 0.1);
}

.btn-modal {
    padding: 10px 20px;
    border: none;
    border-radius: 10px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.btn-modal.secondary {
    background: white;
    color: #666;
    border: 2px solid #e8f4f8;
}

.btn-modal.secondary:hover {
    background: #f8fafc;
}

.btn-modal.success {
    background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%);
    color: white;
    box-shadow: 0 4px 12px rgba(0, 200, 83, 0.3);
}

.btn-modal.success:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 200, 83, 0.4);
}

.btn-modal.danger {
    background: linear-gradient(135deg, #DC3545 0%, #FF6B7A 100%);
    color: white;
    box-shadow: 0 4px 12px rgba(220, 53, 69, 0.3);
}

.btn-modal.danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(220, 53, 69, 0.4);
}

.review-info-box {
    background: #e3f2fd;
    border: 2px solid #90caf9;
    border-radius: 12px;
    padding: 14px 16px;
    margin-bottom: 16px;
    font-size: 0.9rem;
    line-height: 1.6;
}

.review-info-box strong {
    color: #1565c0;
}

/* === RESPONSIVE === */
@media (max-width: 992px) {
    .filter-form {
        grid-template-columns: 1fr 1fr;
    }
    
    .filter-form .form-group-filter:first-child {
        grid-column: 1 / -1;
    }
}



@media (max-width: 480px) {
    .navbar-modern {
        padding: 10px 12px;
    }
    
    .burger-btn {
        width: 36px;
        height: 36px;
        font-size: 1.1rem;
    }
    
    .navbar-title {
        font-size: 0.95rem;
    }
    
    .main-content {
        padding: 12px;
    }
    
    .page-title {
        font-size: 1.2rem;
        gap: 8px;
    }
    
    .summary-card {
        padding: 14px;
    }
    
    .summary-card-title {
        font-size: 0.8rem;
    }
    
    .summary-card-value {
        font-size: 1.15rem;
    }
    
    .card-header-modern {
        padding: 12px 15px;
        font-size: 0.95rem;
    }
    
    .card-body-modern {
        padding: 14px;
    }
    
    .mobile-card {
        padding: 14px;
    }
    
    .mobile-card-title-main {
        font-size: 0.95rem;
    }
    
    .mobile-card-amount {
        font-size: 1rem;
    }
    
    .mobile-card-row {
        font-size: 0.8rem;
    }
    
    .mobile-card-footer .btn-action {
        font-size: 0.8rem;
        padding: 9px;
    }
}
</style>

<!-- Navbar Modern dengan Burger Icon -->

<div class="content-wrapper">
<div class="main-content">
    <div class="navbar-modern">
    <div class="navbar-left">
        <button class="burger-btn" onclick="toggleSidebar()">
            <i class="bi bi-list"></i>
        </button>
        <div>
            <h1 class="navbar-title">Keuangan Cabang</h1>
           
        </div>
    </div>
    <div class="navbar-right">
        <span class="navbar-badge">
            <i class="bi bi-shield-check"></i> Super Admin
        </span>
    </div>
</div>
    <!-- Page Header -->
    <div class="page-header">
        
    </div>

    <?= flashMessage() ?>
    
    <?php if ($flashError): ?>
        <div class="alert-modern danger">
            <i class="bi bi-exclamation-circle-fill"></i>
            <div><?= htmlspecialchars($flashError) ?></div>
        </div>
    <?php endif; ?>

    <!-- Summary Cards -->
    <div class="row g-3 mb-4">
        <div class="col-lg-3 col-md-6">
            <div class="summary-card warning">
                <div class="summary-card-header">
                    <h6 class="summary-card-title">
                        <i class="bi bi-hourglass-split"></i>
                        Saldo Pending (H+5)
                    </h6>
                    <div class="summary-card-icon">
                        <i class="bi bi-hourglass"></i>
                    </div>
                </div>
                <div class="summary-card-value">
                    Rp <?= number_format($summary['total_pending'], 0, ',', '.') ?>
                </div>
                <div class="summary-card-footer">
                    ⏳ Menunggu release
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="summary-card success">
                <div class="summary-card-header">
                    <h6 class="summary-card-title">
                        <i class="bi bi-cash-coin"></i>
                        Saldo Tersedia
                    </h6>
                    <div class="summary-card-icon">
                        <i class="bi bi-wallet2"></i>
                    </div>
                </div>
                <div class="summary-card-value">
                    Rp <?= number_format($summary['total_available'], 0, ',', '.') ?>
                </div>
                <div class="summary-card-footer">
                    💰 Siap ditarik cabang
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="summary-card primary">
                <div class="summary-card-header">
                    <h6 class="summary-card-title">
                        <i class="bi bi-arrow-up-circle"></i>
                        Total Sudah Ditarik
                    </h6>
                    <div class="summary-card-icon">
                        <i class="bi bi-cash-stack"></i>
                    </div>
                </div>
                <div class="summary-card-value">
                    Rp <?= number_format($summary['total_withdrawn'], 0, ',', '.') ?>
                </div>
                <div class="summary-card-footer">
                    ✅ Dana ditransfer
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="summary-card info">
                <div class="summary-card-header">
                    <h6 class="summary-card-title">
                        <i class="bi bi-graph-up-arrow"></i>
                        Total Pemasukan
                    </h6>
                    <div class="summary-card-icon">
                        <i class="bi bi-graph-up"></i>
                    </div>
                </div>
                <div class="summary-card-value">
                    Rp <?= number_format($summary['total_income'], 0, ',', '.') ?>
                </div>
                <div class="summary-card-footer">
                    📈 Seluruh waktu
                </div>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="card-modern">
        <div class="card-header-modern primary">
            <div class="title-left">
                <i class="bi bi-funnel-fill"></i>
                <span>Filter Data</span>
            </div>
        </div>
        <div class="card-body-modern">
            <form method="GET" class="filter-form">
                <div class="form-group-filter">
                    <label class="form-label-filter">Filter Cabang</label>
                    <select name="branch" class="form-select-modern">
                        <option value="">Semua Cabang</option>
                        <?php foreach ($branches as $b): ?>
                            <option value="<?= $b['id'] ?>" <?= $filterBranch == $b['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($b['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <button type="submit" class="btn-filter primary">
                    <i class="bi bi-search"></i>
                    <span>Filter</span>
                </button>
                <a href="/admin/branch_finance.php" class="btn-filter outline">
                    <i class="bi bi-arrow-counterclockwise"></i>
                    <span>Reset</span>
                </a>
            </form>
        </div>
    </div>

    <!-- Saldo Per Cabang -->
    <div class="card-modern">
        <div class="card-header-modern primary">
            <div class="title-left">
                <i class="bi bi-building"></i>
                <span>Saldo Per Cabang</span>
            </div>
            <span class="navbar-badge" style="background: rgba(255,255,255,0.25);">
                <?= count($branchBalances) ?> Cabang
            </span>
        </div>
        
        <!-- Desktop Table View -->
        <div class="desktop-table" style="overflow-x: auto;">
            <table class="table-modern">
                <thead>
                    <tr>
                        <th>Cabang</th>
                        <th class="text-end">Pending (H+5)</th>
                        <th class="text-end">Tersedia</th>
                        <th class="text-end">Sudah Ditarik</th>
                        <th class="text-end">Total Income</th>
                        <th class="text-center">Pengajuan Pending</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($branchBalances as $bb): ?>
                        <tr>
                            <td>
                                <div class="data-value"><?= htmlspecialchars($bb['branch_name'] ?? 'Unknown') ?></div>
                                <?php if ($bb['branch_code']): ?>
                                    <div class="data-sub">Code: <?= htmlspecialchars($bb['branch_code']) ?></div>
                                <?php endif; ?>
                            </td>
                            <td class="text-end">
                                <div class="data-value amount warning">
                                    Rp <?= number_format($bb['pending_balance'], 0, ',', '.') ?>
                                </div>
                            </td>
                            <td class="text-end">
                                <div class="data-value amount success">
                                    Rp <?= number_format($bb['available_balance'], 0, ',', '.') ?>
                                </div>
                            </td>
                            <td class="text-end">
                                <div class="data-value amount primary">
                                    Rp <?= number_format($bb['total_withdrawn'], 0, ',', '.') ?>
                                </div>
                            </td>
                            <td class="text-end">
                                <div class="data-value amount info">
                                    Rp <?= number_format($bb['total_income'], 0, ',', '.') ?>
                                </div>
                            </td>
                            <td class="text-center">
                                <?php if ($bb['pending_withdrawals'] > 0): ?>
                                    <span class="badge-modern danger-count">
                                        🔥 <?= $bb['pending_withdrawals'] ?> pengajuan
                                    </span>
                                <?php else: ?>
                                    <span class="badge-modern secondary">-</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        
        <!-- Mobile Card View -->
        <div class="mobile-cards" style="padding: 16px;">
            <?php foreach ($branchBalances as $bb): ?>
                <div class="mobile-card">
                    <div class="mobile-card-header">
                        <div class="mobile-card-title">
                            <div class="mobile-card-title-main">
                                <i class="bi bi-building" style="color: #0066CC;"></i>
                                <?= htmlspecialchars($bb['branch_name'] ?? 'Unknown') ?>
                            </div>
                            <?php if ($bb['branch_code']): ?>
                                <div class="mobile-card-title-sub">Code: <?= htmlspecialchars($bb['branch_code']) ?></div>
                            <?php endif; ?>
                        </div>
                        <?php if ($bb['pending_withdrawals'] > 0): ?>
                            <span class="badge-modern danger-count">
                                🔥 <?= $bb['pending_withdrawals'] ?>
                            </span>
                        <?php endif; ?>
                    </div>
                    
                    <div class="mobile-card-body">
                        <div class="mobile-card-row">
                            <span class="label">⏳ Pending</span>
                            <span class="value" style="color: #FF9900; font-weight: 800;">
                                Rp <?= number_format($bb['pending_balance'], 0, ',', '.') ?>
                            </span>
                        </div>
                        <div class="mobile-card-row">
                            <span class="label">💰 Tersedia</span>
                            <span class="value" style="color: #00C853; font-weight: 800;">
                                Rp <?= number_format($bb['available_balance'], 0, ',', '.') ?>
                            </span>
                        </div>
                        <div class="mobile-card-row">
                            <span class="label">✅ Ditarik</span>
                            <span class="value" style="color: #0066CC; font-weight: 800;">
                                Rp <?= number_format($bb['total_withdrawn'], 0, ',', '.') ?>
                            </span>
                        </div>
                        <div class="mobile-card-row">
                            <span class="label">📈 Income</span>
                            <span class="value" style="color: #00B8D4; font-weight: 800;">
                                Rp <?= number_format($bb['total_income'], 0, ',', '.') ?>
                            </span>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- Pengajuan Penarikan -->
    <div class="card-modern">
        <div class="card-header-modern warning">
            <div class="title-left">
                <i class="bi bi-cash-stack"></i>
                <span>Pengajuan Penarikan</span>
            </div>
            <span class="navbar-badge" style="background: rgba(255,255,255,0.25);">
                <?= count($withdrawals) ?> Pengajuan
            </span>
        </div>
        
        <?php if (empty($withdrawals)): ?>
            <div class="empty-state">
                <div class="empty-state-icon">
                    <i class="bi bi-inbox"></i>
                </div>
                <h5 class="empty-state-title">Belum ada pengajuan penarikan</h5>
                <p class="empty-state-subtitle">Pengajuan dari cabang akan muncul di sini</p>
            </div>
        <?php else: ?>
            <!-- Desktop Table View -->
            <div class="desktop-table" style="overflow-x: auto;">
                <table class="table-modern">
                    <thead>
                        <tr>
                            <th>No. Pengajuan</th>
                            <th>Cabang</th>
                            <th class="text-end">Jumlah</th>
                            <th>Rekening</th>
                            <th>Status</th>
                            <th>Tgl Pengajuan</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($withdrawals as $w): ?>
                            <?php
                                $statusBadge = [
                                    'pending' => ['pending', '⏳ Menunggu Review'],
                                    'approved' => ['approved', '✅ Disetujui'],
                                    'rejected' => ['rejected', '❌ Ditolak'],
                                    'processed' => ['processed', '💸 Sudah Ditransfer'],
                                ];
                                $badge = $statusBadge[$w['status']] ?? ['secondary', $w['status']];
                            ?>
                            <tr>
                                <td>
                                    <code style="background: #f8fafc; padding: 3px 8px; border-radius: 6px; font-size: 0.8rem;">
                                        <?= htmlspecialchars($w['withdrawal_number']) ?>
                                    </code>
                                </td>
                                <td>
                                    <div class="data-value"><?= htmlspecialchars($w['branch_name']) ?></div>
                                </td>
                                <td class="text-end">
                                    <div class="data-value amount primary">
                                        Rp <?= number_format($w['amount'], 0, ',', '.') ?>
                                    </div>
                                </td>
                                <td>
                                    <div class="bank-info-mobile" style="margin: 0;">
                                        <strong><?= htmlspecialchars($w['bank_name']) ?></strong><br>
                                        <code><?= htmlspecialchars($w['account_number']) ?></code><br>
                                        <span style="color: #666;">a.n. <?= htmlspecialchars($w['account_holder']) ?></span>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge-modern <?= $badge[0] ?>">
                                        <?= $badge[1] ?>
                                    </span>
                                    <?php if ($w['review_notes']): ?>
                                        <div class="data-sub" style="font-style: italic; margin-top: 4px;" title="<?= htmlspecialchars($w['review_notes']) ?>">
                                            💬 <?= htmlspecialchars(substr($w['review_notes'], 0, 30)) ?><?= strlen($w['review_notes']) > 30 ? '...' : '' ?>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <div class="data-value" style="font-size: 0.85rem;">
                                        <?= date('d/m/Y', strtotime($w['requested_at'])) ?>
                                    </div>
                                    <div class="data-sub">
                                        <?= date('H:i', strtotime($w['requested_at'])) ?> · <?= htmlspecialchars($w['requester_name']) ?>
                                    </div>
                                </td>
                                <td>
                                    <?php if ($w['status'] === 'pending'): ?>
                                        <div class="action-buttons">
                                            <button class="btn-action approve" data-bs-toggle="modal" 
                                                    data-bs-target="#reviewModal<?= $w['id'] ?>" data-action="approve_withdrawal">
                                                <i class="bi bi-check-lg"></i> Approve
                                            </button>
                                            <button class="btn-action reject" data-bs-toggle="modal" 
                                                    data-bs-target="#reviewModal<?= $w['id'] ?>" data-action="reject_withdrawal">
                                                <i class="bi bi-x-lg"></i> Tolak
                                            </button>
                                        </div>
                                        
                                        <!-- Modal Review -->
                                        <div class="modal fade modal-modern" id="reviewModal<?= $w['id'] ?>" tabindex="-1">
                                            <div class="modal-dialog modal-dialog-centered">
                                                <form method="POST" id="reviewForm<?= $w['id'] ?>">
                                                    <?= csrfField() ?>
                                                    <input type="hidden" name="withdrawal_id" value="<?= $w['id'] ?>">
                                                    <input type="hidden" name="action" id="reviewAction<?= $w['id'] ?>" value="">
                                                    <div class="modal-content">
                                                        <div class="modal-header" id="modalHeader<?= $w['id'] ?>">
                                                            <h5 class="modal-title">
                                                                <i class="bi bi-clipboard-check-fill"></i>
                                                                <span id="modalTitle<?= $w['id'] ?>">Review Pengajuan</span>
                                                            </h5>
                                                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                                        </div>
                                                        <div class="modal-body">
                                                            <div class="review-info-box">
                                                                <strong>📋 No. Pengajuan:</strong> <?= htmlspecialchars($w['withdrawal_number']) ?><br>
                                                                <strong>🏢 Cabang:</strong> <?= htmlspecialchars($w['branch_name']) ?><br>
                                                                <strong>💰 Jumlah:</strong> Rp <?= number_format($w['amount'], 0, ',', '.') ?><br>
                                                                <strong>🏦 Rekening:</strong> <?= htmlspecialchars($w['bank_name']) ?> - <?= htmlspecialchars($w['account_number']) ?><br>
                                                                <strong>👤 Pemilik:</strong> <?= htmlspecialchars($w['account_holder']) ?>
                                                            </div>
                                                            <div class="form-group-modern">
                                                                <label class="form-label-modern">
                                                                    <span id="notesLabel<?= $w['id'] ?>">Catatan Review</span>
                                                                </label>
                                                                <textarea name="review_notes" class="form-control-modern" rows="3" 
                                                                          placeholder="Catatan untuk cabang..."></textarea>
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn-modal secondary" data-bs-dismiss="modal">Batal</button>
                                                            <button type="submit" class="btn-modal" id="modalSubmitBtn<?= $w['id'] ?>">
                                                                <i class="bi bi-check-circle-fill"></i>
                                                                <span id="modalBtnText<?= $w['id'] ?>">Submit</span>
                                                            </button>
                                                        </div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                        
                                    <?php elseif ($w['status'] === 'approved'): ?>
                                        <form method="POST" style="display: inline;" onsubmit="return confirm('Tandai dana sudah ditransfer?')">
                                            <?= csrfField() ?>
                                            <input type="hidden" name="action" value="mark_processed">
                                            <input type="hidden" name="withdrawal_id" value="<?= $w['id'] ?>">
                                            <button type="submit" class="btn-action primary">
                                                <i class="bi bi-check-all"></i> Sudah Ditransfer
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <div class="data-sub">
                                            <?= $w['reviewed_at'] ? date('d/m/Y', strtotime($w['reviewed_at'])) : '-' ?>
                                        </div>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- Mobile Card View -->
            <div class="mobile-cards" style="padding: 16px;">
                <?php foreach ($withdrawals as $w): ?>
                    <?php
                        $statusBadge = [
                            'pending' => ['pending', '⏳ Menunggu Review'],
                            'approved' => ['approved', '✅ Disetujui'],
                            'rejected' => ['rejected', '❌ Ditolak'],
                            'processed' => ['processed', '💸 Sudah Ditransfer'],
                        ];
                        $badge = $statusBadge[$w['status']] ?? ['secondary', $w['status']];
                    ?>
                    <div class="mobile-card">
                        <div class="mobile-card-header">
                            <div class="mobile-card-title">
                                <div class="mobile-card-title-main"><?= htmlspecialchars($w['branch_name']) ?></div>
                                <div class="mobile-card-title-sub">#<?= htmlspecialchars($w['withdrawal_number']) ?></div>
                            </div>
                            <div class="mobile-card-amount">
                                Rp <?= number_format($w['amount'], 0, ',', '.') ?>
                                <small><?= date('d/m/Y H:i', strtotime($w['requested_at'])) ?></small>
                            </div>
                        </div>
                        
                        <div class="mobile-card-body">
                            <div class="mobile-card-row">
                                <span class="label">Rekening</span>
                                <span class="value">
                                    <?= htmlspecialchars($w['bank_name']) ?>
                                    <div class="bank-info-mobile">
                                        <code><?= htmlspecialchars($w['account_number']) ?></code><br>
                                        a.n. <?= htmlspecialchars($w['account_holder']) ?>
                                    </div>
                                </span>
                            </div>
                            
                            <div class="mobile-card-row">
                                <span class="label">Request</span>
                                <span class="value"><?= htmlspecialchars($w['requester_name']) ?></span>
                            </div>
                            
                            <div class="mobile-card-row">
                                <span class="label">Status</span>
                                <span class="value">
                                    <span class="badge-modern <?= $badge[0] ?>"><?= $badge[1] ?></span>
                                </span>
                            </div>
                            
                            <?php if ($w['review_notes']): ?>
                                <div style="background: #fff9e6; padding: 8px 10px; border-radius: 6px; font-size: 0.8rem; color: #856404; border-left: 3px solid #FF9900;">
                                    💬 <?= htmlspecialchars($w['review_notes']) ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        
                        <?php if ($w['status'] === 'pending'): ?>
                            <div class="mobile-card-footer">
                                <button class="btn-action approve" data-bs-toggle="modal" 
                                        data-bs-target="#reviewModal<?= $w['id'] ?>" data-action="approve_withdrawal">
                                    <i class="bi bi-check-lg"></i> Approve
                                </button>
                                <button class="btn-action reject" data-bs-toggle="modal" 
                                        data-bs-target="#reviewModal<?= $w['id'] ?>" data-action="reject_withdrawal">
                                    <i class="bi bi-x-lg"></i> Tolak
                                </button>
                            </div>
                        <?php elseif ($w['status'] === 'approved'): ?>
                            <div class="mobile-card-footer">
                                <form method="POST" style="flex: 1; display: flex;" onsubmit="return confirm('Tandai dana sudah ditransfer?')">
                                    <?= csrfField() ?>
                                    <input type="hidden" name="action" value="mark_processed">
                                    <input type="hidden" name="withdrawal_id" value="<?= $w['id'] ?>">
                                    <button type="submit" class="btn-action primary" style="flex: 1;">
                                        <i class="bi bi-check-all"></i> Sudah Ditransfer
                                    </button>
                                </form>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
    </div>


    <!-- Transaksi Masuk Terbaru -->
    <div class="card-modern">
        <div class="card-header-modern info">
            <div class="title-left">
                <i class="bi bi-arrow-down-circle-fill"></i>
                <span>Transaksi Masuk Terbaru</span>
            </div>
            <span class="navbar-badge" style="background: rgba(255,255,255,0.25);">
                <?= count($recentTransactions) ?> Transaksi
            </span>
        </div>
        
        <?php if (empty($recentTransactions)): ?>
            <div class="empty-state">
                <div class="empty-state-icon">
                    <i class="bi bi-receipt"></i>
                </div>
                <h5 class="empty-state-title">Belum ada transaksi</h5>
                <p class="empty-state-subtitle">Transaksi masuk akan muncul di sini</p>
            </div>
        <?php else: ?>
            <!-- Desktop Table View -->
            <div class="desktop-table" style="overflow-x: auto;">
                <table class="table-modern">
                    <thead>
                        <tr>
                            <th>Tanggal</th>
                            <th>Cabang</th>
                            <th>Pelanggan</th>
                            <th>No. Tagihan</th>
                            <th class="text-end">Jumlah</th>
                            <th>Metode</th>
                            <th>Status</th>
                            <th>Release</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentTransactions as $t): ?>
                            <?php
                                $trxStatus = [
                                    'pending' => ['pending', '⏳ Pending'],
                                    'released' => ['released', '✅ Released'],
                                    'cancelled' => ['cancelled', '❌ Cancelled'],
                                ];
                                $s = $trxStatus[$t['status']] ?? ['secondary', $t['status']];
                            ?>
                            <tr>
                                <td>
                                    <div class="data-value" style="font-size: 0.85rem;">
                                        <?= date('d/m/Y', strtotime($t['created_at'])) ?>
                                    </div>
                                    <div class="data-sub"><?= date('H:i', strtotime($t['created_at'])) ?> WIB</div>
                                </td>
                                <td>
                                    <div class="data-value"><?= htmlspecialchars($t['branch_name']) ?></div>
                                </td>
                                <td>
                                    <div class="data-value" style="font-size: 0.85rem;">
                                        <?= htmlspecialchars($t['customer_name']) ?>
                                    </div>
                                </td>
                                <td>
                                    <code style="background: #f8fafc; padding: 3px 8px; border-radius: 6px; font-size: 0.8rem;">
                                        <?= htmlspecialchars($t['bill_number']) ?>
                                    </code>
                                </td>
                                <td class="text-end">
                                    <div class="data-value amount success">
                                        Rp <?= number_format($t['amount'], 0, ',', '.') ?>
                                    </div>
                                </td>
                                <td>
                                    <div class="data-value" style="font-size: 0.8rem;">
                                        <?= htmlspecialchars($t['payment_method'] ?? '-') ?>
                                    </div>
                                    <div class="data-sub">
                                        <?= htmlspecialchars($t['payment_channel'] ?? '-') ?>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge-modern <?= $s[0] ?>"><?= $s[1] ?></span>
                                </td>
                                <td>
                                    <?php if ($t['status'] === 'released'): ?>
                                        <div class="data-value" style="font-size: 0.85rem; color: #00C853;">
                                            ✅ <?= date('d/m/Y', strtotime($t['released_at'])) ?>
                                        </div>
                                    <?php else: ?>
                                        <div class="data-value" style="font-size: 0.85rem; color: #FF9900;">
                                            ⏳ <?= date('d/m/Y H:i', strtotime($t['pending_until'])) ?>
                                        </div>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- Mobile Card View -->
            <div class="mobile-cards" style="padding: 16px;">
                <?php foreach ($recentTransactions as $t): ?>
                    <?php
                        $trxStatus = [
                            'pending' => ['pending', '⏳ Pending'],
                            'released' => ['released', '✅ Released'],
                            'cancelled' => ['cancelled', '❌ Cancelled'],
                        ];
                        $s = $trxStatus[$t['status']] ?? ['secondary', $t['status']];
                    ?>
                    <div class="mobile-card">
                        <div class="mobile-card-header">
                            <div class="mobile-card-title">
                                <div class="mobile-card-title-main"><?= htmlspecialchars($t['customer_name']) ?></div>
                                <div class="mobile-card-title-sub">
                                    #<?= htmlspecialchars($t['bill_number']) ?> · <?= htmlspecialchars($t['branch_name']) ?>
                                </div>
                            </div>
                            <div class="mobile-card-amount" style="color: #00C853;">
                                Rp <?= number_format($t['amount'], 0, ',', '.') ?>
                                <small><?= date('d/m/Y H:i', strtotime($t['created_at'])) ?></small>
                            </div>
                        </div>
                        
                        <div class="mobile-card-body">
                            <div class="mobile-card-row">
                                <span class="label">Metode</span>
                                <span class="value">
                                    <?= htmlspecialchars($t['payment_method'] ?? '-') ?>
                                    <div style="color: #999; font-size: 0.75rem; font-weight: 500; margin-top: 2px;">
                                        <?= htmlspecialchars($t['payment_channel'] ?? '-') ?>
                                    </div>
                                </span>
                            </div>
                            
                            <div class="mobile-card-row">
                                <span class="label">Status</span>
                                <span class="value">
                                    <span class="badge-modern <?= $s[0] ?>"><?= $s[1] ?></span>
                                </span>
                            </div>
                            
                            <div class="mobile-card-row">
                                <span class="label">Release</span>
                                <span class="value">
                                    <?php if ($t['status'] === 'released'): ?>
                                        <span style="color: #00C853; font-weight: 700;">
                                            ✅ <?= date('d/m/Y', strtotime($t['released_at'])) ?>
                                        </span>
                                    <?php else: ?>
                                        <span style="color: #FF9900; font-weight: 700;">
                                            ⏳ <?= date('d/m/Y H:i', strtotime($t['pending_until'])) ?>
                                        </span>
                                    <?php endif; ?>
                                </span>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<script>
function toggleSidebar() {
    const sidebar = document.getElementById('sidebar');
    const overlay = document.getElementById('sidebarOverlay');
    if (sidebar && overlay) {
        sidebar.classList.toggle('active');
        overlay.classList.toggle('active');
        document.body.style.overflow = sidebar.classList.contains('active') ? 'hidden' : '';
    }
}

// Set action form berdasarkan tombol yang diklik
document.querySelectorAll('[data-bs-toggle="modal"]').forEach(btn => {
    btn.addEventListener('click', function() {
        const modal = document.querySelector(this.dataset.bsTarget);
        if (!modal) return;
        const action = this.dataset.action;
        const form = modal.querySelector('form');
        const actionInput = form.querySelector('input[name="action"]');
        actionInput.value = action;
        
        const modalId = modal.id.replace('reviewModal', '');
        const header = modal.querySelector('.modal-header');
        const title = modal.querySelector('#modalTitle' + modalId);
        const notesLabel = modal.querySelector('#notesLabel' + modalId);
        const submitBtn = modal.querySelector('#modalSubmitBtn' + modalId);
        const btnText = modal.querySelector('#modalBtnText' + modalId);
        
        if (action === 'approve_withdrawal') {
            header.className = 'modal-header success';
            title.textContent = 'Approve Pengajuan';
            notesLabel.textContent = 'Catatan (Opsional)';
            submitBtn.className = 'btn-modal success';
            btnText.textContent = 'Approve';
            submitBtn.querySelector('i').className = 'bi bi-check-circle-fill';
        } else {
            header.className = 'modal-header danger';
            title.textContent = 'Tolak Pengajuan';
            notesLabel.textContent = 'Alasan Penolakan *';
            submitBtn.className = 'btn-modal danger';
            btnText.textContent = 'Tolak';
            submitBtn.querySelector('i').className = 'bi bi-x-circle-fill';
        }
    });
});
</script>

<?php require __DIR__ . '/../includes/footer.php'; ?>