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

if (!can('part.view')) {
    echo "<div class='alert alert-danger m-4'>Access denied</div>"; exit;
}

$page_js = 'parts.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 class="d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center gap-3 mb-4">
        <div>
            <span class="section-chip mb-2"><i class="bi bi-box-seam"></i> Inventory</span>
            <h2 class="mb-1">Spare Parts Inventory</h2>
            <p class="text-muted mb-0">Manage spare parts stock, categories, and transactions.</p>
        </div>
        <button class="btn btn-primary fw-semibold px-4" id="btnAddPart"><i class="bi bi-plus-lg me-2"></i>New Part</button>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3"><div class="card kpi-card shadow-sm border-0 h-100"><div class="card-body"><div class="kpi-label">Total Parts</div><div class="kpi-value" id="kpiTotalParts">0</div></div></div></div>
        <div class="col-6 col-md-3"><div class="card kpi-card shadow-sm border-0 h-100"><div class="card-body"><div class="kpi-label">Categories</div><div class="kpi-value" id="kpiCategories">0</div></div></div></div>
        <div class="col-6 col-md-3"><div class="card kpi-card border-0 h-100" style="background:#fef3c7;"><div class="card-body"><div class="kpi-label">Low Stock Items</div><div class="kpi-value" id="kpiLowStock">0</div></div></div></div>
        <div class="col-6 col-md-3"><div class="card kpi-card shadow-sm border-0 h-100"><div class="card-body"><div class="kpi-label">Stock Value (₦)</div><div class="kpi-value" id="kpiStockValue">0</div></div></div></div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body"><div class="row g-2 align-items-end">
            <div class="col-md-4"><label class="form-label small fw-semibold text-muted">Search</label><input type="text" class="form-control" id="partSearch" placeholder="Part name, number, supplier..."></div>
            <div class="col-md-3"><label class="form-label small fw-semibold text-muted">Category</label><select id="partCategoryFilter" class="form-select"><option value="">All</option></select></div>
            <div class="col-md-3"><label class="form-label small fw-semibold text-muted">Low Stock Only</label><select id="partLowStockFilter" class="form-select"><option value="">No</option><option value="1">Yes</option></select></div>
            <div class="col-md-2"><button class="btn btn-outline-secondary w-100" id="btnRefreshParts"><i class="bi bi-arrow-clockwise me-2"></i>Refresh</button></div>
            <div class="col-md-1">
                <div class="btn-group w-100" role="group">
                    <button class="btn btn-outline-secondary" id="btnExportPartsCsv" title="CSV"><i class="bi bi-filetype-csv"></i></button>
                    <button class="btn btn-outline-secondary" id="btnExportPartsXlsx" title="Excel"><i class="bi bi-file-earmark-excel"></i></button>
                    <button class="btn btn-outline-secondary" id="btnExportPartsPdf" title="PDF"><i class="bi bi-filetype-pdf"></i></button>
                </div>
            </div>
        </div></div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="table-responsive"><table class="table align-middle table-hover mb-0">
            <thead class="table-light"><tr>
                <th>Part Name</th><th>Part #</th><th>Category</th><th>Stock</th><th>Reorder</th><th>Unit Cost</th><th>Supplier</th><th>Location</th><th>Status</th><th class="text-end">Actions</th>
            </tr></thead>
            <tbody id="partTable"><tr><td colspan="10" class="text-center text-muted py-4">Loading...</td></tr></tbody>
        </table></div>
    </div>
</div>
</main>

<div class="modal fade" id="partModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered modal-dialog-scrollable"><div class="modal-content">
    <div class="modal-header"><div><h5 class="modal-title fw-semibold" id="partModalTitle">New Part</h5><p class="text-muted small mb-0 mt-1">Add a spare part to inventory</p></div><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
    <div class="modal-body">
        <form id="partForm">
            <input type="hidden" id="part_id">
            <div class="row g-3">
                <div class="col-12"><label class="form-label">Part Catalog Item</label><select id="part_catalog_id" class="form-select"><option value="">— Select from catalog —</option></select><small class="text-muted">Selecting an item will auto-fill name, part number, category, and unit of measure.</small></div>
                <div class="col-md-6"><label class="form-label">Part Name <span class="text-danger">*</span></label><input type="text" id="part_name" class="form-control"></div>
                <div class="col-md-6"><label class="form-label">Part Number</label><input type="text" id="part_part_number" class="form-control"></div>
                <div class="col-md-6"><label class="form-label">Category</label><select id="part_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="part_unit_of_measure" class="form-control" value="piece"></div>
                <div class="col-md-4"><label class="form-label">Qty in Stock</label><input type="number" step="0.01" id="part_quantity_in_stock" class="form-control" value="0"></div>
                <div class="col-md-4"><label class="form-label">Reorder Level</label><input type="number" step="0.01" id="part_reorder_level" class="form-control"></div>
                <div class="col-md-4"><label class="form-label">Unit Cost (₦)</label><input type="number" step="0.01" id="part_unit_cost" class="form-control"></div>
                <div class="col-md-6"><label class="form-label">Supplier</label><select id="part_supplier_id" class="form-select"><option value="">Select supplier</option></select></div>
                <div class="col-md-6"><label class="form-label">Supplier Name (override)</label><input type="text" id="part_supplier_name" class="form-control" placeholder="Leave blank to use supplier from setup"></div>
                <div class="col-md-6"><label class="form-label">Bin Location</label><select id="part_bin_location_id" class="form-select"><option value="">Select bin location</option></select></div>
                <div class="col-md-6"><label class="form-label">Bin Location (override)</label><input type="text" id="part_bin_location" class="form-control" placeholder="Leave blank to use from setup"></div>
                <div class="col-12"><label class="form-label">Description</label><textarea id="part_description" class="form-control" rows="2"></textarea></div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" id="savePart"><i class="bi bi-check-lg me-2"></i>Save Part</button>
    </div>
</div></div></div>

<div class="modal fade" id="transactionModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content">
    <div class="modal-header"><div><h5 class="modal-title fw-semibold">Stock Transaction</h5><p class="text-muted small mb-0 mt-1" id="transPartName">Record stock movement</p></div><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
    <div class="modal-body">
        <form id="transactionForm">
            <input type="hidden" id="trans_part_id">
            <div class="row g-3">
                <div class="col-12"><label class="form-label">Transaction Type</label><select id="trans_type" class="form-select"><option value="purchase">Purchase (stock in)</option><option value="usage">Usage (stock out)</option><option value="return">Return (stock in)</option><option value="adjustment">Adjustment</option></select></div>
                <div class="col-md-6"><label class="form-label">Quantity <span class="text-danger">*</span></label><input type="number" step="0.01" id="trans_quantity" class="form-control"></div>
                <div class="col-md-6"><label class="form-label">Unit Cost (₦)</label><input type="number" step="0.01" id="trans_unit_cost" class="form-control"></div>
                <div class="col-12"><label class="form-label">Notes</label><textarea id="trans_notes" class="form-control" rows="2"></textarea></div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" id="saveTransaction"><i class="bi bi-check-lg me-2"></i>Record Transaction</button>
    </div>
</div></div></div>

<script>window.CSRF_TOKEN = <?= json_encode(generateCsrfToken(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>;</script>
<?php include 'views/layouts/footer.php'; ?>
