@extends('desktop.layouts.app')

@section('content')
<div id="movieReviewIndex" class="container">

    <div class="row g-2 align-items-center">
        <div class="col">
            <h5 class="mb-0 fw-bold">
                영화 평점·후기들 <small>({{ $reviews->total }})</small>
            </h5>
        </div>
        <div class="col">
            <select id="sort" class="form-select w-auto ms-auto">
                <option value="1" @if($params->sort == 1) selected @endif>공감순</option>
                <option value="2" @if($params->sort == 2) selected @endif>최신순</option>
                <option value="3" @if($params->sort == 3) selected @endif>평점 높은 순</option>
                <option value="4" @if($params->sort == 4) selected @endif>평점 낮은 순</option>
            </select>
        </div>
    </div>

    <hr/>

    <section id="movieReviewList">
        @if($reviews->total > 0)
            @foreach($reviews->list as $row)
                @php
                    $movieCd = base64_encode($row->movie_cd);
                    $movieNm = $row->movie_nm;
                    if($row->movie_nm_en) {
                        $movieNm .= (' (' . $row->movie_nm_en . ')');
                    }
                @endphp
                <table class="table table-borderless">
                    <colgroup>
                        <col width="10%"/>
                        <col width="1%"/>
                        <col width="*"/>
                        <col width="50px"/>
                    </colgroup>
                    <tr>
                        <td rowspan="3">
                            <a href="{{ $row->viewURL }}" title="{{ $row->movie_nm }}">
                                <img src="{{ $row->thumb_img }}" alt="{{ $row->movie_nm }}" class="img-fluid rounded" onerror="this.src='/images/default/default_thumb.gif'"/>
                            </a>
                        </td>
                        <td colspan="3" class="align-bottom">
                            <a href="{{ $row->viewURL }}" class="movie-title">
                                {{ $row->movie_nm }}
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <img src="{{ asset($row->thumb) }}" alt="{{ $row->owner }}" width="42" height="42" onerror="this.src='/images/default/default_thumb.gif'"/>
                        </td>
                        <td class="pb-0">
                            <dl>
                                <dt>
                                    <var data-rate="{{ $row->rate }}"></var>
                                    <sub>{{ $row->rate }}</sub>
                                    @auth
                                        @if(UID == $row->id)
                                            <span class="badge bg-info text-white">내 평점</span>
                                        @endif
                                    @endauth
                                </dt>
                                <dd>
                                    <ul>
                                        <li><small>{{ $row->owner }}</small></li>
                                        <li><small>{{ $row->createdAt }}</small></li>
                                        <li>
                                            <button type="button" class="btn btn-sm btn-default btn-review-like @if($row->isLike) active @endif" data-mid="{{ $movieCd }}" data-rid="{{ $row->id }}">
                                                <i class="fas fa-thumbs-up"></i>
                                                <em>{{ $row->like }}</em>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-default btn-review-dislike @if($row->isDisLike) active @endif" data-mid="{{ $movieCd }}" data-rid="{{ $row->id }}">
                                                <i class="fas fa-thumbs-down"></i>
                                                <em>{{ $row->dislike }}</em>
                                            </button>
                                        </li>
                                    </ul>
                                </dd>
                            </dl>
                            {{ $row->content }}
                        </td>
                        <td>
                            <div class="dropdown">
                                <a class="btn btn-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="-20,3">
                                    관리
                                </a>
                                <ul class="dropdown-menu dropdown-menu-right w-auto" data-mid="{{ $movieCd }}" data-rid="{{ $row->id }}" data-rate="{{ $row->rate }}" data-name="{{ $movieNm }}" data-content="{{ $row->content }}">
                                    @auth
                                        @if($row->user_id == UID)
                                            <li><a href="#" class="dropdown-item btn-review-modify" data-bs-toggle="modal" data-bs-target="#movieReviewWriteModal">수정</a></li>
                                            <li><a href="#" class="dropdown-item btn-review-remove">삭제</a></li>
                                        @endif
                                    @endauth
                                    <li><a href="#" class="dropdown-item btn-review-blame" @auth data-bs-toggle="modal" data-bs-target="#movieReviewBlameModal" @endauth>신고</a></li>
                                </ul>
                            </div>
                        </td>
                    </tr>
                </table>
            @endforeach

        @else
            <div class="p-2 text-center">
                등록된 후기가 없습니다.
            </div>
        @endif

        <hr/>
        <div class="pagination">
            {{ $reviews->list->onEachSide($params->pageCount)->links('component.pagination') }}
        </div>
    </section>

    <br/>

    <small class="text-muted">
        평점은 영화별로 1인당 1회만 등록이 가능합니다. 게시판 용도에 맞지 않는 글은 운영자에 의해 삭제될 수 있습니다.
    </small>

    <form id="fMovieReviewIndex" method="get" action="{{ route('movie.review.index') }}" accept-charset="UTF-8">
        @csrf
        <input type="hidden" name="page" value="{{ $params->page }}"/>
        <input type="hidden" name="sort" value="{{ $params->sort }}"/>
    </form>

    <!-- 평점·후기 등록/수정 -->
    @include('desktop.movie.review.write')

    <!-- 평점·후기 신고 -->
    @include('desktop.movie.review.blame')
</div>
@endsection

@push('styles')
    <link rel="stylesheet" href="{{ asset('css/desktop/movie/review.css') }}"/>
@endpush

@push('scripts')
    <script src="{{ asset('js/movie/review.js') }}" defer></script>
    <script>
        window.addEventListener('DOMContentLoaded', function () {
            Review.init();
            Review.general("movieReviewList");
        });
    </script>
@endpush
