<?php

$pageTitle =
    'Chi tiết liên hệ - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'contacts';


/*
|--------------------------------------------------------------------------
| FLASH
|--------------------------------------------------------------------------
*/

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

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

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


/*
|--------------------------------------------------------------------------
| TRẠNG THÁI LIÊN HỆ
|--------------------------------------------------------------------------
*/

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


$statusNames = [

    'new' =>
        status_label(
            'contact',
            'new'
        ),

    'reading' =>
        status_label(
            'contact',
            'reading'
        ),

    'replied' =>
        status_label(
            'contact',
            'replied'
        ),

    'closed' =>
        status_label(
            'contact',
            'closed'
        ),

];


$currentContactStatus =
    (string) (
        $contact['status']
        ?? 'new'
    );


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/contacts"
            class="small text-decoration-none"
        >
            ← Liên hệ
        </a>


        <h1 class="mt-2">

            <?= htmlspecialchars(
                $contact['subject']
                ?: 'Liên hệ khách hàng'
            ) ?>

        </h1>

    </div>

</div>



<!-- =========================================================
     THÔNG BÁO
========================================================= -->

<?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; ?>


<?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; ?>



<div class="row g-4">


<!-- =========================================================
     NỘI DUNG
========================================================= -->

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

<section class="owner-card">

    <h2>
        Nội dung
    </h2>


    <div class="contact-message">

        <?= nl2br(
            htmlspecialchars(
                $contact['message']
            )
        ) ?>

    </div>

</section>

</div>



<!-- =========================================================
     THÔNG TIN NGƯỜI GỬI
========================================================= -->

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


<section class="owner-card">

    <h2>
        Người gửi
    </h2>


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

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

        <strong>

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

        </strong>

    </div>



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

        <span>
            Điện thoại
        </span>

        <strong>

            <?php if (!empty($contact['phone'])): ?>

                <a
                    href="tel:<?= htmlspecialchars(
                        $contact['phone']
                    ) ?>"
                    class="text-decoration-none"
                >

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

                </a>

            <?php else: ?>

                -

            <?php endif; ?>

        </strong>

    </div>



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

        <span>
            Email
        </span>

        <strong>

            <?php if (!empty($contact['email'])): ?>

                <a
                    href="mailto:<?= htmlspecialchars(
                        $contact['email']
                    ) ?>"
                    class="text-decoration-none"
                >

                    <?= htmlspecialchars(
                        $contact['email']
                    ) ?>

                </a>

            <?php else: ?>

                -

            <?php endif; ?>

        </strong>

    </div>



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

        <span>
            Ngày gửi
        </span>

        <strong>

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

        </strong>

    </div>



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

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

        <strong>

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

        </strong>

    </div>

</section>



<!-- =========================================================
     XỬ LÝ
========================================================= -->

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

    <h2>
        Xử lý
    </h2>



    <?php if ($currentContactStatus === 'closed'): ?>

        <!-- ĐÃ ĐÓNG -->

        <div class="alert alert-success mb-0">

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

            Liên hệ này đã được đóng.

        </div>



    <?php elseif ($currentContactStatus === 'replied'): ?>

        <!-- ĐÃ PHẢN HỒI -->

        <div class="alert alert-success">

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

            Liên hệ này đã được đánh dấu
            <strong>Đã phản hồi</strong>.

        </div>


        <!-- CHỈ CÒN NÚT ĐÓNG -->

        <form
            method="POST"
            action="<?= BASE_URL ?>/?url=owner/contact/status"
        >

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


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


            <input
                type="hidden"
                name="status"
                value="closed"
            >


            <button
                type="submit"
                class="btn btn-outline-success w-100"
            >

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

                Đóng liên hệ

            </button>

        </form>



    <?php else: ?>

        <!-- CHƯA XỬ LÝ XONG -->

        <form
            method="POST"
            action="<?= BASE_URL ?>/?url=owner/contact/status"
        >

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


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


            <label
                for="contactStatus"
                class="form-label"
            >
                Trạng thái xử lý
            </label>


            <select
                id="contactStatus"
                name="status"
                class="form-select"
                required
            >

                <?php if ($currentContactStatus === 'new'): ?>

                    <option value="reading">
                        Đang xem
                    </option>

                <?php endif; ?>


                <option value="replied">
                    Đã phản hồi
                </option>


                <option value="closed">
                    Đã đóng
                </option>

            </select>



            <button
                type="submit"
                class="btn btn-primary w-100 mt-3"
            >

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

                Cập nhật

            </button>

        </form>

    <?php endif; ?>

</section>


</div>

</div>

</div>

</div>

</div>

</main>


<?php

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

?>