<?php

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

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

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


$pageTitle =
    'Dịch vụ Spa - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'services';


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


                <!-- HEADER -->

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

                    <h1 class="h3 mb-0">
                        Dịch vụ Spa
                    </h1>


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

                        <i class="bi bi-plus-lg me-1"></i>

                        Thêm dịch vụ

                    </a>

                </div>



                <!-- =============================================
                     THÔNG BÁO THÀNH CÔNG
                ============================================== -->

                <?php if ($successMessage): ?>

                    <div
                        class="
                            alert
                            alert-success
                            alert-dismissible
                            fade
                            show
                            mt-3
                        "
                        role="alert"
                    >

                        <i
                            class="
                                bi
                                bi-check-circle-fill
                                me-2
                            "
                        ></i>

                        <?= htmlspecialchars(
                            $successMessage
                        ) ?>


                        <button
                            type="button"
                            class="btn-close"
                            data-bs-dismiss="alert"
                            aria-label="Đóng"
                        ></button>

                    </div>

                <?php endif; ?>



                <!-- =============================================
                     THÔNG BÁO LỖI
                ============================================== -->

                <?php if ($errorMessage): ?>

                    <div
                        class="
                            alert
                            alert-danger
                            alert-dismissible
                            fade
                            show
                            mt-3
                        "
                        role="alert"
                    >

                        <i
                            class="
                                bi
                                bi-exclamation-circle-fill
                                me-2
                            "
                        ></i>

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>


                        <button
                            type="button"
                            class="btn-close"
                            data-bs-dismiss="alert"
                            aria-label="Đóng"
                        ></button>

                    </div>

                <?php endif; ?>



                <!-- =============================================
                     BẢNG DỊCH VỤ
                ============================================== -->

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

                    <table
                        class="
                            table
                            align-middle
                        "
                    >

                        <thead>

                            <tr>

                                <th>
                                    Tên
                                </th>

                                <th>
                                    Danh mục
                                </th>

                                <th>
                                    Giá / thời lượng
                                </th>

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

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

                            </tr>

                        </thead>


                        <tbody>


                            <?php if (!empty($services)): ?>


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


                                    <tr>


                                        <!-- TÊN -->

                                        <td>

                                            <strong>

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

                                            </strong>

                                        </td>



                                        <!-- DANH MỤC -->

                                        <td>

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

                                        </td>



                                        <!-- GIÁ -->

                                        <td>

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

                                            /

                                            <?= (int) $item[
                                                'duration_minutes'
                                            ] ?>

                                            phút

                                        </td>



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

                                        <td>

                                            <?php if (
                                                !empty(
                                                    $item['is_active']
                                                )
                                            ): ?>

                                                <span
                                                    class="
                                                        badge
                                                        text-bg-success
                                                    "
                                                >

                                                    Đang hiển thị

                                                </span>

                                            <?php else: ?>

                                                <span
                                                    class="
                                                        badge
                                                        text-bg-secondary
                                                    "
                                                >

                                                    Đang ẩn

                                                </span>

                                            <?php endif; ?>

                                        </td>



                                        <!-- THAO TÁC -->

                                        <td class="text-end">

                                            <a
                                                class="
                                                    btn
                                                    btn-sm
                                                    btn-outline-primary
                                                "
                                                href="<?= BASE_URL ?>/?url=owner/service/edit&id=<?= (int) $item['id'] ?>"
                                            >

                                                <i
                                                    class="
                                                        bi
                                                        bi-pencil-square
                                                        me-1
                                                    "
                                                ></i>

                                                Sửa

                                            </a>

                                        </td>


                                    </tr>


                                <?php endforeach; ?>


                            <?php else: ?>


                                <tr>

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

                                        Chưa có dịch vụ nào.

                                    </td>

                                </tr>


                            <?php endif; ?>


                        </tbody>

                    </table>

                </div>


            </section>

        </div>

    </div>

</main>


<?php

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

?>