<?php

$pageTitle = 'Quản lý PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage = 'dashboard';


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


/*
|--------------------------------------------------------------------------
| MÀU TRẠNG THÁI LỊCH HẸN
|--------------------------------------------------------------------------
*/

$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 g-4">


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

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

            </div>



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


                <!-- =====================================================
                     HEADER
                ====================================================== -->

                <div
                    class="
                        owner-page-header
                        d-flex
                        flex-wrap
                        justify-content-between
                        gap-3
                    "
                >

                    <div>

                        <h1>
                            Quản lý PET SPA
                        </h1>

                        <p>
                            Điều hành lịch hẹn, dịch vụ và nội dung trong một nơi.
                        </p>

                    </div>


                    <div class="d-flex gap-2 flex-wrap">

                        <a
                            class="btn btn-primary"
                            href="<?= BASE_URL ?>/?url=owner/service/create"
                        >
                            <i class="bi bi-plus-circle"></i>

                            Thêm dịch vụ
                        </a>


                        <a
                            class="btn btn-outline-primary"
                            href="<?= BASE_URL ?>/?url=owner/bookings"
                        >
                            <i class="bi bi-calendar-check"></i>

                            Lịch hẹn
                        </a>


                        <a
                            class="btn btn-outline-primary"
                            href="<?= BASE_URL ?>/?url=owner/post/create"
                        >
                            <i class="bi bi-file-earmark-plus"></i>

                            Viết bài
                        </a>

                    </div>

                </div>



                <!-- =====================================================
                     THỐNG KÊ NHANH
                ====================================================== -->

                <div class="d-flex flex-wrap gap-2 mb-4">

                    <span class="badge text-bg-primary p-2">

                        Hôm nay:

                        <?= (int) (
                            $stats['today_count']
                            ?? 0
                        ) ?>

                    </span>


                    <span class="badge text-bg-warning p-2">

                        Chờ xác nhận:

                        <?= (int) (
                            $stats['pending_count']
                            ?? 0
                        ) ?>

                    </span>


                    <span class="badge text-bg-info p-2">

                        Đang phục vụ:

                        <?= (int) (
                            $stats['in_progress_count']
                            ?? 0
                        ) ?>

                    </span>

                </div>



                <!-- =====================================================
                     LỊCH HẸN GẦN ĐÂY
                ====================================================== -->

                <section class="owner-card">


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

                        <h2 class="h5 mb-0">
                            Lịch hẹn gần đây
                        </h2>


                        <a
                            href="<?= BASE_URL ?>/?url=owner/bookings"
                        >
                            Xem tất cả
                        </a>

                    </div>



                    <div class="table-responsive mt-3">

                        <table class="table align-middle mb-0">


                            <thead>

                                <tr>

                                    <th>
                                        Mã
                                    </th>

                                    <th>
                                        Khách
                                    </th>

                                    <th>
                                        Thời gian
                                    </th>

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

                                </tr>

                            </thead>



                            <tbody>


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


                                    <?php

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


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

                                    ?>


                                    <tr>


                                        <!-- MÃ -->

                                        <td>

                                            <a
                                                href="<?= BASE_URL ?>/?url=owner/booking&id=<?= (int) $item['id'] ?>"
                                            >

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

                                            </a>

                                        </td>



                                        <!-- KHÁCH -->

                                        <td>

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

                                        </td>



                                        <!-- THỜI GIAN -->

                                        <td>

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

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

                                        </td>



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

                                        <td>

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

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

                                            </span>

                                        </td>


                                    </tr>


                                <?php endforeach; ?>



                                <?php if (!$recentBookings): ?>

                                    <tr>

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

                                            Chưa có lịch hẹn.

                                        </td>

                                    </tr>

                                <?php endif; ?>


                            </tbody>

                        </table>

                    </div>

                </section>


            </section>

        </div>

    </div>

</main>


<?php

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

?>