@php
    $tags = App\Models\Tag::whereIn('id', $file->tags)->get();
    $pages = App\Models\Page::get();
    $query = App\Models\Category::where('status', 1)->latest();

    if (auth()->check()) {
        $isOrder = App\Models\Order::where('file_id', $file->id)
            ->where('user_id', auth()->user()->id)
            ->where('type', 1)
            ->where('status', 1)
            ->first();
        $subscribe = isSubscribe(auth()->user()->id);
    }

    // take related files
    $files = App\Models\File::where('category_id', $file->category_id)
        ->whereNotIn('id', [$file->id])
        ->get();

    $biggerWidth = [];
    $biggerHeight = [];

    foreach ($files as $item) {
        if ($item->width > $item->height) {
            $biggerWidth[] = $item;
        } else {
            $biggerHeight[] = $item;
        }
    }

    $orderedExplores = [];
    $biggerWidthCount = count($biggerWidth);
    $biggerHeightCount = count($biggerHeight);

    $rowCount = max(ceil($biggerWidthCount / 2), ceil($biggerHeightCount / 2));

    for ($i = 0; $i < $rowCount; $i++) {
        if ($i % 2 === 0) {
            if (isset($biggerWidth[$i])) {
                $orderedExplores[] = $biggerWidth[$i];
            }

            if (isset($biggerHeight[$i * 2])) {
                $orderedExplores[] = $biggerHeight[$i * 2];
            }

            if (isset($biggerHeight[$i * 2 + 1])) {
                $orderedExplores[] = $biggerHeight[$i * 2 + 1];
            }
        } else {
            if (isset($biggerHeight[$i * 2])) {
                $orderedExplores[] = $biggerHeight[$i * 2];
            }

            if (isset($biggerHeight[$i * 2 + 1])) {
                $orderedExplores[] = $biggerHeight[$i * 2 + 1];
            }

            if (isset($biggerWidth[$i])) {
                $orderedExplores[] = $biggerWidth[$i];
            }
        }
    }

    $latestSix = array_slice($orderedExplores, 0, 6);
    $relatedFiles = collect($latestSix);
