<?php

$pageTitle = 'Lịch Spa của tôi - PET SPA';
$pageStyles = ['customer.css'];
$activeCustomerPage = 'bookings';

require_once BASE_PATH . '/app/helpers/Status.php';

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

?>

<main class="customer-area">

    <div class="container-fluid">

        <div class="row">

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


            <section class="col-lg-9 col-xl-10 py-4">


                <!-- HEADER -->

                <div
                    class="
                        d-flex
                        justify-content-between
                        align-items-center
                        mb-3
                    "
                >

                    <h1 class="h3 mb-0">
                        Lịch Spa của tôi
                    </h1>

                    <a
                        class="btn btn-primary"
                        href="<?= BASE_URL ?>/?url=customer/booking/create"
                    >
                        <i class="bi bi-calendar-plus me-1"></i>
                        Đặt lịch mới
                    </a>

                </div>



                <!-- FILTER STATUS -->

                <div class="mb-3 d-flex flex-wrap gap-1">

                    <?php foreach (
                        [
                            ''            => 'Tất cả',
                            'pending'     => 'Chờ xác nhận',
                            'confirmed'   => 'Đã xác nhận',
                            'in_progress' => 'Đang phục vụ',
                            'completed'   => 'Hoàn thành',
                            'cancelled'   => 'Đã hủy'
                        ]
                        as $key => $label
                    ): ?>

                        <a
                            class="
                                btn
                                btn-sm
                                <?= $status === $key
                                    ? 'btn-primary'
                                    : 'btn-outline-secondary'
                                ?>
                            "
                            href="<?= BASE_URL ?>/?url=customer/bookings&status=<?= urlencode($key) ?>"
                        >
                            <?= htmlspecialchars($label) ?>
                        </a>

                    <?php endforeach; ?>

                </div>



                <!-- TABLE -->

                <div class="table-responsive">

                    <table class="table align-middle">

                        <thead>

                            <tr>
                                <th>Mã lịch</th>
                                <th>Dịch vụ / thú cưng</th>
                                <th>Thời gian</th>
                                <th>Trạng thái</th>
                                <th></th>
                            </tr>

                        </thead>


                        <tbody>

                            <?php foreach ($bookings as $item): ?>

                                <tr>

                                    <!-- MÃ LỊCH -->

                                    <td>

                                        <?= htmlspecialchars(
                                            $item['booking_code']
                                        ) ?>

                                    </td>


                                    <!-- DỊCH VỤ + THÚ CƯNG -->

                                    <td>

                                        <?= htmlspecialchars(
                                            $item[
                                                'service_name_snapshot'
                                            ]
                                        ) ?>

                                        <br>

                                        <small class="text-muted">

                                            <?= htmlspecialchars(
                                                $item['pet_name']
                                            ) ?>

                                        </small>

                                    </td>


                                    <!-- THỜI GIAN -->

                                    <td>

                                        <?= htmlspecialchars(
                                            date(
                                                'd/m/Y',
                                                strtotime(
                                                    $item[
                                                        'booking_date'
                                                    ]
                                                )
                                            )
                                        ) ?>

                                        <br>

                                        <span class="text-muted">

                                            <?= htmlspecialchars(
                                                substr(
                                                    $item['start_time'],
                                                    0,
                                                    5
                                                )
                                            ) ?>

                                        </span>

                                    </td>


                                    <!-- TRẠNG THÁI -->

                                    <td>

                                        <span
                                            class="
                                                badge
                                                rounded-pill
                                                <?= htmlspecialchars(
                                                    booking_status_badge_class(
                                                        $item['status']
                                                    )
                                                ) ?>
                                            "
                                        >

                                            <?= htmlspecialchars(
                                                status_label(
                                                    'booking',
                                                    $item['status']
                                                )
                                            ) ?>

                                        </span>

                                    </td>


                                    <!-- CHI TIẾT -->

                                    <td>

                                        <a
                                            class="
                                                btn
                                                btn-sm
                                                btn-outline-primary
                                            "
                                            href="<?= BASE_URL ?>/?url=customer/booking&id=<?= (int) $item['id'] ?>"
                                        >
                                            Chi tiết
                                        </a>

                                    </td>

                                </tr>

                            <?php endforeach; ?>


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

                                <tr>

                                    <td
                                        colspan="5"
                                        class="
                                            text-center
                                            text-muted
                                            py-4
                                        "
                                    >
                                        Bạn chưa có lịch Spa nào.
                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>


            </section>

        </div>

    </div>

</main>


<?php
require BASE_PATH
    . '/app/views/layouts/footer.php';
?>