<?php
require_once __DIR__ . '/../../classes/Branding.php';
$companyName = Branding::companyName();
$companyLogo = Branding::logoUrl();
$companyLogoFile = Branding::logoFilePath();
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 (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}


if ($_SESSION['role'] != 'admin' && $_SESSION['role'] != 'warehouse') {
    header('Location: /IMS/unauthorized.php');
    exit;
}


$branch = new Branch($conn);
$branches = $branch->getAllBranches();


try {

    $totalShipmentsQuery = "SELECT COUNT(*) as total FROM shipments";
    $totalShipmentsStmt = $conn->prepare($totalShipmentsQuery);
    $totalShipmentsStmt->execute();
    $totalShipments = $totalShipmentsStmt->fetch(PDO::FETCH_ASSOC)['total'];


    $pendingShipmentsQuery = "SELECT COUNT(*) as pending FROM shipments WHERE status = 'pending'";
    $pendingShipmentsStmt = $conn->prepare($pendingShipmentsQuery);
    $pendingShipmentsStmt->execute();
    $pendingShipments = $pendingShipmentsStmt->fetch(PDO::FETCH_ASSOC)['pending'];


    $deliveredShipmentsQuery = "SELECT COUNT(*) as delivered FROM shipments WHERE status = 'delivered'";
    $deliveredShipmentsStmt = $conn->prepare($deliveredShipmentsQuery);
    $deliveredShipmentsStmt->execute();
    $deliveredShipments = $deliveredShipmentsStmt->fetch(PDO::FETCH_ASSOC)['delivered'];
} catch (Exception $e) {

    $totalShipments = 0;
    $pendingShipments = 0;
    $deliveredShipments = 0;
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', Arial, sans-serif;
            background: #f5f7fb;
            color: #1f2937;
            min-height: 100vh;
        }

        .page-shell {
            max-width: 1320px;
            margin: 0 auto;
            padding: 24px;
        }

        .dashboard-header {
            background: #243447;
            color: #fff;
            padding: 24px 0;
            margin-bottom: 24px;
            border-bottom: 4px solid #0d6efd;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 16px;
            max-width: 1320px;
            margin: 0 auto;
            padding: 0 24px;
        }

        .dashboard-header h1 {
            font-size: 28px;
            font-weight: 700;
            margin: 0 0 6px;
            letter-spacing: 0;
        }

        .dashboard-header p {
            color: #cbd5e1;
            margin: 0;
            font-size: 15px;
        }

        .back-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 14px;
            background: rgba(255, 255, 255, 0.08);
            color: #fff;
            text-decoration: none;
            border: 1px solid rgba(255, 255, 255, 0.18);
            border-radius: 6px;
            font-weight: 600;
            white-space: nowrap;
        }

        .back-link:hover {
            color: #fff;
            background: rgba(255, 255, 255, 0.16);
            text-decoration: none;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 16px;
            margin-bottom: 28px;
        }

        .stat-card {
            display: flex;
            align-items: center;
            gap: 16px;
            background: #fff;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            padding: 18px;
            box-shadow: 0 1px 4px rgba(15, 23, 42, 0.06);
        }

        a.stat-card {
            color: inherit;
            text-decoration: none;
        }

        a.stat-card:hover {
            border-color: #b6d4fe;
            box-shadow: 0 8px 18px rgba(15, 23, 42, 0.1);
            color: inherit;
            text-decoration: none;
        }

        .stat-icon {
            width: 48px;
            height: 48px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-size: 20px;
            flex: 0 0 auto;
        }

        .stat-card.total .stat-icon { background: #0d6efd; }
        .stat-card.pending .stat-icon { background: #f59f00; }
        .stat-card.delivered .stat-icon { background: #198754; }

        .stat-number {
            font-size: 30px;
            line-height: 1;
            font-weight: 700;
            margin-bottom: 4px;
            color: #111827;
        }

        .stat-label {
            color: #64748b;
            font-size: 14px;
            font-weight: 600;
        }

        .section-heading {
            display: flex;
            justify-content: space-between;
            align-items: end;
            gap: 16px;
            margin-bottom: 16px;
        }

        .section-title {
            font-size: 22px;
            font-weight: 700;
            color: #111827;
            margin: 0;
        }

        .section-subtitle {
            margin: 4px 0 0;
            color: #64748b;
            font-size: 14px;
        }

        .branches-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 16px;
        }

        .branch-card {
            background: #fff;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            padding: 18px;
            box-shadow: 0 1px 4px rgba(15, 23, 42, 0.06);
        }

        .branch-card-header {
            display: flex;
            align-items: flex-start;
            gap: 12px;
            min-height: 96px;
        }

        .branch-icon {
            width: 42px;
            height: 42px;
            border-radius: 8px;
            background: #e7f1ff;
            color: #0d6efd;
            display: flex;
            align-items: center;
            justify-content: center;
            flex: 0 0 auto;
            font-size: 18px;
        }

        .branch-title {
            font-size: 17px;
            font-weight: 700;
            color: #111827;
            margin: 0 0 6px;
            line-height: 1.3;
        }

        .branch-address {
            color: #64748b;
            font-size: 13px;
            line-height: 1.45;
            margin: 0;
        }

        .view-btn {
            width: 100%;
            min-height: 42px;
            margin-top: 16px;
            padding: 10px 14px;
            background: #0d6efd;
            border: 1px solid #0d6efd;
            border-radius: 6px;
            color: #fff;
            font-weight: 700;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }

        .view-btn:hover {
            background: #0b5ed7;
            border-color: #0b5ed7;
            color: #fff;
            text-decoration: none;
        }

        .empty-state {
            text-align: center;
            padding: 48px 24px;
            background: #fff;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            color: #64748b;
        }

        .empty-state i {
            font-size: 42px;
            margin-bottom: 12px;
            color: #94a3b8;
        }

        .empty-state h3 {
            color: #111827;
            font-size: 20px;
            margin-bottom: 8px;
        }

        @media (max-width: 900px) {
            .header-content,
            .section-heading {
                align-items: flex-start;
                flex-direction: column;
            }

            .stats-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 576px) {
            .page-shell,
            .header-content {
                padding-left: 14px;
                padding-right: 14px;
            }

            .dashboard-header h1 {
                font-size: 23px;
            }

            .branches-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
    <div class="dashboard-header">
        <div class="header-content">
            <div>
                <h1><i class="fas fa-boxes"></i> Shipment Management</h1>
                <p>Monitor and manage shipments across all branches</p>
            </div>
            <div>
                <?php if ($_SESSION['role'] == 'admin'): ?>
                    <a href="../dashboard/admin.php" class="back-link">
                        <i class="fas fa-arrow-left"></i> Admin Dashboard
                    </a>
                <?php else: ?>
                    <a href="../dashboard/warehouse.php" class="back-link">
                        <i class="fas fa-arrow-left"></i> Warehouse Dashboard
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="page-shell">

        <div class="stats-grid">
            <a href="view_shipments.php?branch_id=all&type=all" class="stat-card total" title="View all shipments">
                <div class="stat-icon">
                    <i class="fas fa-shipping-fast"></i>
                </div>
                <div>
                    <div class="stat-number"><?php echo number_format($totalShipments); ?></div>
                    <div class="stat-label">Total Shipments</div>
                </div>
            </a>

            <a href="view_shipments.php?branch_id=all&type=all&status=pending" class="stat-card pending" title="View pending shipments">
                <div class="stat-icon">
                    <i class="fas fa-clock"></i>
                </div>
                <div>
                    <div class="stat-number"><?php echo number_format($pendingShipments); ?></div>
                    <div class="stat-label">Pending Shipments</div>
                </div>
            </a>

            <a href="view_shipments.php?branch_id=all&type=all&status=delivered" class="stat-card delivered" title="View delivered shipments">
                <div class="stat-icon">
                    <i class="fas fa-check-circle"></i>
                </div>
                <div>
                    <div class="stat-number"><?php echo number_format($deliveredShipments); ?></div>
                    <div class="stat-label">Delivered Shipments</div>
                </div>
            </a>
        </div>


        <div class="section-heading">
            <div>
                <h2 class="section-title">Branch Shipments</h2>
                <p class="section-subtitle">Select a branch to view shipment history, approvals, and delivery status.</p>
            </div>
        </div>

        <?php

        $branchesArray = [];
        while ($row = $branches->fetch(PDO::FETCH_ASSOC)) {
            $branchesArray[] = $row;
        }
        ?>

        <?php if (count($branchesArray) > 0): ?>
            <div class="branches-grid">
                <?php foreach ($branchesArray as $row): ?>
                    <div class="branch-card">
                        <div class="branch-card-header">
                            <div class="branch-icon">
                                <i class="fas fa-store"></i>
                            </div>
                            <div>
                                <h3 class="branch-title"><?php echo htmlspecialchars($row['branch_name']); ?></h3>
                                <p class="branch-address"><?php echo htmlspecialchars($row['branch_address']); ?></p>
                            </div>
                        </div>
                        <a href="view_shipments.php?branch_id=<?php echo $row['branch_id']; ?>"
                           class="view-btn">
                            <i class="fas fa-eye"></i>
                            View Shipments
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="empty-state">
                <i class="fas fa-store-slash"></i>
                <h3>No Branches Available</h3>
                <p>There are currently no branches in the system. Please contact your administrator.</p>
            </div>
        <?php endif; ?>
    </div>

    <script>
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                const backLink = document.querySelector('.back-link');
                if (backLink) {
                    backLink.click();
                }
            }
        });
    </script>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
