<?php

$pageTitle =
    'Đơn hàng của tôi - PET SPA';

$pageStyles = [
    'customer.css'
];

$activeCustomerPage =
    'orders';


require BASE_PATH
    . '/app/views/layouts/header.php';

require BASE_PATH
    . '/app/views/layouts/navbar.php';


$statusNames = [

    'pending' =>
        'Chờ xác nhận',

    'confirmed' =>
        'Đã xác nhận',

    'packing' =>
        'Đang đóng gói',

    'shipping' =>
        'Đang giao',

    'completed' =>
        'Hoàn thành',

    'cancelled' =>
        'Đã hủy'
];

?>

<main class="customer-area">

    <div class="container">

        <div class="row g-4">

            <div class="col-lg-3">

                <?php require BASE_PATH
                    . '/app/views/layouts/customer-sidebar.php';
                ?>

            </div>


            <div class="col-lg-9">

                <div class="customer-page-header">

                    <h1>
                        Đơn hàng của tôi
                    </h1>

                    <p>
                        Theo dõi tất cả đơn hàng đã đặt.
                    </p>

                </div>


                <!-- FILTER -->

                <div class="order-filter">

                    <?php

                    $filters = [
                        '' => 'Tất cả',
                        'pending' => 'Chờ xác nhận',
                        'confirmed' => 'Đã xác nhận',
                        'packing' => 'Đóng gói',
                        'shipping' => 'Đang giao',
                        'completed' => 'Hoàn thành',
                        'cancelled' => 'Đã hủy'
                    ];

                    ?>


                    <?php foreach (
                        $filters
                        as $key => $label
                    ): ?>

                        <a
                            href="<?= BASE_URL ?>/?url=customer/orders<?= $key !== ''
                                ? '&status=' . urlencode($key)
                                : ''
                            ?>"
                            class="<?= $status === $key
                                ? 'active'
                                : ''
                            ?>"
                        >
                            <?= htmlspecialchars(
                                $label
                            ) ?>
                        </a>

                    <?php endforeach; ?>

                </div>


                <?php if (
                    empty($orders)
                ): ?>

                    <div class="customer-empty">

                        <i class="bi bi-box-seam"></i>

                        <p>
                            Không có đơn hàng.
                        </p>

                    </div>

                <?php else: ?>

                    <div class="customer-order-list">

                        <?php foreach (
                            $orders
                            as $order
                        ): ?>

                            <article class="customer-order-card">

                                <div class="customer-order-header">

                                    <div>

                                        <strong>

                                            <?= htmlspecialchars(
                                                $order['order_code']
                                            ) ?>

                                        </strong>

                                        <small>

                                            <?= date(
                                                'd/m/Y H:i',
                                                strtotime(
                                                    $order['created_at']
                                                )
                                            ) ?>

                                        </small>

                                    </div>


                                    <span
                                        class="order-status status-<?= htmlspecialchars(
                                            $order['status']
                                        ) ?>"
                                    >

                                        <?= htmlspecialchars(
                                            $statusNames[
                                                $order['status']
                                            ]
                                            ?? $order['status']
                                        ) ?>

                                    </span>

                                </div>


                                <div class="customer-order-body">

                                    <div>

                                        <?= (int)
                                            $order['item_count']
                                        ?>

                                        sản phẩm

                                    </div>


                                    <div>

                                        Tổng tiền:

                                        <strong>

                                            <?= number_format(
                                                (float)
                                                $order['total_amount'],
                                                0,
                                                ',',
                                                '.'
                                            ) ?>đ

                                        </strong>

                                    </div>

                                </div>


                                <div class="customer-order-footer">

                                    <a
                                        href="<?= BASE_URL ?>/?url=customer/order&id=<?= (int)
                                            $order['id']
                                        ?>"
                                        class="btn btn-outline-dark"
                                    >
                                        Xem chi tiết
                                    </a>

                                </div>

                            </article>

                        <?php endforeach; ?>

                    </div>

                <?php endif; ?>


                <!-- PAGINATION -->

                <?php if (
                    $totalPages > 1
                ): ?>

                    <nav class="mt-4">

                        <ul
                            class="pagination justify-content-center"
                        >

                            <?php for (
                                $i = 1;
                                $i <= $totalPages;
                                $i++
                            ): ?>

                                <li
                                    class="page-item <?= $i === $page
                                        ? 'active'
                                        : ''
                                    ?>"
                                >

                                    <a
                                        class="page-link"
                                        href="<?= BASE_URL ?>/?url=customer/orders&page=<?= $i ?><?= $status !== ''
                                            ? '&status=' . urlencode($status)
                                            : ''
                                        ?>"
                                    >
                                        <?= $i ?>
                                    </a>

                                </li>

                            <?php endfor; ?>

                        </ul>

                    </nav>

                <?php endif; ?>

            </div>

        </div>

    </div>

</main>


<?php

require BASE_PATH
    . '/app/views/layouts/footer.php';

?>