<?php

$pageTitle = 'Chi tiết lịch hẹn - PET SPA';
$pageStyles = ['owner.css'];
$activeOwnerPage = 'bookings';


/*
|--------------------------------------------------------------------------
| THÔNG BÁO FLASH
|--------------------------------------------------------------------------
*/

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

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

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


/*
|--------------------------------------------------------------------------
| TÊN TRẠNG THÁI TIẾNG VIỆT
|--------------------------------------------------------------------------
*/

$statusLabels = [
    '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',
];


/*
|--------------------------------------------------------------------------
| MÀU TRẠNG THÁI
|--------------------------------------------------------------------------
*/

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


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


/*
|--------------------------------------------------------------------------
| TRẠNG THÁI ĐƯỢC PHÉP CHUYỂN TIẾP
|--------------------------------------------------------------------------
*/

$nextStatusMap = [

    'pending' => [
        'confirmed',
        'cancelled'
    ],

    'confirmed' => [
        'in_progress',
        'cancelled'
    ],

    'in_progress' => [
        'completed'
    ],

    'completed' => [],

    'cancelled' => [],
];


$nextStatuses =
    $nextStatusMap[$currentStatus]
    ?? [];


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


                <!-- QUAY LẠI -->

                <a
                    href="<?= BASE_URL ?>/?url=owner/bookings"
                    class="text-decoration-none"
                >
                    ← Lịch hẹn
                </a>


                <!-- MÃ LỊCH HẸN -->

                <h1 class="h3 mt-3">

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

                </h1>



                <!-- THÀNH CÔNG -->

                <?php if ($successMessage): ?>

                    <div
                        class="alert alert-success alert-dismissible fade show"
                        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; ?>



                <!-- LỖI -->

                <?php if ($errorMessage): ?>

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

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>

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

                    </div>

                <?php endif; ?>



                <!-- =====================================================
                     THÔNG TIN LỊCH
                ====================================================== -->

                <div class="card mb-3">

                    <div class="card-body">


                        <p>

                            <strong>

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

                            </strong>

                            ,

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

                            —

                            <?= (int) $booking[
                                'duration_minutes_snapshot'
                            ] ?>

                            phút

                        </p>



                        <p>

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

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

                            –

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

                        </p>



                        <p>

                            Khách:

                            <strong>

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

                            </strong>

                            ·

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

                        </p>



                        <p>

                            Thú cưng:

                            <strong>

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

                            </strong>

                            (

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


                            <?php if (!empty($booking['pet_breed'])): ?>

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

                            <?php endif; ?>


                            <?php if (!empty($booking['pet_weight'])): ?>

                                ,
                                <?= htmlspecialchars(
                                    $booking['pet_weight']
                                ) ?>
                                kg

                            <?php endif; ?>

                            )

                        </p>



                        <?php if (!empty($booking['pet_note'])): ?>

                            <p>

                                <strong>
                                    Lưu ý thú cưng:
                                </strong>

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

                            </p>

                        <?php endif; ?>



                        <?php if (!empty($booking['customer_note'])): ?>

                            <p>

                                <strong>
                                    Ghi chú khách:
                                </strong>

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

                            </p>

                        <?php endif; ?>



                        <!-- TRẠNG THÁI HIỆN TẠI -->

                        <p class="mb-0">

                            Trạng thái hiện tại:

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

                                <?= htmlspecialchars(
                                    $statusLabels[$currentStatus]
                                    ?? $currentStatus
                                ) ?>

                            </span>

                        </p>

                    </div>

                </div>



                <!-- =====================================================
                     CHUYỂN TRẠNG THÁI
                ====================================================== -->

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

                    <form
                        class="card card-body mb-3"
                        method="post"
                        action="<?= BASE_URL ?>/?url=owner/booking/status"
                    >

                        <input
                            type="hidden"
                            name="_token"
                            value="<?= htmlspecialchars(
                                $_SESSION['csrf_token']
                                ?? ''
                            ) ?>"
                        >

                        <input
                            type="hidden"
                            name="booking_id"
                            value="<?= (int) $booking['id'] ?>"
                        >


                        <label
                            class="form-label fw-semibold"
                            for="bookingStatus"
                        >
                            Chuyển trạng thái
                        </label>


                        <select
                            id="bookingStatus"
                            class="form-select mb-3"
                            name="status"
                            required
                        >

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

                                <option
                                    value="<?= htmlspecialchars($item) ?>"
                                >

                                    <?= htmlspecialchars(
                                        $statusLabels[$item]
                                        ?? $item
                                    ) ?>

                                </option>

                            <?php endforeach; ?>

                        </select>



                        <label
                            class="form-label"
                            for="bookingOwnerNote"
                        >
                            Ghi chú cho khách
                        </label>


                        <textarea
                            id="bookingOwnerNote"
                            class="form-control mb-3"
                            name="owner_note"
                            maxlength="1000"
                            rows="3"
                            placeholder="Nhập ghi chú nếu cần..."
                        ></textarea>


                        <button
                            type="submit"
                            class="btn btn-primary"
                        >

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

                            Cập nhật trạng thái

                        </button>

                    </form>


                <?php else: ?>

                    <div class="alert alert-light border mb-3">

                        <?php if ($currentStatus === 'completed'): ?>

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

                            Lịch hẹn này đã hoàn thành.

                        <?php elseif ($currentStatus === 'cancelled'): ?>

                            <i class="bi bi-x-circle-fill text-danger me-2"></i>

                            Lịch hẹn này đã được hủy.

                        <?php endif; ?>

                    </div>

                <?php endif; ?>



                <!-- =====================================================
                     GHI CHÚ NỘI BỘ
                ====================================================== -->

                <form
                    class="card card-body"
                    method="post"
                    action="<?= BASE_URL ?>/?url=owner/booking/note"
                >

                    <input
                        type="hidden"
                        name="_token"
                        value="<?= htmlspecialchars(
                            $_SESSION['csrf_token']
                            ?? ''
                        ) ?>"
                    >

                    <input
                        type="hidden"
                        name="booking_id"
                        value="<?= (int) $booking['id'] ?>"
                    >


                    <label
                        class="form-label fw-semibold"
                        for="internalOwnerNote"
                    >
                        Ghi chú nội bộ / Spa
                    </label>


                    <textarea
                        id="internalOwnerNote"
                        class="form-control mb-3"
                        name="owner_note"
                        maxlength="1000"
                        rows="3"
                    ><?= htmlspecialchars(
                        $booking['owner_note']
                        ?? ''
                    ) ?></textarea>


                    <button
                        type="submit"
                        class="btn btn-outline-primary"
                    >

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

                        Lưu ghi chú

                    </button>

                </form>



                <!-- =====================================================
                     LỊCH SỬ TRẠNG THÁI
                ====================================================== -->

                <h2 class="h5 mt-4">
                    Lịch sử
                </h2>


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

                    <ul>

                        <?php foreach ($statusLogs as $log): ?>

                            <?php

                            $logStatus =
                                (string) (
                                    $log['new_status']
                                    ?? ''
                                );

                            ?>

                            <li class="mb-2">

                                <strong>

                                    <?= htmlspecialchars(
                                        $statusLabels[$logStatus]
                                        ?? $logStatus
                                    ) ?>

                                </strong>

                                —

                                <?= htmlspecialchars(
                                    $log['created_at']
                                    ?? ''
                                ) ?>


                                <?php if (!empty($log['actor_name'])): ?>

                                    bởi

                                    <?= htmlspecialchars(
                                        $log['actor_name']
                                    ) ?>

                                <?php endif; ?>

                            </li>

                        <?php endforeach; ?>

                    </ul>

                <?php else: ?>

                    <p class="text-muted">
                        Chưa có lịch sử thay đổi trạng thái.
                    </p>

                <?php endif; ?>


            </section>

        </div>

    </div>

</main>


<?php

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

?>