<?php

$pageTitle = 'Lịch hẹn Spa - PET SPA';
$pageStyles = ['owner.css'];
$activeOwnerPage = 'bookings';

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

$bookingStatusClasses = [
    'pending'     => 'text-bg-warning',
    'confirmed'   => 'text-bg-primary',
    'in_progress' => 'text-bg-info',
    'completed'   => 'text-bg-success',
    'cancelled'   => 'text-bg-danger',
];

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">

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


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

                <h1 class="h3 mb-3">
                    Lịch hẹn Spa
                </h1>


                <!-- =========================================
                     BỘ LỌC
                ========================================== -->

                <form
                    method="GET"
                    class="row g-2 mb-4"
                >

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


                    <!-- NGÀY -->

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

                        <input
                            class="form-control"
                            type="date"
                            name="date"
                            value="<?= htmlspecialchars(
                                $filters['date'] ?? ''
                            ) ?>"
                        >

                    </div>


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

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

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

                            <option value="">
                                Mọi trạng thái
                            </option>


                            <?php foreach (
                                [
                                    'pending',
                                    'confirmed',
                                    'in_progress',
                                    'completed',
                                    'cancelled'
                                ]
                                as $status
                            ): ?>

                                <option
                                    value="<?= htmlspecialchars($status) ?>"
                                    <?= ($filters['status'] ?? '') === $status
                                        ? 'selected'
                                        : ''
                                    ?>
                                >
                                    <?= htmlspecialchars(
                                        status_label(
                                            'booking',
                                            $status
                                        )
                                    ) ?>
                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>


                    <!-- TÌM KIẾM -->

                    <div class="col-md-4">

                        <input
                            class="form-control"
                            type="text"
                            name="q"
                            value="<?= htmlspecialchars(
                                $filters['q'] ?? ''
                            ) ?>"
                            placeholder="Mã lịch, khách, thú cưng"
                        >

                    </div>


                    <!-- NÚT LỌC -->

                    <div class="col-md-2">

                        <button
                            type="submit"
                            class="btn btn-outline-primary w-100"
                        >
                            <i class="bi bi-funnel me-1"></i>
                            Lọc
                        </button>

                    </div>

                </form>



                <!-- =========================================
                     DANH SÁCH LỊCH
                ========================================== -->

                <div class="table-responsive">

                    <table class="table align-middle">

                        <thead>

                            <tr>

                                <th>
                                    Mã lịch
                                </th>

                                <th>
                                    Khách / thú cưng
                                </th>

                                <th>
                                    Dịch vụ
                                </th>

                                <th>
                                    Lịch
                                </th>

                                <th>
                                    Trạng thái
                                </th>

                                <th class="text-end">
                                    Thao tác
                                </th>

                            </tr>

                        </thead>


                        <tbody>


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


                                <?php

                                $status =
                                    (string) (
                                        $item['status']
                                        ?? ''
                                    );

                                $badgeClass =
                                    $bookingStatusClasses[
                                        $status
                                    ]
                                    ?? 'text-bg-secondary';

                                ?>


                                <tr>


                                    <!-- MÃ -->

                                    <td>

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

                                    </td>



                                    <!-- KHÁCH + PET -->

                                    <td>

                                        <strong>

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

                                        </strong>


                                        <br>


                                        <small class="text-muted">

                                            <i class="bi bi-heart me-1"></i>

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

                                        </small>

                                    </td>



                                    <!-- DỊCH VỤ -->

                                    <td>

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

                                    </td>



                                    <!-- THỜI GIAN -->

                                    <td>

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


                                        <br>


                                        <small class="text-muted">

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

                                            –

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

                                        </small>

                                    </td>



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

                                    <td>

                                        <span
                                            class="
                                                badge
                                                rounded-pill
                                                <?= htmlspecialchars(
                                                    $badgeClass
                                                ) ?>
                                            "
                                        >

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

                                        </span>

                                    </td>



                                    <!-- XỬ LÝ -->

                                    <td class="text-end">

                                        <a
                                            class="
                                                btn
                                                btn-sm
                                                btn-outline-primary
                                            "
                                            href="<?= BASE_URL ?>/?url=owner/booking&id=<?= (int) $item['id'] ?>"
                                        >
                                            Xử lý
                                        </a>

                                    </td>


                                </tr>


                            <?php endforeach; ?>



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

                                <tr>

                                    <td
                                        colspan="6"
                                        class="
                                            text-center
                                            text-muted
                                            py-5
                                        "
                                    >

                                        <i
                                            class="
                                                bi
                                                bi-calendar-x
                                                fs-2
                                                d-block
                                                mb-2
                                            "
                                        ></i>

                                        Không có lịch hẹn phù hợp.

                                    </td>

                                </tr>

                            <?php endif; ?>


                        </tbody>

                    </table>

                </div>



                <!-- =========================================
                     PHÂN TRANG
                ========================================== -->

                <?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=owner/bookings&page=<?= $i ?>&status=<?= urlencode(
                                            $filters['status'] ?? ''
                                        ) ?>&date=<?= urlencode(
                                            $filters['date'] ?? ''
                                        ) ?>&q=<?= urlencode(
                                            $filters['q'] ?? ''
                                        ) ?>"
                                    >
                                        <?= $i ?>
                                    </a>

                                </li>


                            <?php endfor; ?>


                        </ul>

                    </nav>

                <?php endif; ?>


            </section>

        </div>

    </div>

</main>


<?php

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

?>