@endphp
@extends($activeTemplate . 'layouts.frontend')
@section('content')
    @if ($general->theme == 2)
        <!-- ==================== Header End Here ==================== -->
        <div class="header" id="header">
            <div class="container ">
                <nav class="navbar navbar-expand-lg">
                    <div class="header-menu-wrapper align-items-center d-flex">
                        <div class="logo-wrapper">
                            <a href="{{ route('home') }}" class="navbar-brand logo" id="normal-logo">
                                <img src="{{ getImage('assets/images/general/logo1.png') }}" alt="...">
                            </a>
                        </div>
                    </div>
                    <form id="myForm" class="form-area d-flex" action="{{ route('search.by.type') }}" method="GET">
                        <div class="d-flex align-items-center">
                            <i class="fas fa-image ps-2"></i>
                            <p class="mx-2">@lang('Image')</p>
                        </div>
                        <div class="search">
                            <input id="searchInput" type="text" name="search" class="form--control"
                                value="{{ old('search') }}" placeholder="Search for photo">
                            <i class="fas fa-search"></i>
                        </div>
                    </form>
                    <button class="navbar-toggler header-button" type="button" data-bs-toggle="collapse"
                        data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                        <span id="hiddenNav"><i class="las la-bars"></i></span>
                    </button>

                    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                        <ul class="navbar-nav nav-menu me-auto ps-lg-4 ps-0">
                            @foreach ($pages as $page)
                                @if ($page->slug == 'explore')
                                    <li class="nav-item">
                                        <a class="nav-link {{ Request::url() == url('/') . '/' . $page->slug ? 'active' : '' }}"
                                            href="{{ route('pages', $page->slug) }}">{{ __($page->name) }}</a>
                                    </li>
                                @endif
                            @endforeach
                            @guest
                                @php
                                    $categories = $query->take(3)->get();
                                @endphp
                                @foreach ($categories as $item)
                                    <li class="nav-item">
                                        <a class="nav-link  {{ Request::url() == route('files.category', $item->id) }} ? 'active' : '' }}"
                                            href="{{ route('files.category', $item->id) }}">{{ __($item->name) }}</a>
                                    </li>
                                @endforeach
                            @endguest
                            @auth
                                @php
                                    $categories = $query->take(2)->get();
                                @endphp
                                @foreach ($categories as $item)
                                    <li class="nav-item">
                                        <a class="nav-link  {{ Request::url() == route('files.category', $item->id) }} ? 'active' : '' }}"
                                            href="{{ route('files.category', $item->id) }}">{{ __($item->name) }}</a>
                                    </li>
                                @endforeach
                            @endauth

                        </ul>
                        <div class="d-flex lan">
                            <i class="fas fa-globe"></i>
                            <select class="form-select langSel">
                                @foreach ($language as $item)
                                    <option value="{{ $item->code }}" @if (session('lang') == $item->code) selected @endif>
                                        {{ __($item->name) }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="login">
                            @guest
                                <a class="my-auto" href="{{ route('user.login') }}"><i
                                        class="fa-solid fa-user-large"></i>@lang('Sign In')</a>
                            @endguest

                            @auth
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('user.home') }}">@lang('Dashboard')</a>
                                </li>
                            @endauth
                        </div>
                    </div>
                </nav>
            </div>
        </div>
        <!-- ==================== Header End Here ==================== -->
    @endif
    <section class=" item-details py-100">
        <div class="container">
            <div class="row g-4">
                <div class="col-lg-8 col-md-7 col-12">
                    <div class="thumb-main">
                        <img src="{{ getImage(getFilePath('preview') . '/' . $file->file) }}" alt="image">
                        <div class="info">
                            @php
                                if (Auth::check()) {
                                    $wish = App\Models\Wishlist::where('user_id', auth()->user()->id)
                                        ->where('file_id', $file->id)
                                        ->first();
                                }
                            @endphp

                            @guest
                                <button class="btn-img myGuestWishlistButton"><i class="fas fa-heart"></i></button>
                            @endguest
                            @auth
                                @if (isset($wish))
                                    <button value="{{ $file->id }}" class="btn-img markSingleWishlist"><i
                                            class="fas fa-heart text--base"></i></button>
                                @else
                                    <button value="{{ $file->id }}" class="btn-img markSingleWishlist"><i
                                            class="fas fa-heart"></i></button>
                                @endif
                            @endauth
                        </div>
                    </div>
                    <div class="content">
                        <h5>{{ __($file->title) }}</h5>
                        <p>{{ __($file->description) }}</p>
                        <div class="tag">
                            <p>@lang('Related tags')</p>
                            @foreach ($tags as $item)
                                <a href="{{ route('search.by.tag', $item->id) }}"><i
                                        class="fas fa-search"></i>{{ __($item->name) }}</a>
                            @endforeach
                        </div>
                    </div>

                </div>
                <div class="col-lg-4 col-md-5 col-12">
                    @if (isset($file->is_premium))
                        <div class="right-side">
                            <h6>@lang('Ways to shop')</h6>
                            <form action="{{ route('user.payment') }}" method="POST" class="mt-4">
                                @csrf
                                <input type="hidden" name="file_id" value="{{ $file->id }}">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="payment" value="1"
                                        id="flexRadioDefault1">
                                    <label class="form-check-label" for="flexRadioDefault1">
                                        {{ $general->cur_sym }}{{ $general->image_price }} @lang('for this image')
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="payment" value="2"
                                        id="flexRadioDefault2" checked>
                                    <label class="form-check-label" for="flexRadioDefault2">
                                        {{ $general->image_quantity }}
                                        @lang('images')/{{ $general->package_type == 1 ? 'month' : 'year' }}
                                        @lang('for')
                                        {{ $general->cur_sym }}{{ $general->package_price }}
                                    </label>
                                </div>
                                <div class="mt-4">
                                    @guest
                                        <button type="submit" class="btn button w-100 myGuestDownloadButton"><i
                                                class="fas fa-download"></i>
                                            @lang('Download')</button>
                                    @endguest

                                    @auth
                                        @if (isset($isOrder))
                                            <a href="{{ route('user.file.download', $file->id) }}"
                                                class="btn button w-100"><i class="fas fa-download"></i>
                                                @lang('Download')</a>
                                        @elseif (isset($subscribe))
                                            <a href="{{ route('user.file.get', $file->id) }}" class="btn button w-100"><i
                                                    class="fas fa-download"></i>
                                                @lang('Download')</a>
                                        @elseif (auth()->user()->id == $file->user_id)
                                            <button type="submit" class="btn button w-100 ownFile"><i
                                                    class="fas fa-download"></i>
                                                @lang('Download')</button>
                                        @else
                                            <button type="submit" class="btn button w-100"><i class="fas fa-download"></i>
                                                @lang('Download')</button>
                                        @endif

                                    @endauth
                                </div>
                            </form>

                            <div class="type">
                                <div class="details">
                                    <i class="fas fa-certificate"></i>
                                    <h6>@lang('Vast Collection')</h6>
                                </div>
                                <div class="details">
                                    <i class="fas fa-sign-in-alt"></i>
                                    <h6>@lang('Sign In first to buy image')</h6>
                                </div>
                                <div class="details">
                                    <i class="fas fa-file"></i>
                                    <h6>@lang('File type'): @lang('PNG'), @lang('JPG'), @lang('JPEG')
                                    </h6>
                                </div>
                            </div>
                        </div>
                    @else
                        <div class="right-side">
                            <h6>@lang('Download this image for free!')</h6>

                            <div class="mt-4">
                                @guest
                                    <button type="submit" class="btn button w-100 myGuestDownloadButton"><i
                                            class="fas fa-download"></i>
                                        @lang('Free Download')</button>
                                @endguest

                                @auth
                                    <a href="{{ route('user.file.download', $file->id) }}" class="btn button w-100"><i
                                            class="fas fa-download"></i>
                                        @lang('Free Download')</a>

                                @endauth
                            </div>

                            <div class="type">
                                <div class="details">
                                    <i class="fas fa-certificate"></i>
                                    <h6>@lang('Vast Collection')</h6>
                                </div>
                                <div class="details">
                                    <i class="fas fa-sign-in-alt"></i>
                                    <h6>@lang('Sign In first to buy image')</h6>
                                </div>
                                <div class="details">
                                    <i class="fas fa-file"></i>
                                    <h6>@lang('File type'): @lang('PNG'), @lang('JPG'), @lang('JPEG')
                                    </h6>
                                </div>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
            <div class="more categories-item">
                <div>
                    <h4>@lang('More Related Photos')</h4>
                </div>
                @if ($relatedFiles->isEmpty())
                    <div class="d-flex justify-content-start mt-5 mb-3">
                        <h4 class="no-file">@lang('Looks like there are no related file here.')</h4>
                    </div>
                @endif
                <div class="row g-4 justify-content-center ">
                    @foreach ($relatedFiles as $item)
                        @php
                            $tags = App\Models\Tag::whereIn('id', $item->tags)
                                ->take(3)
                                ->get();

                            if (Auth::check()) {
                                $Wishlist = App\Models\Wishlist::where('user_id', auth()->user()->id)
                                    ->where('file_id', $item->id)
                                    ->first();
                            }
                        @endphp
                        @if ($item->width > $item->height)
                            <div class="col-lg-6 col-12">
                                <div class="thumb">
                                    <a href="{{ route('file.details', $item->id) }}">
                                        <img src="{{ getImage(getFilePath('preview') . '/' . $item->file) }}"
                                            alt="image">
                                    </a>
                                    <div class="info">
                                        @guest
                                            <button class="btn-img myGuestWishlistButton"><i
                                                    class="fas fa-heart"></i>@lang('Add')</button>
                                        @endguest
                                        @auth
                                            @if (isset($Wishlist))
                                                <button value="{{ $item->id }}" class="btn-img markWishlist"><i
                                                        class="fas fa-heart text--base"></i>@lang('Added')</button>
                                            @else
                                                <button value="{{ $item->id }}" class="btn-img markWishlist"><i
                                                        class="fas fa-heart"></i>@lang('Add')</button>
                                            @endif
                                        @endauth
                                    </div>
                                    <div class="tag">
                                        @foreach ($tags as $item)
                                            <a href="{{ route('search.by.tag', $item->id) }}"
                                                class="text-nowrap">{{ $item->name }}</a>
                                        @endforeach
                                    </div>
                                </div>
                            </div>
                        @else
                            <div class="col-lg-3 col-md-6 col-12">
                                <div class="thumb">
                                    <a href="{{ route('file.details', $item->id) }}">
                                        <img src="{{ getImage(getFilePath('preview') . '/' . $item->file) }}"
                                            alt="image">
                                    </a>
                                    <div class="info">
                                        @guest
                                            <button class="btn-img myGuestWishlistButton"><i
                                                    class="fas fa-heart"></i>@lang('Add')</button>
                                        @endguest
                                        @auth
                                            @if (isset($Wishlist))
                                                <button value="{{ $item->id }}" class="btn-img markWishlist"><i
                                                        class="fas fa-heart text--base"></i>@lang('Added')</button>
                                            @else
                                                <button value="{{ $item->id }}" class="btn-img markWishlist"><i
                                                        class="fas fa-heart"></i>@lang('Add')</button>
                                            @endif
                                        @endauth
                                    </div>
                                    <div class="tag">
                                        @foreach ($tags as $item)
                                            <a href="{{ route('search.by.tag', $item->id) }}"
                                                class="text-nowrap">{{ $item->name }}</a>
                                        @endforeach
                                    </div>
                                </div>
                            </div>
                        @endif
                    @endforeach
                </div>
            </div>
        </div>
    </section>
