@extends('admin.layout.page-app')
@section('page_title', __('label.postages'))
@section('tab_title', __('label.postages'))

@section('content')
    @include('admin.layout.sidebar')

     <!-- Select2 -->
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" />

    <div class="right-content">
        @include('admin.layout.header')

        <div class="body-content">
            <!-- mobile title -->
            <h1 class="page-title-sm">{{__('label.postages')}} </h1>

            <div class="border-bottom row mb-3">
                <div class="col-sm-12">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">{{__('label.dashboard')}}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{__('label.postages')}}</li>
                    </ol>
                </div>
            </div>

            <!-- Search -->
            <form action="{{ route('story.index')}}" method="GET">
                <div class="page-search">
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text" id="basic-addon1">
                                <i class="fa-solid fa-magnifying-glass fa-xl light-gray"></i>
                            </span>
                        </div>
                        <input type="text" name="input_search" value="@if(isset($_GET['input_search'])){{$_GET['input_search']}}@endif" class="form-control" placeholder="{{__('label.search_postages')}}" aria-label="Search" aria-describedby="basic-addon1" autocomplete="off">
                    </div>
                    <div class="sorting w-75">
                        <h6 style="width: 70px;">{{__('label.sort_by')}}</h6>
                        <select class="form-control" name="input_in_charge" id="in_charge_id">
                            <option value="all" selected>{{__('label.all_in_charge')}}</option>
                            @for ($i = 0; $i < count($in_charge); $i++) <option value="{{ $in_charge[$i]['id'] }}" @if(isset($_GET['input_in_charge'])){{ $_GET['input_in_charge'] == $in_charge[$i]['id'] ? 'selected' : ''}} @endif>
                                {{ $in_charge[$i]['full_name'] }}
                                </option>
                                @endfor
                        </select>
                    </div>
                    <div class="">
                        <button class="btn btn-default w-100" type="submit">{{__('label.search')}}</button>
                    </div>
                </div>
            </form>

            <div class="row">
                <div class="col-12 col-sm-6 col-md-4 col-xl-3">
                    <a href="{{ route('story.create') }}" class="add-video-btn">
                        <i class="fa-regular fa-square-plus fa-3x icon" style="color: #818181;"></i>
                        {{__('label.add_postages')}}
                    </a>
                </div>

                @foreach ($data as $key => $value)
                <div class="col-12 col-sm-6 col-md-4 col-xl-3">
                    <div class="card video-card">
                        <div class="position-relative">
                            @if($value->type == "2")
                            <img class="card-img-top" src="{{asset('assets/imgs/no_img.png')}}" alt="">
                            <button class="btn play-btn-top video" data-toggle="modal" data-target="#videoModal" data-video="{{$value->story_url}}" data-image="{{$value->story_url}}">
                                <i class="fa-regular fa-circle-play text-white fa-4x mr-2 mt-2"></i>
                            </button>
                            @else
                            <img class="card-img-top" src="{{$value->story_url}}" alt="">
                            @endif
                            <ul class="list-inline overlap-control" aria-labelledby="dropdownMenuLink">
                                <li class="list-inline-item">
                                    <a class="btn" href="{{route('story.edit', [$value->id])}}" title="Edit">
                                        <i class="fa-solid fa-pen-to-square fa-xl" class="dot-icon" style="color: #4e45b8;"></i>
                                    </a>
                                </li>
                                <li class="list-inline-item">
                                    <a class="btn" href="{{route('story.show', [$value->id])}}" title="Delete" onclick="return confirm('{{ __('label.delete_postages') }}')">
                                        <i class="fa-solid fa-trash-can fa-xl" class="dot-icon" style="color: #4e45b8;"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                        <div class="card-body">
                            <h5 class="card-title">@if($value->in_charge){{ $value->in_charge->full_name }}@endif</h5>
                            <h6>{{ String_Cut($value->description, 25) }}</h5>
                            <div class="d-flex justify-content-between mt-1 mb-0">
                                <span class="article-blog-time ">{{ $value->created_at->format('Y-m-d') }}</span>
                                <div class="article-options">
                                    @if($value->status == 1)
                                    <button class="btn btn-sm show-btn" id="{{$value->id}}">{{__('label.show')}}</button>
                                    @elseif($value->status == 0)
                                    <button class="btn btn-sm hide-btn" id="{{$value->id}}">{{__('label.hide')}}</button>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                @endforeach

                <div class="modal fade" id="videoModal" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered modal-lg">
                        <div class="modal-content">
                            <div class="modal-body p-0 bg-transparent">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true" class="text-dark">&times;</span>
                                </button>
                                <video controls width="800" height="500" preload='none' poster="" id="theVideo" controlsList="nodownload noplaybackrate" disablepictureinpicture>
                                    <source src="">
                                </video>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Pagination -->
            <div class="d-flex justify-content-between align-items-center">
                <div> Showing {{ $data->firstItem() }} to {{ $data->lastItem() }} of total {{$data->total()}} entries </div>
                <div class="pb-5"> {{ $data->links('pagination::bootstrap-4') }} </div>
            </div>
        </div>
    </div>
@endsection

@section('pagescript')
    <!-- Select2 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>

    <script>        
        $("#in_charge_id").select2();
        $("#department_id").select2();
        $(function() {
            $(".video").click(function() {
                var theModal = $(this).data("target"),
                    videoSRC = $(this).attr("data-video"),
                    videoPoster = $(this).attr("data-image"),
                    videoSRCauto = videoSRC + "";

                $(theModal + ' source').attr('src', videoSRCauto);
                $(theModal + ' video').attr('poster', videoPoster);
                $(theModal + ' video').load();
                $(theModal + ' button.close').click(function() {
                    $(theModal + ' source').attr('src', videoSRC);
                });
            });
        });

        $("#videoModal .close").click(function() {
            theVideo.pause()
        });
    </script>
@endsection