<?php
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; }

// 1. Dynamic lookups for Regions & Categories
$regions = $conn->query("SELECT id, region_name FROM ghana_regions WHERE is_active = 1 ORDER BY region_name ASC")->fetch_all(MYSQLI_ASSOC);
$categories = $conn->query("SELECT id, category_name FROM supplier_categories WHERE is_active = 1 ORDER BY id ASC")->fetch_all(MYSQLI_ASSOC);

// 2. Dynamic lookup for National ID Types
$nat_id_types = $conn->query("SELECT id, id_name FROM dict_national_id_types WHERE is_active = 1 ORDER BY id_name ASC")->fetch_all(MYSQLI_ASSOC);

// 3. Dynamic Reflection for Supplier Type ENUMs (Company / Individual)
$supplier_types_enum = [];
$type_query = $conn->query("SHOW COLUMNS FROM suppliers LIKE 'supplier_type'");
if ($type_query && $row = $type_query->fetch_assoc()) {
    preg_match_all("/'([^']+)'/", $row['Type'], $matches);
    if (!empty($matches[1])) { $supplier_types_enum = $matches[1]; }
}
?>

<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-handshake text-primary me-2"></i>Business Partner Registration</h2>
            <p class="text-muted small m-0 mt-1">Register suppliers, contractors, and individuals dynamically.</p>
        </div>
        <a href="?cat=inventory&page=inventory/listSuppliers.php" class="btn btn-light border fw-bold text-secondary shadow-sm">
            <i class="fa-solid fa-arrow-left me-2"></i> Partner Registry
        </a>
    </div>

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

    <form id="supplierManagementForm" class="bg-white p-4 rounded-3 border shadow-sm" novalidate>
        
        <div class="bg-light-subtle border p-3 rounded-3 mb-4">
            <label class="form-label fw-bold text-dark text-uppercase small"><i class="fa-solid fa-sitemap me-2 text-primary"></i>1. Entity Classification <span class="text-danger">*</span></label>
            <div class="d-flex gap-4 mt-2">
                <?php foreach ($supplier_types_enum as $index => $enum_val): ?>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="supplier_type" id="type_<?= $enum_val ?>" value="<?= $enum_val ?>" <?= $index === 0 ? 'checked' : '' ?> onchange="window.toggleSupplierType()">
                        <label class="form-check-label fw-bold" for="type_<?= $enum_val ?>"><?= $enum_val === 'Company' ? 'Company / Organization' : 'Individual Proprietor' ?></label>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

        <div id="section_company" class="row g-4 mb-4 border-bottom pb-4">
            <h6 class="fw-bold text-secondary mb-0"><i class="fa-regular fa-building me-2"></i>Organization Profile</h6>
            <div class="col-md-6">
                <label class="form-label text-secondary small fw-bold">Registered Company Name <span class="text-danger">*</span></label>
                <input type="text" name="company_name" id="req_company" class="form-control" placeholder="e.g. Ernest Chemists Limited" required>
            </div>
            <div class="col-md-3">
                <label class="form-label text-secondary small fw-bold">Registration Number</label>
                <input type="text" name="registration_number" class="form-control" placeholder="e.g. CS123452023">
            </div>
            <div class="col-md-3">
                <label class="form-label text-secondary small fw-bold">Tax ID (TIN)</label>
                <input type="text" name="tin_number" class="form-control font-monospace" placeholder="e.g. P0001234567">
            </div>
        </div>

        <div id="section_individual" class="row g-4 mb-4 border-bottom pb-4 d-none">
            <h6 class="fw-bold text-secondary mb-0"><i class="fa-regular fa-user me-2"></i>Personal Profile</h6>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">First Name <span class="text-danger">*</span></label>
                <input type="text" name="first_name" id="req_fname" class="form-control">
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Middle Name</label>
                <input type="text" name="middle_name" class="form-control">
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Last Name <span class="text-danger">*</span></label>
                <input type="text" name="last_name" id="req_lname" class="form-control">
            </div>
            <div class="col-md-6">
                <label class="form-label text-secondary small fw-bold">National ID Type (From DB)</label>
                <select name="national_id_type_id" class="form-select border-primary shadow-sm">
                    <option value="">-- Select Identity Document --</option>
                    <?php foreach($nat_id_types as $nid): ?>
                        <option value="<?= $nid['id'] ?>"><?= htmlspecialchars($nid['id_name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label text-secondary small fw-bold">National ID Number</label>
                <input type="text" name="national_id_number" class="form-control font-monospace" placeholder="GHA-123456789-0">
            </div>
        </div>

        <div class="row g-4 mb-4 border-bottom pb-4">
            <h6 class="fw-bold text-secondary mb-0"><i class="fa-solid fa-address-book me-2"></i>Contact & Logistics</h6>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Primary Contact Person</label>
                <input type="text" name="contact_person" class="form-control" placeholder="Name of rep">
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Mobile Number <span class="text-danger">*</span></label>
                <input type="text" name="mobile" class="form-control font-monospace" required>
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Email Address</label>
                <input type="email" name="email" class="form-control">
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Region</label>
                <select name="region_id" class="form-select">
                    <option value="">-- Select Region --</option>
                    <?php foreach ($regions as $r): ?>
                        <option value="<?= $r['id'] ?>"><?= htmlspecialchars($r['region_name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">City / Town</label>
                <input type="text" name="city" class="form-control">
            </div>
            <div class="col-md-4">
                <label class="form-label text-secondary small fw-bold">Digital Address (GPS)</label>
                <input type="text" name="digital_address" class="form-control font-monospace" placeholder="e.g. GA-123-4567">
            </div>
        </div>

        <div class="row g-4 mb-4">
            <div class="col-12">
                <h6 class="fw-bold text-primary mb-3"><i class="fa-solid fa-layer-group me-2"></i>Authorized Procurement Roles</h6>
                <div class="bg-light p-3 rounded-3 border">
                    <div class="row g-3">
                        <?php foreach ($categories as $cat): ?>
                            <div class="col-md-4">
                                <div class="form-check">
                                    <input class="form-check-input border-primary" type="checkbox" name="categories[]" value="<?= $cat['id'] ?>" id="cat_<?= $cat['id'] ?>">
                                    <label class="form-check-label text-dark fw-semibold" for="cat_<?= $cat['id'] ?>"><?= htmlspecialchars($cat['category_name']) ?></label>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="d-flex justify-content-end pt-3 border-top">
            <button type="submit" id="btnSubmitSupplier" class="btn btn-primary px-5 fw-bold shadow-sm">
                <i class="fa-solid fa-cloud-arrow-up me-2"></i> Register Business Partner
            </button>
        </div>
    </form>
</div>

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