@php
use App\Models\Game;
use App\Models\GameList;
use App\Models\Genre;
use App\Models\Platform;
@endphp

{{-- Check to see if the user is logged in. --}}
@if (Auth::check())
    {{-- Check role of user. --}}
    @if (
        Auth::user()->role_id != 1 &&
        Auth::user()->role_id != 2 &&
        Auth::user()->role_id != 3
    )
        {{-- Redirect to home page. --}}
        <script>window.location = "/";</script>
    @endif
@endif

<x-base-layout>
    <!--begin::Row-->
    <div class="row gy-5 g-xl-8">
        <!--begin::Col-->
        <div class="col-xxl-8">
            <div class="card shadow-sm mb-2">
                <div class="card-header">
                    <h3 class="card-title">Clone Game - {{ $game->name }}</h3>
                </div>
                <div class="card-body">
                    @if (count($errors) > 0)
                    <!--begin::Alert-->
                    <div class="alert alert-danger d-flex align-items-center p-5 mb-10">
                        <span class="svg-icon svg-icon-2hx svg-icon-danger me-4">
                            <i class="fad fa-times-circle"></i>
                        </span>
                        <div class="d-flex flex-column">
                            <h4 class="mb-1 text-danger">Error Adding Game</h4>
                            <ul>
                                @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    </div>
                    <!--end::Alert-->
                    @endif
                    <form class="form w-100" novalidate="novalidate" action="{{ route('admin.game.store') }}" method="post" enctype="multipart/form-data">
                        @csrf
                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Name</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="name" value="{{ old('name') ?? $game->name }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Alternative Titles</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="alt_titles" placeholder="Alt Title 1|Alt Title 2|Alt Title3" value="{{ old('alt_titles') ?? $game->alt_titles }}" />
                            <div class="text-muted mt-1">Please use the pipe character <code>|</code> to separate each alternative title. Do not include any spaces before or after the pipe.</div>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Platform</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="platform_id">
                                <option></option>
                                @php
                                $platforms = Platform::all()->sortBy('name');
                                @endphp
                                @foreach ($platforms as $platform)
                                    <option value="{{ $platform->id }}" @selected(old('platform_id') == $platform->id)>
                                        {{ $platform->name }}
                                    </option>
                                @endforeach
                            </select>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-12 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Description</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <textarea class="form-control" data-kt-autosize="true" rows="5" name="description">{{ old('description') ?? $game->description }}</textarea>
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-8 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Source</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="source" placeholder="https://domain.com" value="{{ old('source') ?? $game->source }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Boxart</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="file" name="boxart" value="" />
                            <!--end::Input-->
                            <div class="text-muted mt-1">Boxart images must not be over <code>512kb</code> and must be a <code>JPG/JPEG</code>, <code>GIF</code> or <code>PNG</code> file.</div>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <hr />
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Genres</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            @php
                                $genres = Genre::all()->sortBy('name');
                            @endphp

                            @foreach ($genres as $genre)
                            <div class="mb-2">
                                <div class="form-check form-check-custom form-check-solid">
                                    <input class="form-check-input" type="checkbox" name="genre_ids[]" value="{{ $genre->id }}" id="{{ $genre->name }}" @checked(in_array($genre->id, explode(',', $game->genre_ids))) {{ (in_array($genre->id, old('genre_ids', []))) ? "checked" : (in_array($genre->id, explode(',', $game->genre_ids))) }}>
                                    <label class="form-check-label" for="{{ $genre->name }}">
                                        {{ $genre->name }}
                                    </label>
                                </div>
                            </div>
                            @endforeach
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Developer(s)</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="developers" value="{{ old('developers') ?? $game->developers }}" />
                            <!--end::Input-->
                            <div class="text-muted mt-1">Please ensure this is a comma separated value if there are multiple developers.</div>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Publishers(s)</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="publishers" value="{{ old('publishers') ?? $game->publishers }}" />
                            <!--end::Input-->
                            <div class="text-muted mt-1">Please ensure this is a comma separated value if there are multiple publishers.</div>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Composer(s)</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="composers" value="{{ old('composers') ?? $game->composers }}" />
                            <!--end::Input-->
                            <div class="text-muted mt-1">Please ensure this is a comma separated value if there are multiple composers.</div>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">Official Website</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="website" placeholder="https://domain.com" value="{{ old('website') ?? $game->website }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">NA Release Date</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="na_release_date" placeholder="{{ date('F j, Y') }}" value="{{ old('na_release_date') ?? $game->na_release_date }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">JP Release Date</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="jp_release_date" placeholder="{{ date('F j, Y') }}" value="{{ old('jp_release_date') ?? $game->jp_release_date }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">EU Release Date</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="eu_release_date" placeholder="{{ date('F j, Y') }}" value="{{ old('eu_release_date') ?? $game->eu_release_date }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">AUS Release Date</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <input class="form-control" type="text" name="aus_release_date" placeholder="{{ date('F j, Y') }}" value="{{ old('aus_release_date') ?? $game->aus_release_date }}" />
                            <!--end::Input-->
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">ESRB Rating</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="esrb_rating">
                                <option></option>
                                @php
                                $esrbRatings = [
                                    1 => 'Everyone',
                                    2 => 'Everyone 10+',
                                    3 => 'Teen',
                                    4 => 'Mature 17+',
                                    5 => 'Adults Only 18+',
                                    6 => 'Rating Pending',
                                    7 => 'Rating Pending - Likely Mature 17+'
                                ]
                                @endphp
                                @foreach ($esrbRatings as $key => $value)
                                    <option value="{{ $value }}" @selected(old('esrb_rating') ?? $game->esrb_rating == $value)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">PEGI Rating</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="pegi_rating">
                                <option></option>
                                @php
                                $pegiRatings = [
                                    1 => 'PEGI 3',
                                    2 => 'PEGI 7',
                                    3 => 'PEGI 12',
                                    4 => 'PEGI 16',
                                    5 => 'PEGI 18'
                                ]
                                @endphp
                                @foreach ($pegiRatings as $key => $value)
                                    <option value="{{ $value }}" @selected(old('pegi_rating') ?? $game->pegi_rating == $value)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">CERO Rating</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="cero_rating">
                                <option></option>
                                @php
                                $ceroRatings = [
                                    1 => 'CERO A',
                                    2 => 'CERO B',
                                    3 => 'CERO C',
                                    4 => 'CERO D',
                                    5 => 'CERO Z'
                                ]
                                @endphp
                                @foreach ($ceroRatings as $key => $value)
                                    <option value="{{ $value }}" @selected(old('cero_rating') ?? $game->cero_rating == $value)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Input group-->
                        <div class="fv-row col-md-6 mb-5">
                            <!--begin::Label-->
                            <label class="form-label fs-6 fw-bolder text-dark">ACB Rating</label>
                            <!--end::Label-->
                            <!--begin::Input-->
                            <select class="form-select" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" name="acb_rating">
                                <option></option>
                                @php
                                $acbRatings = [
                                    1 => 'E',
                                    2 => 'G',
                                    3 => 'PG',
                                    4 => 'M',
                                    5 => 'MA',
                                    6 => 'MA 15+',
                                    7 => 'R 18+',
                                    8 => 'X 18+'
                                ]
                                @endphp
                                @foreach ($acbRatings as $key => $value)
                                    <option value="{{ $value }}" @selected(old('acb_rating') ?? $game->acb_rating == $value)>
                                        {{ $value }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <!--end::Input group-->

                        <!--begin::Alert-->
                        <div class="alert alert-primary d-flex align-items-center p-5 mb-5">
                            <span class="svg-icon svg-icon-2hx svg-icon-primary me-4">
                                <i class="fal fa-circle-info"></i>
                            </span>
                            <div class="d-flex flex-column">
                                <h4 class="mb-1 text-primary">Hold On!</h4>
                                <p>Before you submit your update, please ensure you've followed <a href="https://internal.myvideogamelist.com/w/job-guidelines/game-database-administrator/">our guidelines</a>. Thank you!</p>
                            </div>
                        </div>
                        <!--end::Alert-->

                        <!--begin::Actions-->
                        <!--begin::Submit button-->
                        <button type="submit" class="btn btn-primary">
                            <span class="indicator-label">Submit</span>
                            <span class="indicator-progress">Please wait...
                                <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
                            </span>
                        </button>
                        <!--end::Submit button-->
                        <!--end::Actions-->
                    </form>
                </div>
            </div>
        </div>
        <!--end::Col-->
    </div>
    <!--end::Row-->
</x-base-layout>
