<?php
require_once __DIR__ . '/../config.php';
requireLogin();

$pageTitle = 'Procurement';
include __DIR__ . '/../includes/admin-header.php';
?>

        <div class="row g-4 mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">Purchase Orders</h5>
                            <div class="d-flex gap-2">
                                <input type="date" class="form-control form-control-sm" id="startDate" placeholder="Start Date">
                                <input type="date" class="form-control form-control-sm" id="endDate" placeholder="End Date">
                                <select class="form-select form-select-sm" id="statusFilter">
                                    <option value="">All Status</option>
                                    <option value="Pending">Pending</option>
                                    <option value="Approved">Approved</option>
                                    <option value="Partial">Partial</option>
                                    <option value="Received">Received</option>
                                </select>
                                <button class="btn btn-sm btn-primary" onclick="loadProcurement()">Filter</button>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm table-hover">
                                <thead>
                                    <tr>
                                        <th>Order ID</th>
                                        <th>Item</th>
                                        <th>Description</th>
                                        <th>Ordered Qty</th>
                                        <th>Received Qty</th>
                                        <th>Unit Price</th>
                                        <th>Supplier</th>
                                        <th>PO Date</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody id="procurementBody">
                                    <tr><td colspan="9" class="text-center">Loading...</td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
        function loadProcurement() {
            const startDate = document.getElementById('startDate').value;
            const endDate = document.getElementById('endDate').value;
            const status = document.getElementById('statusFilter').value;
            
            let url = '../api/procurement.php';
            const params = [];
            if (startDate) params.push('start_date=' + startDate);
            if (endDate) params.push('end_date=' + endDate);
            if (status) params.push('status=' + status);
            if (params.length) url += '?' + params.join('&');
            
            fetch(url)
                .then(r => r.json())
                .then(response => {
                    const tbody = document.getElementById('procurementBody');
                    if (!response.data || response.data.length === 0) {
                        tbody.innerHTML = '<tr><td colspan="9" class="text-center">No orders found</td></tr>';
                        return;
                    }
                    tbody.innerHTML = response.data.map(po => {
                        const statusClass = {
                            'Pending': 'bg-warning',
                            'Approved': 'bg-info',
                            'Partial': 'bg-primary',
                            'Received': 'bg-success'
                        }[po.Status] || 'bg-secondary';
                        return '<tr>' +
                            '<td>' + po.OrderID + '</td>' +
                            '<td>' + (po.ItemCode || '') + '</td>' +
                            '<td>' + (po.ItemDescription || '').substring(0, 50) + '</td>' +
                            '<td>' + (parseFloat(po.OrderedQty) || 0).toLocaleString() + '</td>' +
                            '<td>' + (parseFloat(po.Quantity_Received) || 0).toLocaleString() + '</td>' +
                            '<td>$' + (parseFloat(po.UnitPrice) || 0).toFixed(2) + '</td>' +
                            '<td>' + (po.Supplier || '') + '</td>' +
                            '<td>' + (po.PO_Date || '') + '</td>' +
                            '<td><span class="badge ' + statusClass + '">' + po.Status + '</span></td>' +
                        '</tr>';
                    }).join('');
                });
        }

        loadProcurement();
        </script>

<?php include __DIR__ . '/../includes/admin-footer.php'; ?>