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


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


$auth = new Auth($conn);


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

$branchId = isset($_GET['branch_id']) ? $_GET['branch_id'] : null;
$type = isset($_GET['type']) ? $_GET['type'] : null;

if (!$branchId || !in_array($type, ['simple', 'detailed'])) {
    header('Location: get_branch_inventory.php');
    exit();
}


$stmt = $conn->prepare("SELECT branch_name FROM branches WHERE branch_id = ?");
$stmt->execute([$branchId]);
$branchName = $stmt->fetchColumn();

if (!$branchName) {
    header('Location: get_branch_inventory.php');
    exit();
}

$inventory = new Inventory($conn);

if ($type === 'simple') {
    $items = $inventory->getSimpleItems($branchId);
    $pageTitle = "Simple Items";
} else {
    $items = $inventory->getDetailedItems($branchId);
    $pageTitle = "Detailed Items";
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<div class="container mt-5">
        <a href="get_branch_inventory.php" class="btn btn-link">← Back to Branch Inventory</a>
        <h1 class="mb-4"><?php echo $pageTitle; ?> for <?php echo htmlspecialchars($branchName); ?></h1>

        <?php if (empty($items)): ?>
            <div class="alert alert-info">No <?php echo strtolower($pageTitle); ?> found for this branch.</div>
        <?php else: ?>
            <table class="table table-bordered mt-4">
                <thead>
                    <tr>
                        <th>Item ID</th>
                        <th>Item Name</th>
                        <th>Item Details</th>
                        <th>Unique ID</th>
                        <th>Unit Price</th>
                        <?php if ($type === 'detailed'): ?>
                            <th>Warranty</th>
                            <th>Serial Number</th>
                            <th>Technical Specs</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $item): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($item['item_id']); ?></td>
                            <td><?php echo htmlspecialchars($item['item_name']); ?></td>
                            <td><?php echo htmlspecialchars($item['item_details'] ?? 'N/A'); ?></td>
                            <td><?php echo htmlspecialchars($item['unique_id']); ?></td>
                            <td><?php echo htmlspecialchars($item['unit_price']); ?></td>
                            <?php if ($type === 'detailed'): ?>
                                <td><?php echo htmlspecialchars($item['warranty']); ?></td>
                                <td><?php echo htmlspecialchars($item['serial_number']); ?></td>
                                <td><?php echo htmlspecialchars($item['technical_specs']); ?></td>
                            <?php endif; ?>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>