<?php
// ========================================================
// 1. BACKEND DATA FETCH & DELETE LOGIC
// ========================================================
error_reporting(E_ALL);
ini_set('display_errors', 1);

$storage_file = 'accounts_data.json';

// --- DELETE LOGIC START ---
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['index'])) {
    if (file_exists($storage_file)) {
        $all_accounts = json_decode(file_get_contents($storage_file), true);
        $delete_index = intval($_GET['index']);
        
        if (isset($all_accounts[$delete_index])) {
            unset($all_accounts[$delete_index]);
            // Re-index the array to prevent gaps
            $all_accounts = array_values($all_accounts);
            file_put_contents($storage_file, json_encode($all_accounts, JSON_PRETTY_PRINT));
        }
    }
    // Redirect to clear the GET parameters
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}
// --- DELETE LOGIC END ---

$bank_accounts = [];
$cash_accounts = [];

if (file_exists($storage_file)) {
    $file_content = file_get_contents($storage_file);
    $all_accounts = json_decode($file_content, true);
    
    if (is_array($all_accounts)) {
        foreach ($all_accounts as $index => $account) {
            // Index save kar rahe hain taake delete karte waqt use ho sake
            $account['temp_index'] = $index; 
            
            if (isset($account['type']) && $account['type'] === 'BANK') {
                $bank_accounts[] = $account;
            } elseif (isset($account['type']) && $account['type'] === 'CASH') {
                $cash_accounts[] = $account;
            }
        }
    }
}

// Currency label helper - purani entries jin mein currency save nahi hui unke liye default Rs rahega
function get_currency_label($account) {
    if (isset($account['currency']) && strtoupper($account['currency']) === 'OMR') {
        return 'OMR';
    }
    return 'Rs.';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accounts Directory - Tameer Al Madina</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background: #F4F6F9;
    font-family: 'Poppins', sans-serif;
    min-height: 100vh;
}

.main-layout {
    display: flex;
    width: 100%;
    min-height: 100vh;
    position: relative;
}

/* Master Fixed Left Sidebar Layout */
aside, .sidebar, [class*="sidebar"] {
    width: 260px;
    min-width: 260px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transition: all 0.3s ease;
}

.content-wrapper {
    flex: 1;
    margin-left: 260px;
    display: flex;
    flex-direction: column;
    background: #F4F6F9;
    min-width: 0;
    width: calc(100% - 260px);
    transition: all 0.3s ease;
}

/* System Top Headers Section */
.top-navbar {
    height: 60px;
    background: #FFFFFF;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 30px;
    border-bottom: 1px solid #EAEAEA;
}

.navbar-left-flex {
    display: flex;
    align-items: center;
    gap: 15px;
}

.mobile-menu-toggle {
    display: none;
    font-size: 20px;
    color: #0A192F;
    cursor: pointer;
    background: #F4F6F9;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    border: 1px solid #E2E8F0;
}

.search-container {
    position: relative;
    width: 300px;
}

.search-container input {
    width: 100%;
    padding: 8px 15px 8px 40px;
    background: #F8F9FA;
    border: 1px solid #E2E8F0;
    border-radius: 8px;
    font-size: 13px;
    outline: none;
}

.search-container i {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #A0AEC0;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 20px;
}

.profile-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.profile-info i.profile-icon {
    font-size: 26px;
    color: #4A5568;
    background: #F4F6F9;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 1px solid #E2E8F0;
}

.profile-text h4 {
    font-size: 13px;
    color: #1A202C;
    font-weight: 600;
}

.profile-text p {
    font-size: 11px;
    color: #718096;
}

