<?php

$pageTitle =
    'Khách hàng - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'customers';


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>
                Khách hàng
            </h1>

            <p>

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

                tài khoản khách hàng.

            </p>

        </div>

    </div>



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

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

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

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


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

                <input
                    type="text"
                    name="search"
                    class="form-control"
                    placeholder="Tên, số điện thoại hoặc email..."
                    value="<?= htmlspecialchars(
                        $search
                    ) ?>"
                >

            </div>


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

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

            </div>

        </form>

    </section>



    <!-- DANH SÁCH -->

    <section class="owner-card">

        <div class="table-responsive">

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

                <thead>

                    <tr>

                        <th>Khách hàng</th>

                        <th>SĐT</th>

                        <th class="text-center">
                            Tổng lịch Spa
                        </th>

                        <th class="text-center">
                            Hoàn thành
                        </th>

                        <th class="text-center">
                            Sắp tới
                        </th>

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

                        <th>
                            Ngày tham gia
                        </th>

                        <th></th>

                    </tr>

                </thead>


                <tbody>


                <?php foreach (
                    $customers
                    as $customer
                ): ?>


                    <tr>


                        <!-- KHÁCH -->

                        <td>

                            <div
                                class="owner-customer-cell"
                            >

                                <div
                                    class="owner-customer-avatar"
                                >

                                    <?php if (
                                        !empty(
                                            $customer['avatar']
                                        )
                                    ): ?>

                                        <img
                                            src="<?= BASE_URL ?>/<?= htmlspecialchars(
                                                ltrim(
                                                    $customer['avatar'],
                                                    '/'
                                                )
                                            ) ?>"
                                            alt=""
                                        >

                                    <?php else: ?>

                                        <i class="bi bi-person"></i>

                                    <?php endif; ?>

                                </div>


                                <div>

                                    <strong>

                                        <?= htmlspecialchars(
                                            $customer[
                                                'full_name'
                                            ]
                                        ) ?>

                                    </strong>


                                    <small>

                                        <?= htmlspecialchars(
                                            $customer['email']
                                            ?? 'Chưa có email'
                                        ) ?>

                                    </small>

                                </div>

                            </div>

                        </td>



                        <!-- PHONE -->

                        <td>

                            <?= htmlspecialchars(
                                $customer['phone']
                            ) ?>

                        </td>



                        <!-- TỔNG LỊCH -->

                        <td class="text-center">

                            <span
                                class="
                                    badge
                                    rounded-pill
                                    text-bg-primary
                                "
                            >

                                <?= (int) (
                                    $customer[
                                        'booking_count'
                                    ]
                                    ?? 0
                                ) ?>

                            </span>

                        </td>



                        <!-- HOÀN THÀNH -->

                        <td class="text-center">

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

                                <?= (int) (
                                    $customer[
                                        'completed_booking_count'
                                    ]
                                    ?? 0
                                ) ?>

                            </span>

                        </td>



                        <!-- SẮP TỚI -->

                        <td class="text-center">

                            <span
                                class="
                                    badge
                                    rounded-pill
                                    text-bg-info
                                "
                            >

                                <?= (int) (
                                    $customer[
                                        'upcoming_booking_count'
                                    ]
                                    ?? 0
                                ) ?>

                            </span>

                        </td>



                        <!-- STATUS -->

                        <td>

                            <?php if (
                                $customer['status']
                                === 'active'
                            ): ?>

                                <span
                                    class="badge bg-success"
                                >
                                    Hoạt động
                                </span>

                            <?php else: ?>

                                <span
                                    class="badge bg-danger"
                                >
                                    Đã khóa
                                </span>

                            <?php endif; ?>

                        </td>



                        <!-- CREATED -->

                        <td>

                            <?= date(
                                'd/m/Y',
                                strtotime(
                                    $customer[
                                        'created_at'
                                    ]
                                )
                            ) ?>

                        </td>



                        <!-- DETAIL -->

                        <td>

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

                        </td>

                    </tr>


                <?php endforeach; ?>



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

                    <tr>

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

                    </tr>

                <?php endif; ?>


                </tbody>

            </table>

        </div>

    </section>



    <!-- PHÂN TRANG -->

    <?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/customers';

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

?>