<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Branch.php';


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


$auth = new Auth($conn);


if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}


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>
        .add-items-dashboard {
            padding: 2rem 0;
            background-color: #f4f6f9;
            width: 100%;
            overflow: hidden;
        }

        .dashboard-return-link {
            display: inline-block;
            margin: 15px;
            padding: 10px 15px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        .dashboard-return-link:hover {
            background-color: #2980b9;
            color: white;
        }

        .dashboard-return-link i {
            margin-right: 8px;
        }

        .add-items-dashboard .row {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            padding: 15px;
            width: 100%;
            overflow-x: visible;
            margin: 0 !important;
            white-space: normal !important;
            max-width: 100%;
        }

        .container-fluid.add-items-dashboard {
            padding-left: 15px;
            padding-right: 15px;
            max-width: 100%;
            overflow-x: hidden;
        }

        .branch-card {
            margin-bottom: 15px;
            display: block;
            box-sizing: border-box;
            width: 100%;
            min-width: 0;
        }

        .add-items-dashboard .card-hover {
            height: 100%;
            display: flex;
            flex-direction: column;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            background-color: white;
            border: 1px solid #e1e1e1;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
        }

        .add-items-dashboard .card-hover:hover {
            transform: translateY(-10px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }

        .add-items-dashboard .card-body {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;
            text-align: center;
            padding: 1.5rem;
            flex-grow: 1;
        }

        .add-items-dashboard .card-title {
            font-size: 1.25rem;
            margin-bottom: 0.75rem;
            font-weight: bold;
            color: #2c3e50;
        }

        .add-items-dashboard .card-text {
            color: #7f8c8d;
            margin-bottom: 1rem;
            flex-grow: 1;
        }

        .add-items-dashboard .branch-btn {
            margin-top: auto;
            width: 100%;
            background-color: #2ecc71;
            border-color: #2ecc71;
            color: white;
            transition: background-color 0.3s ease;
        }

        .add-items-dashboard .branch-btn:hover {
            background-color: #27ae60;
            border-color: #27ae60;
        }

        @media (max-width: 768px) {
            .add-items-dashboard .row {
                padding: 0 15px;
            }
        }
    </style>
    <a href="../dashboard/admin.php" class="dashboard-return-link">
        <i class="fas fa-arrow-left"></i> Back to Dashboard
    </a>

    <div class="container-fluid add-items-dashboard">
        <h2 class="text-center mb-4">Branch Sales Reports</h2>
        <div class="row">
            <?php foreach ($branches as $branchItem): ?>
                <div class="branch-card">
                    <div class="card text-center card-hover">
                        <div class="card-body">
                            <h5 class="card-title"><?php echo htmlspecialchars($branchItem['branch_name']); ?></h5>
                            <p class="card-text"><?php echo htmlspecialchars($branchItem['branch_address']); ?></p>
                            <a href="branch_reports.php?branch_id=<?php echo $branchItem['branch_id']; ?>" class="btn btn-info branch-btn">
                                <i class="fas fa-chart-line"></i> View Sales Reports
                            </a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>

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