@extends('desktop.layouts.app')

@section('content')
<div id="movieReviewShow" class="container-fluid" data-mid="{{ $movieCd }}">

    <div class="row g-2 align-items-center">
        <div class="col-auto">
            <h5 class="mb-0 fw-bold">
                {{ $info->movie_nm }} @if($info->movie_nm_en) ({{ $info->movie_nm_en }}) @endif
            </h5>
        </div>
        <div class="col text-end">
            <button type="button" class="btn btn-sm btn-default" onclick="history.back();">뒤로가기</button>
        </div>
    </div>
    <hr/>

    <div class="row">
        <div class="col-auto">
            <img src="{{ $detail->main_img }}" class="img-fluid rounded d-block" width="220px" alt="{{ $info->movie_nm }}" onerror="this.src='/images/default/no_image.png'"/>
            <div class="p-3 text-center">
                @isset($detail->main_img)
                <button type="button" id="btnMainImgEnlargement" class="btn btn-default" title="{{ $info->movie_nm }}" value="{{ $detail->main_img }}">
                    <i class="fa-solid fa-magnifying-glass"></i>
                </button>
                @endisset
            </div>
        </div>
        <div class="col">
            <label class="form-label">주요 정보</label>
            <table class="table table-borderless">
                <colgroup>
                    <col width="100px"/>
                    <col width="*"/>
                </colgroup>
                <tr>
                    <th>감독</th>
                    <td>
                        @if($info->directors)
                            @foreach(json_decode($info->directors) as $director)
                                {{ $director->peopleNm }}
                                @if($director->peopleNmEn) ({{ $director->peopleNmEn }}) @endif
                                @if(!$loop->last), @endif
                            @endforeach
                        @else
                            -
                        @endif
                    </td>
                </tr>
                <tr>
                    <th>평점</th>
                    <td class="ps-0">
                        <div class="float-start">
                            <span id="movieAvgRate" data-rate="{{ $avgRate }}"></span>
                        </div>
                        <div class="float-start">
                            <strong>{{ sprintf("%.1f", $avgRate) }}</strong>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th>제작연도</th>
                    <td>{{ $info->prdt_year ?? '-' }}</td>
                </tr>
                <tr>
                    <th>상영시간</th>
                    <td>
                        @if($info->show_tm)
                            {{ $info->show_tm }}분
                        @else
                            -
                        @endif
                    </td>
                </tr>
                <tr>
                    <th>개봉일</th>
                    <td>
                        @if($info->open_dt)
                            {{ (new DateTime($info->open_dt))->format('Y.m.d') }}
                        @else
                            -
                        @endif
                    </td>
                </tr>
                <tr>
                    <th>유형</th>
                    <td>{{ $info->type_nm ?? '-' }}</td>
                </tr>
                <tr>
                    <th>장르</th>
                    <td>
                        @if($info->genres)
                            @foreach(json_decode($info->genres) as $genre)
                                {{ $genre->genreNm }} @if(!$loop->last), @endif
                            @endforeach
                        @else
                            -
                        @endif
                    </td>
                </tr>
                <tr>
                    <th>심의등급</th>
                    <td>
                        @if($info->audits)
                            @foreach(json_decode($info->audits) as $audit)
                                <p class="mb-0">{{ $audit->auditNo }} / {{ $audit->watchGradeNm }}</p>
                            @endforeach
                        @else
                            -
                        @endif
                    </td>
                </tr>
                <tr>
                    <th>줄거리</th>
                    <td>
                        <div class="row">
                            <div class="col col-lg-12 col-xl-12 col-xxl-10">
                                {{ $detail->synopsis ?? '-' }}
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <hr/>

    <section id="movieReviewList">
        <div class="row">
            <div class="col-auto">
                <h5>
                    평점·후기 <small>({{ $reviews->total }})</small>
                </h5>
            </div>
            <div class="col">
                <a href="#" id="btnMovieReviewWrite" @auth data-bs-toggle="modal" data-bs-target="#movieReviewWriteModal" @endauth>내 평점 등록</a>
            </div>
            <div class="col-auto">
                <select id="sort" class="form-select">
                    <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>
        @if($reviews->total > 0)
            @foreach($reviews->list as $row)
                <table class="table table-borderless">
                    <colgroup>
                        <col width="50px"/>
                        <col width="*"/>
                        <col width="50px"/>
                    </colgroup>
                    <tr>
                        <td rowspan="2">
                            <img src="{{ asset($row->thumb) }}" alt="{{ $row->owner }}" width="{{ THUMB_MAX_WIDTH - 30 }}" height="{{ THUMB_MAX_HEIGHT - 30 }}" 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>
                        </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-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>
                    <tr>
                        <td colspan="2" class="pt-0">
                            {{ $row->content }}
                        </td>
                    </tr>
                </table>
            @endforeach
        @else
            <div class="p-2 text-center">
                등록된 후기가 없습니다.
            </div>
        @endif
    </section>

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

    <form id="fMovieReviewIndex" method="get" action="{{ route('movie.review.show', $movieCd) }}" 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
