<?php

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

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    '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="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>
    Đánh giá sản phẩm
</h1>

<p>
    <?= number_format(
        $totalReviews
    ) ?>
    đánh giá.
</p>

</div>

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


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

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

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

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

<input
    type="text"
    name="search"
    class="form-control"
    placeholder="Sản phẩm, khách hàng, nội dung..."
    value="<?= htmlspecialchars(
        $search
    ) ?>"
>

</div>


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

<select
    name="status"
    class="form-select"
>

<option value="">
    Tất cả
</option>

<option
    value="visible"
    <?= $status === 'visible'
        ? 'selected'
        : ''
    ?>
>
    Đang hiển thị
</option>

<option
    value="hidden"
    <?= $status === 'hidden'
        ? 'selected'
        : ''
    ?>
>
    Đã ẩn
</option>

</select>

</div>


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

<button
    class="btn btn-dark w-100"
>
    Lọc
</button>

</div>

</form>

</section>


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

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

<article class="owner-card owner-review-card">

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

<div>

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

<small>
    đánh giá
    <?= htmlspecialchars(
        $review['product_name']
    ) ?>
</small>

</div>


<div class="text-warning fs-5">

<?= str_repeat(
    '★',
    (int)
    $review['rating']
) ?>

<?= str_repeat(
    '☆',
    5 - (int)
    $review['rating']
) ?>

</div>

</div>


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

<p class="owner-review-comment">

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

</p>

<?php endif; ?>


<div class="small text-muted mb-3">

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

•

<?= $review['status']
    === 'visible'
    ? 'Đang hiển thị'
    : 'Đã ẩn'
?>

</div>


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

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

<strong>
    Phản hồi hiện tại
</strong>

<p class="mb-0">

<?= nl2br(
    htmlspecialchars(
        $review['owner_reply']
    )
) ?>

</p>

</div>

<?php endif; ?>


<form
    method="POST"
    action="<?= BASE_URL ?>/?url=owner/review/reply"
    class="mt-3"
>

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

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


<div class="input-group">

<input
    type="text"
    name="owner_reply"
    class="form-control"
    placeholder="Phản hồi khách hàng..."
    value="<?= htmlspecialchars(
        $review['owner_reply']
        ?? ''
    ) ?>"
    required
>

<button
    class="btn btn-primary"
>
    Phản hồi
</button>

</div>

</form>


<form
    method="POST"
    action="<?= BASE_URL ?>/?url=owner/review/toggle"
    class="mt-2"
>

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

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

<button
    class="btn btn-sm btn-light"
    type="submit"
>

<?= $review['status']
    === 'visible'
    ? 'Ẩn đánh giá'
    : 'Hiện đánh giá'
?>

</button>

</form>

</article>

<?php endforeach; ?>


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

<div class="owner-card text-center text-muted py-5">
    Chưa có đánh giá.
</div>

<?php endif; ?>

</div>

</div>

</div>

</div>

</main>


<?php

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

?>