<?php

$pageTitle =
    'Quản lý đơn hàng - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'orders';


$successMessage =
    $_SESSION['flash_success']
    ?? null;

$errorMessage =
    $_SESSION['flash_error']
    ?? null;


unset(
    $_SESSION['flash_success'],
    $_SESSION['flash_error']
);


$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'
];


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

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

?>

<main class="owner-area">

    <div class="container-fluid">

        <div class="row g-4">

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

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

            </div>


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

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

                    <div>

                        <h1>
                            Đơn hàng
                        </h1>

                        <p>

                            <?= number_format(
                                $totalOrders,
                                0,
                                ',',
                                '.'
                            ) ?>

                            đơn hàng.

                        </p>

                    </div>

                </div>


                <?php if ($successMessage): ?>

                    <div class="alert alert-success">

                        <?= htmlspecialchars(
                            $successMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <?php if ($errorMessage): ?>

                    <div class="alert alert-danger">

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <!-- FILTER -->

                <section class="owner-card mb-4">

                    <form
                        method="GET"
                        class="row g-3"
                    >

                        <input
                            type="hidden"
                            name="url"
                            value="owner/orders"
                        >


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

                            <input
                                type="text"
                                name="search"
                                class="form-control"
                                placeholder="Mã đơn, khách hàng, số điện thoại..."
                                value="<?= htmlspecialchars(
                                    $search
                                ) ?>"
                            >

                        </div>


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

                            <select
                                name="status"
                                class="form-select"
                            >

                                <option value="">
                                    Tất cả trạng thái
                                </option>


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

                                    <option
                                        value="<?= htmlspecialchars(
                                            $key
                                        ) ?>"

                                        <?= $status === $key
                                            ? 'selected'
                                            : ''
                                        ?>
                                    >

                                        <?= htmlspecialchars(
                                            $label
                                        ) ?>

                                    </option>

                                <?php endforeach; ?>

                            </select>

                        </div>


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

                            <button
                                class="btn btn-dark w-100"
                            >
                                Tìm kiếm
                            </button>

                        </div>

                    </form>

                </section>


                <!-- TABLE -->

                <section class="owner-card">

                    <div class="table-responsive">

                        <table
                            class="table align-middle owner-table"
                        >

                            <thead>

                                <tr>

                                    <th>Mã đơn</th>
                                    <th>Khách hàng</th>
                                    <th>Sản phẩm</th>
                                    <th>Tổng tiền</th>
                                    <th>Thanh toán</th>
                                    <th>Trạng thái</th>
                                    <th>Ngày đặt</th>
                                    <th></th>

                                </tr>

                            </thead>


                            <tbody>

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

                                    <tr>

                                        <td>

                                            <strong>

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

                                            </strong>

                                        </td>


                                        <td>

                                            <div>

                                                <strong>

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

                                                </strong>

                                                <small
                                                    class="d-block text-muted"
                                                >

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

                                                </small>

                                            </div>

                                        </td>


                                        <td>

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

                                        </td>


                                        <td>

                                            <strong>

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

                                            </strong>

                                        </td>


                                        <td>

                                            <?php if (
                                                $order[
                                                    'payment_status'
                                                ]
                                                === 'paid'
                                            ): ?>

                                                <span
                                                    class="badge bg-success"
                                                >
                                                    Đã thanh toán
                                                </span>

                                            <?php else: ?>

                                                <span
                                                    class="badge bg-warning text-dark"
                                                >
                                                    Chưa thanh toán
                                                </span>

                                            <?php endif; ?>

                                        </td>


                                        <td>

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

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

                                            </span>

                                        </td>


                                        <td>

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

                                        </td>


                                        <td>

                                            <a
                                                href="<?= BASE_URL ?>/?url=owner/order&id=<?= (int)
                                                    $order['id']
                                                ?>"
                                                class="btn btn-sm btn-light"
                                            >
                                                Chi tiết
                                            </a>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>


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

                                    <tr>

                                        <td
                                            colspan="8"
                                            class="text-center text-muted py-5"
                                        >
                                            Không tìm thấy đơn hàng.
                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </section>


                <!-- PAGINATION -->

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

                    <nav class="mt-4">

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

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

                                <?php

                                $query = $_GET;

                                $query['url'] =
                                    'owner/orders';

                                $query['page'] =
                                    $i;

                                ?>


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

                                    <a
                                        class="page-link"
                                        href="<?= BASE_URL ?>/?<?= htmlspecialchars(
                                            http_build_query(
                                                $query
                                            )
                                        ) ?>"
                                    >

                                        <?= $i ?>

                                    </a>

                                </li>

                            <?php endfor; ?>

                        </ul>

                    </nav>

                <?php endif; ?>

            </div>

        </div>

    </div>

</main>


<?php

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

?>