@extends('admin.layout.page-app')
@section('page_title', __('label.add_postages'))
@section('tab_title', __('label.add_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.add_postages')}}</h1>
            <div class="border-bottom row mb-3">
                <div class="col-sm-10">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="{{ route('admin.dashboard') }}">{{__('label.dashboard')}}</a>
                        </li>
                        <li class="breadcrumb-item">
                            <a href="{{ route('story.index') }}">{{__('label.postages')}}</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">
                            {{__('label.add_postages')}}
                        </li>
                    </ol>
                </div>
                <div class="col-sm-2 d-flex align-items-center justify-content-end">
                    <a href="{{ route('story.index') }}" class="btn btn-default mw-120" style="margin-top:-14px">{{__('label.postages_list')}}</a>
                </div>
            </div>

            <div class="card custom-border-card">
                <form enctype="multipart/form-data" id="story">
                    <input type="hidden" name="id" value="">
                    <div class="form-row">
                        <div class="col-md-9">
                            <div class="form-row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>{{__('label.in_charge')}}<span class="text-danger">*</span></label>
                                        <select class="form-control" style="width:100%!important;" name="in_charge_id" id="in_charge_id">
                                            <option value="">{{__('label.select_in_charge')}}</option>
                                            @foreach ($in_charge as $key => $value)
                                            <option value="{{ $value->id}}">
                                                {{ $value->full_name }} (@if($value->department){{ $value->department->name }}@endif)
                                            </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>{{__('label.upload_type')}}<span class="text-danger">*</span></label>
                                        <select class="form-control" name="type" id="type">
                                            <option value="1">{{__('label.image')}} </option>
                                            <option value="2">{{__('label.video')}}</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col-md-12 video_box_type">
                                    <div class="form-row">
                                        <div class="form-group col-lg-12 video_box">
                                            <div style="display: block;">
                                                <label for="input-1">{{__('label.upload_video')}}<span class="text-danger">*</span></label>
                                                <div id="filelist_story"></div>
                                                <div id="container_story" style="position: relative;">
                                                    <div class="form-group d-flex align-items-center">
                                                        <input type="file" id="uploadFile_story" name="uploadFile_story" class="form-control import-file mr-4">
                                                        <a id="upload_story" class="btn text-white py-2" style="background-color:#4e45b8;">{{__('label.upload_files')}}</a>
                                                    </div>
                                                    <input type="hidden" name="video" id="mp3_file_name_story" class="form-control">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label>{{__('label.description')}}</label>
                                        <textarea name="description" class="form-control" placeholder="{{__('label.description_here')}}"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3 image-box">
                            <div class="form-group ml-5">
                                <label class="ml-5">{{__('label.image')}}<span class="text-danger">*</span></label>
                                <div class="avatar-upload ml-5">
                                    <div class="avatar-edit">
                                        <input type='file' name="image" id="imageUpload" accept=".png, .jpg, .jpeg" />
                                        <label for="imageUpload" title="Select File"></label>
                                    </div>
                                    <div class="avatar-preview">
                                        <img src="{{asset('assets/imgs/upload_img.png')}}" alt="upload_img.png" id="imagePreview">
                                    </div>
                                </div>
                                <label class="mt-3 ml-5 text-gray">{{__('label.max_size_5mb')}}</label>
                            </div>
                        </div>
                    </div>

                    <div class="border-top mt-2 pt-3 text-right">
                        <button type="button" class="btn btn-default mw-120" onclick="save_story()">{{__('label.save')}}</button>
                        <a href="{{route('story.index')}}" class="btn btn-cancel mw-120 ml-2">{{__('label.cancel')}}</a>
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@section('pagescript')
    <!-- Chunk JS -->
    <script src="{{ asset('/assets/js/plupload.full.min.js')}}"></script>
    <script src="{{ asset('/assets/js/common.js')}}"></script>
    <!-- Select2 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>

    <script>
        $("#in_charge_id").select2();

        $(document).ready(function() {
            $(".video_box").hide();
            $('#type').change(function() {
                var optionValue = $(this).val();

                if (optionValue == '2') {
                    $(".video_box").show();
                    $(".image-box").hide();
                } else {
                    $(".image-box").show();
                    $(".video_box").hide();

                }
            });
        });

        function save_story() {
            var Check_Admin = '<?php echo Demo_Mode(); ?>';
            if (Check_Admin == 1) {
                $("#dvloader").show();
                var formData = new FormData($("#story")[0]);
                $.ajax({
                    type: 'POST',
                    url: '{{ route("story.store") }}',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(resp) {
                        $("#dvloader").hide();
                        get_responce_message(resp, 'story', '{{ route("story.index") }}');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#dvloader").hide();
                        toastr.error(errorThrown, textStatus);
                    }
                });
            } else {
                showError();
            }
        }
    </script>
@endsection