/* Page Fluid Workspace Content */
.page-container {
    padding: 20px 30px;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.form-card {
    background: #FFFFFF;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    width: 100%;
    display: flex;
    flex-direction: column;
    flex: 1;
}

.form-banner {
    background: linear-gradient(135deg, #0A192F 0%, #172A45 100%);
    padding: 15px 30px;
    color: #FFFFFF;
    border-bottom: 4px solid #ff6a00;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.banner-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.banner-icon {
    background: rgba(255, 255, 255, 0.1);
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    font-size: 16px;
}

.banner-text h2 {
    font-size: 18px;
    font-weight: 600;
}

.banner-text p {
    font-size: 11px;
    color: #A0AEC0;
}

/* Primary Operational Buttons Structure */
.btn-add-account {
    background: #0f2357;
    color: #FFFFFF;
    padding: 8px 18px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(15, 35, 87, 0.15);
    border: 1px solid rgba(255,255,255,0.1);
}

.btn-add-account:hover {
    background: #091636;
    transform: translateY(-1px);
}

/* System Sub-Section Titles */
.section-title {
    padding: 25px 30px 10px 30px;
    margin: 0;
    font-size: 16px;
    color: #0f2357;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Master Desktop Data Grid Design Configuration */
.table-responsive {
    padding: 5px 30px 20px 30px;
    width: 100%;
    overflow-x: auto;
    display: block;
}

table {
    width: 100%;
    border-collapse: collapse;
}

table thead {
    background: #0f2357;
}

table th {
    padding: 12px 16px;
    font-size: 12px;
    font-weight: 600;
    color: #FFFFFF;
    text-align: left;
    letter-spacing: 0.5px;
}

table th:first-child { border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
table th:last-child { border-top-right-radius: 4px; border-bottom-right-radius: 4px; }

table td {
    padding: 12px 16px;
    border-bottom: 1px solid #E2E8F0;
    font-size: 13px;
    color: #334155;
    background: #FFFFFF;
    vertical-align: middle;
}

table tbody tr:hover td {
    background: #F8FAFC;
}

.amount-cell {
    font-weight: 700;
    color: #10b981;
    text-align: right;
}

/* Delete Button Style */
.btn-delete {
    color: #ff0000;
    cursor: pointer;
    font-size: 14px;
    text-decoration: none;
}
.btn-delete:hover {
    color: #cc0000;
}

/* Badges Styling */
.badge {
    background-color: #e0f2fe;
    color: #0369a1;
    padding: 4px 10px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    display: inline-block;
}
.badge-cash {
    background-color: #ffedd5;
    color: #c2410c;
}

.separator {
    height: 1px;
    background-color: #e2e8f0;
    margin: 15px 30px;
}

/* Mobile View Responsive Cards Layout */
.mobile-cards-container {
    display: none;
    padding: 10px 20px 20px 20px;
    flex-direction: column;
    gap: 12px;
}

.mobile-card {
    background: #FFFFFF;
    border-radius: 8px;
    padding: 15px;
    border: 1px solid #E2E8F0;
    box-shadow: 0 2px 5px rgba(0,0,0,0.02);
}

.card-header-flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px dashed #E2E8F0;
    padding-bottom: 8px;
    margin-bottom: 10px;
}

.card-title-main {
    font-weight: 700;
    color: #0f2357;
    font-size: 14px;
}

.card-body-details {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
}

.detail-row .label {
    color: #64748b;
}

.detail-row .value {
    color: #334155;
    font-weight: 500;
}

.card-amount-big {
    font-size: 14px;
    font-weight: 700;
    color: #10b981;
}

.no-data {
    text-align: center;
    color: #64748b;
    padding: 35px;
    font-style: italic;
    font-size: 13px;
    background-color: #f8fafc;
    border-radius: 6px;
    border: 1px dashed #cbd5e1;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    z-index: 998;
}

@media (min-width: 992px) {
    body { overflow: hidden; }
    .page-container { height: calc(100vh - 60px); }
}

@media (max-width: 1024px) and (min-width: 769px) {
    aside, .sidebar, [class*="sidebar"] { width: 240px; min-width: 240px; }
    .content-wrapper { margin-left: 240px; width: calc(100% - 240px); }
}

@media (max-width: 768px) {
    body { overflow-y: auto !important; }
    .mobile-menu-toggle { display: inline-flex; }
    aside, .sidebar, [class*="sidebar"] { left: -260px !important; }
    aside.active, .sidebar.active, [class*="sidebar"].active { left: 0 !important; }
    .content-wrapper { margin-left: 0 !important; width: 100% !important; }
    .top-navbar { padding: 0 15px; }
    .search-container { width: 140px; }
    .page-container { padding: 10px; }
    .form-banner { padding: 12px 15px; flex-direction: column; gap: 15px; align-items: flex-start; }
    .btn-add-account { width: 100%; justify-content: center; }
    .section-title { padding: 15px 20px 5px 20px; }
    .separator { margin: 10px 20px; }
    
    /* Responsive View Switches */
    .table-responsive { display: none; }
    .mobile-cards-container { display: flex; }
}
</style>
</head>
<body>

<div class="sidebar-overlay" id="sidebarOverlay" onclick="toggleMobileSidebar()"></div>

<div class="main-layout">
    
    <?php 
    $sidebar_path = "/home/tameeralmadina/public_html/TAM/sidebar";
    if (file_exists($sidebar_path)) {
        include $sidebar_path;
    } elseif (file_exists($sidebar_path . ".php")) {
        include $sidebar_path . ".php";
    }
    ?>

    <div class="content-wrapper">
        
        <div class="top-navbar">
            <div class="navbar-left-flex">
                <div class="mobile-menu-toggle" onclick="toggleMobileSidebar()">
                    <i class="fa-solid fa-bars"></i>
                </div>
                <div class="search-container">
                    <i class="fa-solid fa-magnifying-glass"></i>
                    <input type="text" placeholder="Search parameters here...">
                </div>
            </div>
            
            <div class="user-profile">
                <div class="profile-info">
                    <i class="fa-solid fa-circle-user profile-icon"></i>
                    <div class="profile-text">
                        <h4>Admin</h4>
                        <p>Administrator</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="page-container">
            <div class="form-card">
                
                <div class="form-banner">
                    <div class="banner-left">
                        <div class="banner-icon">
                            <i class="fa-solid fa-wallet"></i>
                        </div>
                        <div class="banner-text">
                            <h2>Accounts Directory</h2>
                            <p>Manage and audit structural bank ledgers, internal petty cash, and liquid asset categories</p>
                        </div>
                    </div>
                    <a href="add-accounts.php" class="btn-add-account">
                        <i class="fa-solid fa-plus"></i> Add Account
                    </a>
                </div>

                <h3 class="section-title"><i class="fa-solid fa-building-columns"></i> Bank Accounts</h3>
                
                <div class="table-responsive">
                    <?php if (empty($bank_accounts)): ?>
                        <div class="no-data">No Bank Accounts Found</div>
                    <?php else: ?>
                        <table>
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Bank Name / Title</th>
                                    <th>Account Number</th>
                                    <th>Type</th>
                                    <th style="text-align: right;">Opening Balance</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($bank_accounts as $bank): ?>
                                    <tr>
                                        <td><strong><?php echo date('d-M-Y', strtotime($bank['date'])); ?></strong></td>
                                        <td><span style="color: #0f2357; font-weight: 600;"><?php echo htmlspecialchars($bank['bank_name']); ?></span></td>
                                        <td><code><?php echo htmlspecialchars($bank['account_number']); ?></code></td>
                                        <td><span class="badge">Bank</span></td>
                                        <td class="amount-cell"><?php echo get_currency_label($bank); ?> <?php echo number_format($bank['amount'], 2); ?></td>
                                        <td><a href="?action=delete&index=<?php echo $bank['temp_index']; ?>" class="btn-delete" onclick="return confirm('Are you sure?')"><i class="fa-solid fa-trash"></i></a></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                </div>

                <div class="mobile-cards-container">
                    <?php if (empty($bank_accounts)): ?>
                        <div class="no-data">No Bank Accounts Found</div>
                    <?php else: ?>
                        <?php foreach ($bank_accounts as $bank): ?>
                            <div class="mobile-card">
                                <div class="card-header-flex">
                                    <span class="card-title-main"><?php echo htmlspecialchars($bank['bank_name']); ?></span>
                                    <a href="?action=delete&index=<?php echo $bank['temp_index']; ?>" class="btn-delete" onclick="return confirm('Are you sure?')"><i class="fa-solid fa-trash"></i></a>
                                </div>
                                <div class="card-body-details">
                                    <div class="detail-row">
                                        <span class="label">A/C Number:</span>
                                        <span class="value"><code><?php echo htmlspecialchars($bank['account_number']); ?></code></span>
                                    </div>
                                    <div class="detail-row">
                                        <span class="label">Date:</span>
                                        <span class="value"><?php echo date('d-M-Y', strtotime($bank['date'])); ?></span>
                                    </div>
                                    <div class="detail-row" style="margin-top: 5px; border-top: 1px solid #f1f5f9; padding-top: 5px;">
                                        <span class="label" style="font-weight: 600; color:#0f2357;">Balance:</span>
                                        <span class="card-amount-big"><?php echo get_currency_label($bank); ?> <?php echo number_format($bank['amount'], 2); ?></span>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>

                <div class="separator"></div>

                <h3 class="section-title"><i class="fa-solid fa-money-bill-wave"></i> Cash / Petty Cash Accounts</h3>
                
                <div class="table-responsive">
                    <?php if (empty($cash_accounts)): ?>
                        <div class="no-data">No Cash Categories Found</div>
                    <?php else: ?>
                        <table>
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Cash Account Name / Description</th>
                                    <th>Type</th>
                                    <th style="text-align: right;">Opening Balance</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($cash_accounts as $cash): ?>
                                    <tr>
                                        <td><strong><?php echo date('d-M-Y', strtotime($cash['date'])); ?></strong></td>
                                        <td><span style="color: #0f2357; font-weight: 600;"><?php echo htmlspecialchars($cash['description']); ?></span></td>
                                        <td><span class="badge badge-cash">Cash</span></td>
                                        <td class="amount-cell"><?php echo get_currency_label($cash); ?> <?php echo number_format($cash['amount'], 2); ?></td>
                                        <td><a href="?action=delete&index=<?php echo $cash['temp_index']; ?>" class="btn-delete" onclick="return confirm('Are you sure?')"><i class="fa-solid fa-trash"></i></a></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                </div>

                <div class="mobile-cards-container">
                    <?php if (empty($cash_accounts)): ?>
                        <div class="no-data">No Cash Categories Found</div>
                    <?php else: ?>
                        <?php foreach ($cash_accounts as $cash): ?>
                            <div class="mobile-card">
                                <div class="card-header-flex">
                                    <span class="card-title-main"><?php echo htmlspecialchars($cash['description']); ?></span>
                                    <a href="?action=delete&index=<?php echo $cash['temp_index']; ?>" class="btn-delete" onclick="return confirm('Are you sure?')"><i class="fa-solid fa-trash"></i></a>
                                </div>
                                <div class="card-body-details">
                                    <div class="detail-row">
                                        <span class="label">Date:</span>
                                        <span class="value"><?php echo date('d-M-Y', strtotime($cash['date'])); ?></span>
                                    </div>
                                    <div class="detail-row" style="margin-top: 5px; border-top: 1px solid #f1f5f9; padding-top: 5px;">
                                        <span class="label" style="font-weight: 600; color:#0f2357;">Balance:</span>
                                        <span class="card-amount-big"><?php echo get_currency_label($cash); ?> <?php echo number_format($cash['amount'], 2); ?></span>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
                
            </div>
        </div>
    </div>
</div>

<script>
function toggleMobileSidebar() {
    const sidebarElements = document.querySelectorAll('aside, .sidebar, [class*="sidebar"]');
    const overlay = document.getElementById('sidebarOverlay');
    
    sidebarElements.forEach(sidebar => {
        sidebar.classList.toggle('active');
    });
    
    if(sidebarElements[0] && sidebarElements[0].classList.contains('active')) {
        overlay.style.display = 'block';
    } else {
        overlay.style.display = 'none';
    }
}
</script>
</body>
</html>