<?php

$pageTitle =
    $customer['full_name']
    . ' - Khách hàng';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'customers';


$statusNames = [

    'pending' =>
        'Chờ xác nhận',

    'confirmed' =>
        'Đã xác nhận',

    'in_progress' =>
        'Đang thực hiện',

    'completed' =>
        'Hoàn thành',

    'cancelled' =>
        'Đã hủy'
];


$statusClasses = [

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



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


    <!-- HEADER -->

    <div class="owner-page-header">

        <div>

            <a
                href="<?= BASE_URL ?>/?url=owner/customers"
                class="small text-decoration-none"
            >
                ← Khách hàng
            </a>


            <h1 class="mt-2">

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

            </h1>


            <p>
                Thông tin khách hàng và lịch sử sử dụng dịch vụ Spa.
            </p>

        </div>

    </div>



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

    <div class="row g-3">


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

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

            <div class="owner-stat-card">

                <i class="bi bi-calendar-check"></i>

                <strong>

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

                </strong>

                <span>
                    Tổng lịch Spa
                </span>

            </div>

        </div>



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

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

            <div class="owner-stat-card">

                <i class="bi bi-check-circle"></i>

                <strong>

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

                </strong>

                <span>
                    Đã hoàn thành
                </span>

            </div>

        </div>



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

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

            <div class="owner-stat-card">

                <i class="bi bi-calendar-event"></i>

                <strong>

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

                </strong>

                <span>
                    Lịch sắp tới
                </span>

            </div>

        </div>


    </div>



    <div class="row g-4 mt-1">


        <!-- =================================================
             THÔNG TIN KHÁCH
        ================================================== -->

        <div class="col-xl-5">


            <section class="owner-card">

                <h2>
                    Thông tin khách hàng
                </h2>


                <div class="owner-info-row">

                    <span>
                        Họ tên
                    </span>

                    <strong>

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

                    </strong>

                </div>


                <div class="owner-info-row">

                    <span>
                        Số điện thoại
                    </span>

                    <strong>

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

                    </strong>

                </div>


                <div class="owner-info-row">

                    <span>
                        Email
                    </span>

                    <strong>

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

                    </strong>

                </div>


                <div class="owner-info-row">

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

                    <strong>

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

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

                        <?php else: ?>

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

                        <?php endif; ?>

                    </strong>

                </div>


                <div class="owner-info-row">

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

                    <strong>

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

                    </strong>

                </div>

            </section>



            <!-- GIỮ NGUYÊN ĐỊA CHỈ -->

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

                <h2>
                    Địa chỉ đã lưu
                </h2>


                <?php foreach (
                    $addresses
                    as $address
                ): ?>

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

                        <strong>

                            <?= htmlspecialchars(
                                $address[
                                    'recipient_name'
                                ]
                            ) ?>

                        </strong>


                        <?php if (
                            (int) $address[
                                'is_default'
                            ] === 1
                        ): ?>

                            <span
                                class="badge bg-success"
                            >
                                Mặc định
                            </span>

                        <?php endif; ?>


                        <small>

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

                        </small>


                        <p>

                            <?= htmlspecialchars(
                                implode(
                                    ', ',
                                    array_filter([
                                        $address[
                                            'address_line'
                                        ],
                                        $address[
                                            'ward'
                                        ],
                                        $address[
                                            'district'
                                        ],
                                        $address[
                                            'province'
                                        ]
                                    ])
                                )
                            ) ?>

                        </p>

                    </div>

                <?php endforeach; ?>


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

                    <p
                        class="
                            text-muted
                            mb-0
                        "
                    >
                        Khách chưa lưu địa chỉ.
                    </p>

                <?php endif; ?>

            </section>

        </div>



        <!-- =================================================
             LỊCH SỬ SPA
        ================================================== -->

        <div class="col-xl-7">

            <section class="owner-card">

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

                    <h2 class="mb-0">
                        Lịch sử Spa
                    </h2>


                    <span class="text-muted small">
                        20 lịch gần nhất
                    </span>

                </div>



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

                    <div
                        class="
                            text-center
                            text-muted
                            py-4
                        "
                    >

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

                        Khách chưa có lịch Spa.

                    </div>


                <?php else: ?>


                    <div class="table-responsive">

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

                            <thead>

                                <tr>

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

                                    <th>
                                        Thú cưng
                                    </th>

                                    <th>
                                        Thời gian
                                    </th>

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

                                    <th></th>

                                </tr>

                            </thead>


                            <tbody>


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


                                <?php

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

                                ?>


                                <tr>


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

                                    <td>

                                        <strong>

                                            <?= htmlspecialchars(
                                                $booking[
                                                    'service_name_snapshot'
                                                ]
                                                ?? 'Dịch vụ Spa'
                                            ) ?>

                                        </strong>


                                        <small
                                            class="
                                                d-block
                                                text-muted
                                            "
                                        >

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

                                        </small>

                                    </td>



                                    <!-- PET -->

                                    <td>

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

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

                                    </td>



                                    <!-- TIME -->

                                    <td>

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


                                        <small
                                            class="
                                                d-block
                                                text-muted
                                            "
                                        >

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

                                            –

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

                                        </small>

                                    </td>



                                    <!-- STATUS -->

                                    <td>

                                        <span
                                            class="badge rounded-pill <?= htmlspecialchars(
                                                $statusClasses[
                                                    $status
                                                ]
                                                ?? 'text-bg-secondary'
                                            ) ?>"
                                        >

                                            <?= htmlspecialchars(
                                                $statusNames[
                                                    $status
                                                ]
                                                ?? $status
                                            ) ?>

                                        </span>

                                    </td>



                                    <!-- DETAIL -->

                                    <td>

                                        <a
                                            href="<?= BASE_URL ?>/?url=owner/booking&id=<?= (int) $booking['id'] ?>"
                                            class="
                                                btn
                                                btn-sm
                                                btn-light
                                            "
                                        >
                                            Xem
                                        </a>

                                    </td>


                                </tr>


                            <?php endforeach; ?>


                            </tbody>

                        </table>

                    </div>


                <?php endif; ?>


            </section>

        </div>


    </div>

</div>

</div>

</div>

</main>


<?php

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

?>