<?php
require_once __DIR__ . '/../../classes/Inventory.php';
include '../../includes/header.php';






$branchId = $auth->getUserBranch($_SESSION['user_id']);


if (!isset($inventory)) {
    $inventory = new Inventory($conn);
}


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




$itemType = isset($_GET['item_type']) ? $_GET['item_type'] : 'simple';




function getSearchFieldOptions($itemType, $currentField) {
    $commonOptions = [
        'unique_id' => 'Unique Code',
        'item_name' => 'Item Name',
        'item_details' => 'Item Details',
        'unit_price' => 'Price'
    ];

    $detailedOptions = [
        'serial_number' => 'Serial Number',
        'technical_specs' => 'Technical Specs'
    ];

    $options = $commonOptions;
  //  if ($itemType === 'detailed') {
        $options = array_merge($options, $detailedOptions);
   // }

    $html = '';
    foreach ($options as $value => $label) {
        $selected = $currentField === $value ? 'selected' : '';
        $html .= "<option value=\"{$value}\" {$selected}>{$label}</option>";
    }

    return $html;
}


function getTableHeaders($itemType) {
    $headers = ['Unique Code', 'Item Name', 'Item Details', 'category', 'Price', 'Date Entered'];

    $headers = array_merge($headers, ['Warranty', 'Serial Number', 'Technical Specs']);

    return $headers;
}


?>




<style>
    .toggle-buttons {
        margin-bottom: 20px;
    }

    .toggle-btn {
        padding: 8px 16px;
        margin-right: 10px;
        border: 1px solid #ddd;
        background-color: #f8f9fa;
        cursor: pointer;
        text-decoration: none;
        display: inline-block;
    }

    .toggle-btn.active {
        background-color: #007bff;
        color: white;
        border-color: #007bff;
    }

    .toggle-btn:hover {
        text-decoration: none;
        background-color: #e9ecef;
    }

    .toggle-btn.active:hover {
        background-color: #0056b3;
    }

    .search-container {
        margin-bottom: 20px;
    }

    .print-btn {
        margin-left: 10px;
    }

    @media print {
        .no-print {
            display: none;
        }
        .container {
            width: 100%;
            max-width: 100%;
        }
    }
</style>




<div class="container mt-3 mb-5">


    <div class="row mb-3">
        <div class="col-12">
            <a href="<?php echo getDashboardLink(); ?>" class="btn btn-secondary">Back to Dashboard</a>
        </div>
    </div>


    <h2 class="mb-4">Branch Missing items - <?php echo htmlspecialchars($branchName); ?></h2>

    <!--
    <div class="toggle-buttons no-print">
        <a href="?item_type=simple" class="toggle-btn <?php echo $itemType === 'simple' ? 'active' : ''; ?>">
            Simple Items
        </a>
        <a href="?item_type=detailed" class="toggle-btn <?php echo $itemType === 'detailed' ? 'active' : ''; ?>">
            Detailed Items
        </a>
    </div> -->

    <!--
    <div class="search-container no-print">
        <form method="GET" class="form-inline">
            <input type="hidden" name="item_type" value="<?php echo htmlspecialchars($itemType); ?>">
            <div class="form-row align-items-center">
                <div class="col-auto">
                    <select name="search_field" class="form-control mb-2">
                        <?php echo getSearchFieldOptions($itemType, $searchField); ?>
                    </select>
                </div>
                <div class="col-auto">
                    <input type="text"
                           name="search_term"
                           class="form-control mb-2"
                           placeholder="Search..."
                           value="<?php echo htmlspecialchars($searchTerm); ?>">
                </div>
                <div class="col-auto">
                    <button type="submit" class="btn btn-primary mb-2">Search</button>
                    <button type="button" class="btn btn-secondary mb-2 print-btn" onclick="window.print();">
                        Print
                    </button>
                </div>
            </div>
        </form>
    </div> -->


    <table class="table table-striped" id="example">
        <thead>
            <tr>
                <?php foreach (getTableHeaders($itemType) as $header): ?>
                    <th><?php echo htmlspecialchars($header); ?></th>
                <?php endforeach; ?>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>




<script>
    new DataTable('#example', {
        serverSide: true,
        processing: true,
        ordering: false,
        pageLength: 25,
        ajax: {
            url: '../../ajax/inventory/missing_items_data.php',
            type: 'GET'
        },
        columns: [
            { data: 'unique_id' },
            { data: 'item_name' },
            { data: 'item_details' },
            { data: 'category' },
            { data: 'unit_price' },
            { data: 'date_entered' },
            { data: 'warranty' },
            { data: 'serial_number' },
            { data: 'technical_specs' }
        ]
    });
</script>

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