<?php
require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../app/orders.php';

$user = sparrow_require_backyard_login();

$orders = sparrow_get_trashed_orders();

sparrow_backyard_header('Trashed Orders');
?>

<section class="card">
    <h1>Trashed Orders</h1>

    <p class="muted">
        These orders are hidden from the dashboard. Restore them if they were trashed by mistake, or permanently delete them when you are sure.
    </p>

    <p>
        <a class="button button-secondary" href="<?= sparrow_escape(sparrow_url('/backyard/dashboard.php')) ?>">
            Back to Dashboard
        </a>
    </p>

    <?php if (!$orders): ?>
        <p class="muted">No trashed orders found.</p>
    <?php else: ?>

        <form method="post" action="<?= sparrow_escape(sparrow_url('/backyard/orders-trash.php')) ?>">

            <div class="orders-toolbar">
                <div class="bulk-actions">
                    <select name="bulk_action" class="bulk-action-select">
                        <option value="">Bulk action...</option>
                        <option value="restore">Restore Selected</option>
                        <option value="delete_permanent">Delete Permanently</option>
                    </select>

                    <button type="submit" class="button button-small"
                        onclick="return confirmTrashBulkAction();">
                        Apply
                    </button>
                </div>
            </div>

            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>
                                <input type="checkbox" id="select_all_orders">
                            </th>
                            <th>Order</th>
                            <th>Customer</th>
                            <th>Status</th>
                            <th>Qty</th>
                            <th>Details</th>
                            <th>Payment</th>
                            <th>Order Date</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php foreach ($orders as $order): ?>
                            <?php
                            $trash_total = (float) ($order['total_amount'] ?? 0);
                            $trash_paid = (float) ($order['paid_amount'] ?? 0);
                            $trash_blessing = (float) ($order['blessing_amount'] ?? 0);
                            $trash_discount = (float) ($order['order_discount_amount'] ?? 0);
                            $trash_is_gift = !empty($order['is_gift']);
                            $trash_balance = $trash_is_gift
                                ? 0.00
                                : max(0, $trash_total - $trash_discount - $trash_paid);
                            ?>
                            <tr>
                                <td>
                                    <input type="checkbox" name="order_ids[]" value="<?= (int) $order['id'] ?>">
                                </td>

                                <td>
                                    <strong><?= sparrow_escape($order['order_number'] ?? '') ?></strong>
                                </td>

                                <td>
                                    <?= sparrow_escape($order['customer_name'] ?? '') ?>
                                </td>

                                <td>
                                    <span class="status-pill">
                                        <?= sparrow_escape($order['status'] ?? '') ?>
                                    </span>
                                </td>

                                <td>
                                    <?= (int) ($order['total_quantity'] ?? $order['quantity'] ?? 0) ?>
                                </td>

                                <td>
                                    <?= sparrow_escape($order['item_summary'] ?? '') ?>
                                </td>

                                <td>
                                    <div class="dashboard-payment-pills">
                                        <span class="status-pill status-pill-total">
                                            Total: $<?= number_format($trash_total, 2) ?>
                                        </span>

                                        <span class="status-pill status-pill-paid">
                                            Paid: $<?= number_format($trash_paid, 2) ?>
                                        </span>

                                        <span class="status-pill status-pill-blessing">
                                            Blessing: $<?= number_format($trash_blessing, 2) ?>
                                        </span>

                                        <span class="status-pill status-pill-discount">
                                            Discount: $<?= number_format($trash_discount, 2) ?>
                                        </span>

                                        <span class="status-pill status-pill-balance">
                                            Balance: $<?= number_format($trash_balance, 2) ?>
                                        </span>
                                    </div>
                                </td>

                                <td>
                                    <?= sparrow_escape($order['order_date'] ?? '') ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </form>

    <?php endif; ?>
</section>

<script>
const selectAllOrders = document.getElementById('select_all_orders');

if (selectAllOrders) {
    selectAllOrders.addEventListener('change', function () {
        document.querySelectorAll('input[name="order_ids[]"]').forEach(function (checkbox) {
            checkbox.checked = selectAllOrders.checked;
        });
    });
}

function confirmTrashBulkAction() {
    const actionSelect = document.querySelector('select[name="bulk_action"]');
    const checkedOrders = document.querySelectorAll('input[name="order_ids[]"]:checked');

    if (!actionSelect || actionSelect.value === '') {
        alert('Choose a bulk action first.');
        return false;
    }

    if (checkedOrders.length === 0) {
        alert('Select at least one order first.');
        return false;
    }

    if (actionSelect.value === 'restore') {
        return confirm('Restore the selected orders back to the dashboard?');
    }

    if (actionSelect.value === 'delete_permanent') {
        return confirm('Permanently delete the selected orders? This cannot be undone.');
    }

    return true;
}
</script>

<?php sparrow_backyard_footer(); ?>
