<?php
// Core config and database connection
require_once dirname(__DIR__, 3) . '/dripend/config/config.php';
require_once dirname(__DIR__, 3) . '/dripend/includes/db.php';

if (!isset($conn) && isset($db)) { $conn = $db; }

// DYNAMIC RELATION FETCH: Querying the store_types dictionary table
$store_types = [];
$type_query = $conn->query("SELECT id, type_name FROM store_types WHERE is_active = 1 ORDER BY type_name ASC");
if ($type_query) {
    while ($row = $type_query->fetch_assoc()) {
        $store_types[] = $row;
    }
}
?>

<div class="container-fluid py-4 px-4">
    <div class="d-flex justify-content-between align-items-center mb-4 pb-3 border-bottom">
        <div>
            <h2 class="h3 text-dark fw-bold m-0"><i class="fa-solid fa-shop text-primary me-2"></i>Provision New Inventory Store</h2>
            <p class="text-muted small m-0 mt-1">Define autonomous storage locations capable of serving multiple cross-departmental requisitions.</p>
        </div>
        <a href="?cat=inventory&page=inventory/listStores.php" class="btn btn-light border fw-bold text-secondary shadow-sm">
            <i class="fa-solid fa-arrow-left me-2"></i> Store Registry
        </a>
    </div>

    <div id="storeResponseMessage" class="d-none alert shadow-sm mb-4 rounded-3 border-0"></div>

    <form id="storeManagementForm" class="bg-white p-4 rounded-3 border shadow-sm" novalidate>
        <div class="row g-4 mb-4">
            <div class="col-md-6">
                <label class="form-label fw-bold text-secondary small text-uppercase">Store / Location Name <span class="text-danger">*</span></label>
                <input type="text" name="store_name" class="form-control border-primary shadow-sm" placeholder="e.g. Central Medical Store, Main Pharmacy" required>
                <div class="invalid-feedback">A unique operational store name is required.</div>
            </div>

            <div class="col-md-6">
                <label class="form-label fw-bold text-secondary small text-uppercase">Operational Hierarchy Type <span class="text-danger">*</span></label>
                <select name="store_type_id" class="form-select border-primary shadow-sm" required>
                    <option value="" selected disabled>-- Select Configuration Type From DB --</option>
                    <?php foreach ($store_types as $type): ?>
                        <option value="<?= (int)$type['id'] ?>"><?= htmlspecialchars($type['type_name']) ?></option>
                    <?php endforeach; ?>
                </select>
                <div class="invalid-feedback">Please select a valid operational tier classification.</div>
            </div>
        </div>

        <div class="d-flex justify-content-end gap-3 pt-3 border-top">
            <button type="submit" id="btnSubmitStore" class="btn btn-primary px-5 fw-bold shadow-sm">
                <i class="fa-solid fa-hard-drive me-2"></i> Provision Store
            </button>
        </div>
    </form>
</div>

<script src="<?php echo BASE_URL; ?>application/assets/js/storeManagement.js"></script>