<?php

session_start();
require_once __DIR__ . '/../../includes/auth.php';

require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Branch.php';
require_once __DIR__ . '/../../includes/dashboard_helper.php';

$database = new Database();
$conn = $database->getConnection();

$auth = new Auth($conn);

if (!$auth->isSuperAdmin($_SESSION['user_id'])) {
    header('Location: /unauthorized.php');
    exit();
}

$branch = new Branch($conn);

$branchesResult = $branch->getAllBranches();
$branches = $branchesResult->fetchAll(PDO::FETCH_ASSOC);
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap');

        :root { --accent: #7C3AED; --accent-dark: #5B21B6; --ink: #18181B; --sub: #71717A; --line: #E4E4E7; }

        .branch-select-page { font-family: 'Manrope', system-ui, -apple-system, sans-serif; color: var(--ink); max-width: 880px; margin: 0 auto; padding: 28px 20px 70px; }

        .top-strip a {
            color: var(--sub); text-decoration: none; font-size: .82rem; font-weight: 600;
            display: inline-flex; align-items: center; gap: 6px; margin-bottom: 24px;
        }
        .top-strip a:hover { color: var(--ink); }
        .top-strip a::before { content: '←'; font-size: .95rem; }

        .head { margin-bottom: 8px; }
        .head .eyebrow { font-size: .74rem; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); margin-bottom: 8px; }
        .head h1 { font-size: 1.7rem; font-weight: 800; letter-spacing: -.02em; margin: 0; }
        .head p { font-size: .9rem; color: var(--sub); margin-top: 6px; }

        .branch-grid {
            display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            gap: 16px; margin-top: 28px;
        }

        .branch-card {
            display: block; text-decoration: none; color: inherit;
            border: 1px solid var(--line); border-radius: 12px; padding: 20px;
            transition: border-color .15s, transform .15s, box-shadow .15s;
        }
        .branch-card:hover {
            border-color: var(--accent); transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(124, 58, 237, .12); text-decoration: none; color: inherit;
        }

        .branch-card .branch-name { font-size: 1.05rem; font-weight: 700; color: var(--ink); margin-bottom: 4px; }
        .branch-card .branch-id { font-size: .78rem; color: var(--sub); margin-bottom: 16px; }
        .branch-card .branch-cta {
            font-size: .82rem; font-weight: 700; color: var(--accent);
            display: inline-flex; align-items: center; gap: 6px;
        }
        .branch-card:hover .branch-cta { color: var(--accent-dark); }

        .empty-state {
            text-align: center; padding: 50px 20px; color: var(--sub);
            border: 1px dashed var(--line); border-radius: 12px; margin-top: 28px;
        }
        .empty-state h3 { color: var(--ink); font-size: 1.05rem; margin: 10px 0 6px; }
        .empty-state p { font-size: .88rem; }

        @media (max-width: 480px) {
            .branch-grid { grid-template-columns: 1fr; }
        }
    </style>
    <div class="branch-select-page">
        <div class="top-strip">
            <a href="<?php echo getDashboardLink(); ?>">Back to Dashboard</a>
        </div>

        <div class="head">
            <div class="eyebrow">Invoices</div>
            <h1>Select a Branch</h1>
            <p>Choose a branch to view, create, and manage its invoices.</p>
        </div>

        <?php if (!empty($branches)): ?>
            <div class="branch-grid">
                <?php foreach ($branches as $branchData): ?>
                    <a href="list_invoices.php?branch_id=<?php echo $branchData['branch_id']; ?>" class="branch-card">
                        <div class="branch-name"><?php echo htmlspecialchars($branchData['branch_name']); ?></div>
                        <div class="branch-id">Branch ID: <?php echo htmlspecialchars($branchData['branch_id']); ?></div>
                        <div class="branch-cta">View Invoices &rarr;</div>
                    </a>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="empty-state">
                <h3>No Branches Available</h3>
                <p>No branches have been set up in the system yet.</p>
            </div>
        <?php endif; ?>
    </div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
