<?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'])) {
    header('Location: ../../login.php');
    exit();
}
$auth->enforceSuperAdminPrivileges($_SESSION['user_id']);


$branch_id = $_GET['branch_id'] ?? null;
$type = $_GET['type'] ?? null;
if ($branch_id === null || $type === null) {
    die("Invalid request.");
}
$branch_id = (int)$branch_id;


$stmt = $conn->prepare("SELECT branch_name FROM branches WHERE branch_id = ?");
$stmt->execute([$branch_id]);
$branch_name = $stmt->fetchColumn();
if (!$branch_name) {
    die("Invalid branch.");
}


$inventory = new Inventory($conn);
if ($type === 'simple') {
    $items = $inventory->getSimpleItems($branch_id);
} elseif ($type === 'detailed') {
    $items = $inventory->getDetailedItems($branch_id);
} else {
    die("Invalid type.");
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<div class="container mt-5">
        <a href="choose_form.php?branch_id=<?php echo $branch_id; ?>" class="btn btn-link">← Back to Choose Form</a>
        <h1 class="mb-4"><?php echo ucfirst($type); ?> Items for Branch: <?php echo htmlspecialchars($branch_name); ?></h1>
        <?php if (isset($items) && count($items) > 0): ?>
            <table class="table table-bordered mt-4">
                <thead>
                    <tr>
                        <th>Item ID</th>
                        <th>Item Name</th>
                        <th>Item Details</th>
                        <th>Description</th>
                        <th>Quantity</th>
                        <?php if ($type === 'detailed'): ?>
                            <th>Brand</th>
                            <th>Model</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['description']); ?></td>
                            <td><?php echo htmlspecialchars($item['quantity']); ?></td>
                            <?php if ($type === 'detailed'): ?>
                                <td><?php echo htmlspecialchars($item['brand']); ?></td>
                                <td><?php echo htmlspecialchars($item['model']); ?></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 else: ?>
            <div class="alert alert-info">No items found for this branch.</div>
        <?php endif; ?>
    </div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>