<?php
/**
 * @var object $orders
 * @var string $search
 * @var string $status
 * @var string $supplierId
 * @var string $dateFrom
 * @var string $dateTo
 * @var array $suppliers
 * @var bool $isGrn
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-<?= !empty($isGrn) ? 'clipboard-list' : 'shopping-cart' ?> me-2 text-gold"></i>
        <?= !empty($isGrn) ? 'Goods Received Notes' : 'Purchase Orders' ?></h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title"><?= !empty($isGrn) ? 'All GRNs' : 'All Purchase Orders' ?></h6>
        </div>
        <div class="card-header-right">
            <?php if (empty($isGrn) && can('purchases_create')): ?>
                <a href="<?= url('purchases/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Create PO
                </a>
            <?php endif; ?>
            <a href="<?= url('purchases/grn') ?>" class="btn btn-sm <?= !empty($isGrn) ? 'btn-gold' : 'btn-outline-gold' ?> ms-2">
                <i class="fas fa-clipboard-list me-1"></i>GRN List
            </a>
            <a href="<?= url('purchases') ?>" class="btn btn-sm <?= empty($isGrn) ? 'btn-gold' : 'btn-outline-gold' ?> ms-1">
                <i class="fas fa-shopping-cart me-1"></i>PO List
            </a>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3 g-2">
            <div class="col-md-4">
                <form method="GET" action="<?= !empty($isGrn) ? url('purchases/grn') : url('purchases') ?>" class="d-flex gap-2">
                    <div class="input-group">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-search text-gold"></i>
                        </span>
                        <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                               placeholder="Search by order number or supplier..." value="<?= e($search) ?>">
                        <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        <?php if (!empty($search)): ?>
                            <a href="<?= !empty($isGrn) ? url('purchases/grn') : url('purchases') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
            <?php if (empty($isGrn)): ?>
                <div class="col-md-2">
                    <select name="status" class="form-select form-select-sm bg-transparent" onchange="filterBy(this, 'status')">
                        <option value="">All Status</option>
                        <option value="draft" <?= $status === 'draft' ? 'selected' : '' ?>>Draft</option>
                        <option value="approved" <?= $status === 'approved' ? 'selected' : '' ?>>Approved</option>
                        <option value="received" <?= $status === 'received' ? 'selected' : '' ?>>Received</option>
                        <option value="cancelled" <?= $status === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="supplier_id" class="form-select form-select-sm bg-transparent" onchange="filterBy(this, 'supplier_id')">
                        <option value="">All Suppliers</option>
                        <?php foreach ($suppliers as $sup): ?>
                            <option value="<?= $sup->id ?>" <?= $supplierId == $sup->id ? 'selected' : '' ?>>
                                <?= e($sup->name) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_from" class="form-control form-control-sm bg-transparent"
                           value="<?= e($dateFrom) ?>" onchange="filterBy(this, 'date_from')" placeholder="From">
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_to" class="form-control form-control-sm bg-transparent"
                           value="<?= e($dateTo) ?>" onchange="filterBy(this, 'date_to')" placeholder="To">
                </div>
            <?php endif; ?>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th><?= !empty($isGrn) ? 'GRN Number' : 'Order Number' ?></th>
                        <?php if (!empty($isGrn)): ?>
                            <th>PO Reference</th>
                        <?php endif; ?>
                        <th>Supplier</th>
                        <?php if (empty($isGrn)): ?>
                            <th>Order Date</th>
                            <th>Expected</th>
                        <?php else: ?>
                            <th>Received Date</th>
                        <?php endif; ?>
                        <th class="text-end">Total</th>
                        <th>Status</th>
                        <?php if (empty($isGrn)): ?>
                            <th>Created By</th>
                        <?php else: ?>
                            <th>Received By</th>
                        <?php endif; ?>
                        <th width="80" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($orders->data)): ?>
                        <tr>
                            <td colspan="<?= !empty($isGrn) ? 8 : 9 ?>" class="text-center text-muted py-4">
                                <i class="fas fa-<?= !empty($isGrn) ? 'clipboard' : 'cart-plus' ?> fa-2x mb-2"></i>
                                <p class="mb-0">No <?= !empty($isGrn) ? 'GRNs' : 'purchase orders' ?> found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($orders->currentPage - 1) * $orders->perPage + 1; ?>
                        <?php foreach ($orders->data as $order): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= !empty($isGrn) ? url('purchases/grn/' . $order->id) : url('purchases/show/' . $order->id) ?>"
                                       class="text-gold text-decoration-none">
                                        <strong><?= e($order->grn_number ?? $order->order_number) ?></strong>
                                    </a>
                                </td>
                                <?php if (!empty($isGrn)): ?>
                                    <td>
                                        <a href="<?= url('purchases/show/' . $order->purchase_order_id) ?>" class="text-gold">
                                            <?= e($order->order_number ?? '-') ?>
                                        </a>
                                    </td>
                                <?php endif; ?>
                                <td><?= e($order->supplier_name ?? '-') ?></td>
                                <?php if (empty($isGrn)): ?>
                                    <td><?= formatDate($order->order_date ?? $order->created_at) ?></td>
                                    <td><?= !empty($order->expected_date) ? formatDate($order->expected_date) : '-' ?></td>
                                <?php else: ?>
                                    <td><?= formatDate($order->received_date ?? $order->created_at) ?></td>
                                <?php endif; ?>
                                <td class="text-end"><?= currency($order->total_amount ?? 0) ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($order->status ?? 'draft') ?>">
                                        <?= e(ucfirst($order->status ?? 'draft')) ?>
                                    </span>
                                </td>
                                <td>
                                    <small><?= e($order->user_first_name ?? '') ?> <?= e($order->user_last_name ?? '') ?></small>
                                </td>
                                <td class="text-center">
                                    <a href="<?= !empty($isGrn) ? url('purchases/grn/' . $order->id) : url('purchases/show/' . $order->id) ?>"
                                       class="btn btn-sm btn-outline-info" title="View">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($orders->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($orders->currentPage - 1) * $orders->perPage) + 1 ?>
                    to <?= min($orders->currentPage * $orders->perPage, $orders->total) ?>
                    of <?= $orders->total ?> entries
                </div>
                <?= paginateLinks($orders, !empty($isGrn) ? url('purchases/grn') : url('purchases')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
function filterBy(el, param) {
    const url = new URL(window.location.href);
    url.searchParams.set(param, el.value);
    url.searchParams.delete('page');
    window.location.href = url.toString();
}
EOS;
?>
