<?php
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
requireLogin();

$userName = $_SESSION['user_name'] ?? 'User';
$page_js = 'part_catalog.js';

include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
?>

<main class="main-content">
<?php include 'views/layouts/topbar.php'; ?>

<div class="container-fluid p-4">
    <div id="alertBox" class="alert d-none"></div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body p-4 p-lg-5">
            <div class="row g-4 align-items-center">
                <div class="col-lg-8">
                    <span class="badge bg-primary bg-opacity-10 text-primary mb-3 px-3 py-2">Part Catalog Setup</span>
                    <h2 class="mb-2">Part Catalog</h2>
                    <p class="mb-0 text-muted">Predefined spare part names and numbers. Selecting a catalog item in Parts Inventory auto-fills the part name and part number.</p>
                </div>
                <div class="col-lg-4 text-lg-end">
                    <button type="button" class="btn btn-primary" id="btnAddItem"><i class="bi bi-plus-lg"></i> Add Catalog Item</button>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-12 col-md-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Total Items</div>
                            <div class="kpi-value" id="kpiTotal">0</div>
                        </div>
                        <div class="kpi-icon text-primary fs-3"><i class="bi bi-box-seam"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Active</div>
                            <div class="kpi-value" id="kpiActive">0</div>
                        </div>
                        <div class="kpi-icon text-success fs-3"><i class="bi bi-check-circle"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Categories</div>
                            <div class="kpi-value" id="kpiCategories">0</div>
                        </div>
                        <div class="kpi-icon text-info fs-3"><i class="bi bi-tags"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <div class="row mb-3">
                <div class="col-md-5">
                    <input type="text" id="searchBox" class="form-control" placeholder="Search by name or part number...">
                </div>
                <div class="col-md-3">
                    <select id="categoryFilter" class="form-select">
                        <option value="">All Categories</option>
                    </select>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>Name</th>
                            <th>Part Number</th>
                            <th>Category</th>
                            <th>Unit of Measure</th>
                            <th>Default Cost</th>
                            <th>Reorder Level</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody id="catalogTableBody">
                        <tr><td colspan="8" class="text-center text-muted py-4">Loading...</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Catalog Item Modal -->
<div class="modal fade" id="itemModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="itemModalTitle">Add Catalog Item</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="item_id" value="0">
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Part Name <span class="text-danger">*</span></label>
                        <input type="text" id="item_name" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Part Number</label>
                        <input type="text" id="item_part_number" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Category</label>
                        <select id="item_category_id" class="form-select">
                            <option value="">Select category</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Unit of Measure</label>
                        <input type="text" id="item_unit_of_measure" class="form-control" placeholder="e.g. Pieces, Litres, Kg">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Default Unit Cost (₦)</label>
                        <input type="number" step="0.01" id="item_default_unit_cost" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Default Reorder Level</label>
                        <input type="number" step="0.01" id="item_default_reorder_level" class="form-control">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="btnSaveItem">Save</button>
            </div>
        </div>
    </div>
</div>

</main>

<?php include 'views/layouts/footer.php'; ?>
