<?php

$pageTitle =
    'Đánh giá sản phẩm - PET SPA';

$pageStyles = [
    'customer.css'
];

$activeCustomerPage =
    'reviews';


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

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


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


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

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

?>

<main class="customer-area">

<div class="container">

<div class="row g-4">

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

<?php require BASE_PATH
    . '/app/views/layouts/customer-sidebar.php';
?>

</div>


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

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

<h1>
    Đánh giá sản phẩm
</h1>

<p>
    Chia sẻ trải nghiệm về những sản phẩm bạn đã mua.
</p>

</div>


<?php if ($successMessage): ?>

<div class="alert alert-success">
    <?= htmlspecialchars(
        $successMessage
    ) ?>
</div>

<?php endif; ?>


<?php if ($errorMessage): ?>

<div class="alert alert-danger">
    <?= htmlspecialchars(
        $errorMessage
    ) ?>
</div>

<?php endif; ?>


<!-- CHƯA ĐÁNH GIÁ -->

<section class="customer-card">

<h2>
    Chờ bạn đánh giá
</h2>


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

<p class="text-muted mb-0">
    Hiện không có sản phẩm nào cần đánh giá.
</p>

<?php endif; ?>


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

<div class="customer-review-item">

<div class="customer-review-product">

<div class="customer-review-image">

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

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

<?php else: ?>

🐾

<?php endif; ?>

</div>


<div>

<strong>
    <?= htmlspecialchars(
        $item['product_name']
    ) ?>
</strong>

<small>
    Đơn:
    <?= htmlspecialchars(
        $item['order_code']
    ) ?>
</small>

</div>

</div>


<form
    method="POST"
    action="<?= BASE_URL ?>/?url=customer/review/store"
>

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

<input
    type="hidden"
    name="order_item_id"
    value="<?= (int)
        $item['order_item_id']
    ?>"
>


<div class="mb-2">

<label class="form-label">
    Số sao
</label>

<select
    name="rating"
    class="form-select"
    required
>

<option value="5">
    ⭐⭐⭐⭐⭐ - Rất tốt
</option>

<option value="4">
    ⭐⭐⭐⭐ - Tốt
</option>

<option value="3">
    ⭐⭐⭐ - Bình thường
</option>

<option value="2">
    ⭐⭐ - Chưa tốt
</option>

<option value="1">
    ⭐ - Không hài lòng
</option>

</select>

</div>


<div class="mb-2">

<textarea
    name="comment"
    class="form-control"
    rows="3"
    placeholder="Chia sẻ trải nghiệm của bạn..."
></textarea>

</div>


<button
    type="submit"
    class="btn btn-primary btn-sm"
>
    Gửi đánh giá
</button>

</form>

</div>

<?php endforeach; ?>

</section>


<!-- ĐÃ ĐÁNH GIÁ -->

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

<h2>
    Đánh giá của bạn
</h2>


<?php foreach (
    $reviews
    as $review
): ?>

<article class="customer-review-history">

<div
    class="d-flex justify-content-between gap-3"
>

<strong>
    <?= htmlspecialchars(
        $review['product_name']
    ) ?>
</strong>

<span class="text-warning">
    <?= str_repeat(
        '★',
        (int)
        $review['rating']
    ) ?>
</span>

</div>


<?php if (
    !empty(
        $review['comment']
    )
): ?>

<p>
    <?= nl2br(
        htmlspecialchars(
            $review['comment']
        )
    ) ?>
</p>

<?php endif; ?>


<small class="text-muted">

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

</small>


<?php if (
    !empty(
        $review['owner_reply']
    )
): ?>

<div class="owner-review-reply">

<strong>
    PET SPA phản hồi:
</strong>

<p class="mb-0">
    <?= nl2br(
        htmlspecialchars(
            $review['owner_reply']
        )
    ) ?>
</p>

</div>

<?php endif; ?>

</article>

<?php endforeach; ?>


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

<p class="text-muted mb-0">
    Bạn chưa gửi đánh giá nào.
</p>

<?php endif; ?>

</section>

</div>

</div>

</div>

</main>


<?php

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

?>