@endsection

@push('script')
    <script>
        $(document).on('click', '.myGuestDownloadButton', function() {
            window.location.href = "{{ route('user.login') }}";
        });
    </script>

    <script>
        $(document).on('click', '.myGuestWishlistButton', function() {
            window.location.href = "{{ route('user.login') }}";
        });
    </script>

    <script>
        $(document).on('click', '.ownFile', function() {
            Toast.fire({
                icon: 'error',
                title: 'You can\'t buy your own file.'
            })
            return false;
        });
    </script>

    <script>
        $(document).on('click', '.markWishlist', function(e) {
            e.preventDefault();
            var file_id = $(this).val();
            var button = $(this);
            $.ajax({
                url: "{{ route('user.mark.wishlist') }}",
                data: {
                    file_id: file_id,
                },
                method: "GET",
                success: function(data) {
                    if (data.status == true) {
                        var innerData = `<i class="fas fa-heart text--base"></i>@lang('Added')`;
                        button.html(innerData);
                        Toast.fire({
                            icon: 'success',
                            title: 'Successfully set this image to your wishlist.'
                        })
                        return false;
                    }

                    if (data.status == false) {
                        var innerData = `<i class="fas fa-heart"></i>@lang('Add')`;
                        button.html(innerData);
                        Toast.fire({
                            icon: 'success',
                            title: 'Successfully removed this image from your wishlist.'
                        })
                        return false;
                    }

                    if (data.auth == false) {
                        window.location.href = "{{ route('user.login') }}";
                    }
                },
                error: function(xhr, status, error) {},
            });

        });
    </script>
    <script>
        $(document).on('click', '.markSingleWishlist', function(e) {
            e.preventDefault();
            var file_id = $(this).val();
            var button = $(this);
            $.ajax({
                url: "{{ route('user.mark.wishlist') }}",
                data: {
                    file_id: file_id,
                },
                method: "GET",
                success: function(data) {
                    if (data.status == true) {
                        var innerData = `<i class="fas fa-heart text--base"></i>`;
                        button.html(innerData);
                        Toast.fire({
                            icon: 'success',
                            title: 'Successfully set this image to your wishlist.'
                        })
                        return false;
                    }

                    if (data.status == false) {
                        var innerData = `<i class="fas fa-heart"></i>`;
                        button.html(innerData);
                        Toast.fire({
                            icon: 'success',
                            title: 'Successfully removed this image from your wishlist.'
                        })
                        return false;
                    }

                    if (data.auth == false) {
                        window.location.href = "{{ route('user.login') }}";
                    }
                },
                error: function(xhr, status, error) {},
            });

        });
    </script>

    <script>
        $(document).ready(function() {
            'use strict';
            $('#searchInput').keypress(function(e) {
                if (e.which === 13) {
                    e.preventDefault();
                    $('#myForm').submit();
                }
            });
        });
    </script>
@endpush
