<?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 (!$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>
        body {
            background-color: #f8f9fa;
            font-family: 'Roboto', sans-serif;
        }

        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
        }

        .page-header {
            text-align: center;
            margin-bottom: 40px;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }

        .page-header h1 {
            margin-bottom: 10px;
            font-weight: 300;
        }

        .page-header p {
            margin: 0;
            opacity: 0.9;
        }

        .branch-card {
            background: white;
            border-radius: 10px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            border-left: 4px solid #007bff;
        }

        .branch-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
        }

        .branch-name {
            font-size: 1.4rem;
            font-weight: 500;
            color: #2c3e50;
            margin-bottom: 10px;
        }

        .branch-id {
            color: #6c757d;
            font-size: 0.9rem;
            margin-bottom: 15px;
        }

        .view-loans-laybuys-btn {
            background: linear-gradient(45deg, #28a745, #20c997);
            border: none;
            padding: 10px 25px;
            border-radius: 25px;
            color: white;
            text-decoration: none;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            font-weight: 500;
        }

        .view-loans-laybuys-btn:hover {
            transform: scale(1.05);
            color: white;
            text-decoration: none;
            box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
        }

        .view-loans-laybuys-btn i {
            margin-right: 8px;
        }

        .back-btn {
            background: linear-gradient(45deg, #6c757d, #495057);
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            color: white;
            text-decoration: none;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            font-weight: 500;
            margin-bottom: 30px;
        }

        .back-btn:hover {
            transform: scale(1.05);
            color: white;
            text-decoration: none;
            box-shadow: 0 4px 15px rgba(108, 117, 125, 0.3);
        }

        .back-btn i {
            margin-right: 8px;
        }

        .no-branches {
            text-align: center;
            padding: 40px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .no-branches i {
            font-size: 4rem;
            color: #dee2e6;
            margin-bottom: 20px;
        }

        .stats-info {
            background: #e3f2fd;
            border-left: 4px solid #2196f3;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 30px;
        }

        .stats-info h5 {
            color: #1976d2;
            margin-bottom: 10px;
        }

        .stats-info p {
            margin: 0;
            color: #424242;
        }

        @media (max-width: 768px) {
            .container {
                margin: 20px auto;
                padding: 15px;
            }

            .page-header {
                margin-bottom: 30px;
                padding: 15px;
            }

            .branch-card {
                padding: 20px;
            }
        }
    </style>
    <div class="container">

        <a href="../admin.php" class="back-btn">
            <i class="fas fa-arrow-left"></i>
            Back to Dashboard
        </a>


        <div class="page-header">
            <h1><i class="fas fa-hand-holding-usd"></i> Lay-Buys Management</h1>
            <p>Select a branch to view and manage lay-buys</p>
        </div>


        <div class="stats-info">
            <h5><i class="fas fa-info-circle"></i> Information</h5>
            <p>Select a branch below to view lay-buys, create new records, and manage existing records for that specific location.</p>
        </div>


        <?php if (!empty($branches)): ?>
            <?php foreach ($branches as $branchData): ?>
                <div class="branch-card">
                    <div class="branch-name">
                        <i class="fas fa-building"></i>
                        <?php echo htmlspecialchars($branchData['branch_name']); ?>
                    </div>
                    <div class="branch-id">
                        Branch ID: <?php echo htmlspecialchars($branchData['branch_id']); ?>
                    </div>
                    <a href="laybuy_and_loans.php?branch_id=<?php echo $branchData['branch_id']; ?>"
                       class="view-loans-laybuys-btn">
                        <i class="fas fa-hand-holding-usd"></i>
                        View Lay-Buys
                    </a>
                </div>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="no-branches">
                <i class="fas fa-exclamation-triangle"></i>
                <h3>No Branches Available</h3>
                <p>No branches have been set up in the system yet. Please contact your system administrator to add branches.</p>
            </div>
        <?php endif; ?>
    </div>

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