<?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__ . '/../../classes/Shipment.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();
}

$auth->enforceSuperAdminOrWarehousePrivileges($_SESSION['user_id']);


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

<?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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            color: #2c3e50;
        }

        .dashboard-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 2rem 0;
            margin-bottom: 2rem;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        }

        .dashboard-header h1 {
            color: white;
            font-weight: 700;
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
            text-align: center;
        }

        .dashboard-header p {
            color: rgba(255, 255, 255, 0.9);
            font-size: 1.1rem;
            text-align: center;
            margin-bottom: 0;
        }

        .back-link {
            display: inline-flex;
            align-items: center;
            padding: 12px 24px;
            background: rgba(255, 255, 255, 0.1);
            color: white;
            text-decoration: none;
            border-radius: 50px;
            font-weight: 500;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            margin: 0 auto;
            display: block;
            width: fit-content;
        }

        .back-link:hover {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
        }

        .back-link i {
            margin-right: 8px;
            font-size: 1.1rem;
        }

        .container-fluid {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 2rem;
        }

        .branches-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
            gap: 2rem;
            padding: 1rem 0;
        }

        .branch-card {
            background: white;
            border-radius: 20px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            border: 1px solid rgba(255, 255, 255, 0.8);
            position: relative;
            overflow: hidden;
        }

        .branch-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            transform: scaleX(0);
            transition: transform 0.3s ease;
        }

        .branch-card:hover::before {
            transform: scaleX(1);
        }

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

        .branch-icon {
            width: 80px;
            height: 80px;
            border-radius: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 1.5rem;
            position: relative;
            overflow: hidden;
        }

        .branch-icon::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            transform: rotate(45deg);
            transition: all 0.6s ease;
            opacity: 0;
        }

        .branch-card:hover .branch-icon::before {
            opacity: 1;
            transform: rotate(45deg) translate(50%, 50%);
        }

        .branch-icon i {
            font-size: 2rem;
            color: white;
            z-index: 1;
        }

        .branch-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 0.5rem;
            text-align: center;
        }

        .branch-address {
            color: #7f8c8d;
            font-size: 0.95rem;
            text-align: center;
            margin-bottom: 2rem;
            line-height: 1.5;
        }

        .ship-btn {
            width: 100%;
            padding: 15px 20px;
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
            border: none;
            border-radius: 12px;
            color: white;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .ship-btn:hover {
            background: linear-gradient(135deg, #27ae60 0%, #229954 100%);
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(46, 204, 113, 0.3);
            color: white;
        }

        .ship-btn:active {
            transform: translateY(0);
        }

        .stats-bar {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 2rem;
        }

        .stat-item {
            text-align: center;
        }

        .stat-number {
            font-size: 2rem;
            font-weight: 700;
            color: #667eea;
            display: block;
        }

        .stat-label {
            font-size: 0.9rem;
            color: #7f8c8d;
            font-weight: 500;
        }

        .empty-state {
            text-align: center;
            padding: 4rem 2rem;
            color: #7f8c8d;
        }

        .empty-state i {
            font-size: 4rem;
            margin-bottom: 1rem;
            opacity: 0.5;
        }

        .empty-state h3 {
            margin-bottom: 1rem;
            color: #2c3e50;
        }


        @media (max-width: 768px) {
            .dashboard-header {
                padding: 1.5rem 0;
            }

            .dashboard-header h1 {
                font-size: 2rem;
            }

            .container-fluid {
                padding: 0 1rem;
            }

            .branches-grid {
                grid-template-columns: 1fr;
                gap: 1.5rem;
            }

            .branch-card {
                padding: 1.5rem;
            }

            .stats-bar {
                flex-direction: column;
                gap: 1rem;
            }

            .back-link {
                padding: 10px 20px;
                font-size: 0.9rem;
            }
        }

        @media (max-width: 480px) {
            .branch-icon {
                width: 60px;
                height: 60px;
            }

            .branch-icon i {
                font-size: 1.5rem;
            }

            .branch-title {
                font-size: 1.25rem;
            }
        }


        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }


        .branch-card {
            animation: fadeInUp 0.6s ease forwards;
            opacity: 0;
            transform: translateY(30px);
        }

        .branch-card:nth-child(1) { animation-delay: 0.1s; }
        .branch-card:nth-child(2) { animation-delay: 0.2s; }
        .branch-card:nth-child(3) { animation-delay: 0.3s; }
        .branch-card:nth-child(4) { animation-delay: 0.4s; }
        .branch-card:nth-child(5) { animation-delay: 0.5s; }
        .branch-card:nth-child(6) { animation-delay: 0.6s; }

        @keyframes fadeInUp {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>

    <div class="dashboard-header">
        <div class="container-fluid">
            <?php if ($_SESSION['role'] == 'admin'): ?>
                <a href="../dashboard/admin.php" class="back-link">
                    <i class="fas fa-arrow-left"></i> Back to Admin Dashboard
                </a>
            <?php else: ?>
                <a href="../dashboard/warehouse.php" class="back-link">
                    <i class="fas fa-arrow-left"></i> Back to Warehouse Dashboard
                </a>
            <?php endif; ?>

            <h1><i class="fas fa-shipping-fast"></i> Ship Items to Branches</h1>
            <p>Select a branch to ship items to. Items will be added to branch inventory after verification.</p>
        </div>
    </div>

    <div class="container-fluid">
        <?php

        $branchCount = 0;
        $branchesArray = [];


        while ($row = $branches->fetch(PDO::FETCH_ASSOC)) {
            if (strtolower($row['branch_name']) !== 'warehouse') {
                $branchesArray[] = $row;
                $branchCount++;
            }
        }
        ?>

        <div class="stats-bar">
            <div class="stat-item">
                <span class="stat-number"><?php echo $branchCount; ?></span>
                <span class="stat-label">Available Branches</span>
            </div>
        </div>

        <?php if ($branchCount > 0): ?>
            <div class="branches-grid">
                <?php foreach ($branchesArray as $index => $row): ?>
                    <div class="branch-card">
                        <div class="branch-icon">
                            <i class="fas fa-store"></i>
                        </div>
                        <h3 class="branch-title"><?php echo htmlspecialchars($row['branch_name']); ?></h3>
                        <p class="branch-address"><?php echo htmlspecialchars($row['branch_address']); ?></p>
                        <a href="ship_items_form.php?branch_id=<?php echo $row['branch_id']; ?>"
                           class="ship-btn"
                           onclick="this.innerHTML='<div class=\'loading\'></div> Processing...'; this.style.pointerEvents='none';">
                            <i class="fas fa-truck"></i>
                            Ship Items Here
                        </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 available for shipping. Please contact your administrator.</p>
            </div>
        <?php endif; ?>
    </div>

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        const shipButtons = document.querySelectorAll('.ship-btn');
        const modalEl = document.getElementById('selectionMethodModal');
        const modal = bootstrap.Modal.getOrCreateInstance(modalEl);
        const manualBtn = document.getElementById('manualSelectBtn');
        const barcodeBtn = document.getElementById('barcodeScanBtn');

        shipButtons.forEach(btn => {
            btn.addEventListener('click', function(e) {
                e.preventDefault();

                const shipmentUrl = this.href;

                manualBtn.dataset.targetUrl = shipmentUrl + '&mode=manual';
                barcodeBtn.dataset.targetUrl = shipmentUrl + '&mode=scanner';

                modal.show();


                this.innerHTML = '<i class="fas fa-truck"></i> Ship Items Here';
                this.style.pointerEvents = 'auto';
            });
        });

        [manualBtn, barcodeBtn].forEach(link => {
            link.addEventListener('click', function(e) {
                e.preventDefault();
                e.stopImmediatePropagation();
                if (this.dataset.targetUrl) {
                    window.location.href = this.dataset.targetUrl;
                }
            });
        });
    });
    </script>
    <div class="modal fade" id="selectionMethodModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content" style="border-radius: 15px;">
            <div class="modal-header">
                <h5 class="modal-title" id="modalLabel">Choose Item Selection Method</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body text-center">
                <p>How would you like to select items for this shipment?</p>
                <a href="#" id="manualSelectBtn" class="btn btn-primary btn-lg m-2" style="width: 200px;">
                    <i class="fas fa-tasks me-2"></i>Manual Selection
                </a>
                <a href="#" id="barcodeScanBtn" class="btn btn-success btn-lg m-2" style="width: 200px;">
                    <i class="fas fa-barcode me-2"></i>Barcode Scanning
                </a>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>