@extends('layouts.admin')

@php
    if($create){
        $title = __('adminforms.carousels.title_insert');
        $formpar = array('route' => 'admin.carousel.store', 'autocomplete' => 'off');
    } else {
        $title = __('adminforms.carousels.title_update');
        $formpar = array('route' => array('admin.carousel.update', $carousel->car_id), 'method' => 'PUT', 'autocomplete' => 'off');
    }

    $tr = [
        'car id'     => __('adminforms.carousels.car_id'),
        'car title'  => __('adminforms.carousels.car_title'),
        'car type'   => __('adminforms.carousels.car_type'),
        'car order'  => __('adminforms.carousels.car_order'),
        'car limit'  => __('adminforms.carousels.car_limit'),
        'cg id'      => __('adminforms.carousels.cg_id'),
        'cat id'     => __('adminforms.carousels.cat_id'),
        'brand id'   => __('adminforms.carousels.brand_id'),
        'pos id'     => __('adminforms.carousels.pos_id'),
        'car active' => __('adminforms.carousels.car_active'),
        'prod id'    => __('adminforms.carousels.prod_id'),
    ];
@endphp

@section('title'){{$title}}@endsection
@section('header')<i class="fa fa-th-large"></i> {{$title}}@endsection
@section('header_desc') @endsection
@section('breadcumbs') @endsection

@section('content')
    <!-- Main content -->
    <section class="content">



        <div class="row">

            <div class="col-xs-12">
                @if (session('error'))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                        <i class="icon fa fa-exclamation-triangle"></i> <strong>Info: </strong> &nbsp;  &nbsp;
                        {{ session('error') }}
                    </div>
                @endif

                    @if($errors->any())
                        <div style="font-weight: bold; color: #9e0505">{{__('adminforms.common.correct_errors')}}</div>
                        @foreach ($errors->all() as $error)
                            <div style="color: #9e0505;">{{strtr($error, $tr)}}</div>
                        @endforeach
                        <br>
                    @endif
            </div>

            {!! Form::open($formpar) !!}

            <div class="col-xs-12">
                <div class="box">


                <!-- /.box-header -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title"><i class="fa fa-list-alt"></i> {{__('adminforms.carousels.general_block')}}</h3>
                        </div>
                        <div class="panel-body">


                            <div class="row">



                                <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                    @php $cgId = old('car_type', $carousel->car_type); @endphp
                                    {{Form::label('car_type', ucfirst(__('adminforms.carousels.car_type').':'))}}
                                    {{Form::select('car_type', $types, $cgId, ['placeholder' => __('adminforms.common.select'), 'class' => 'form-control', 'id' => 'car-type-select'])}}
                                    <small class="error">{{ strtr($errors->first('car_type'),$tr) }}</small>
                                </div>


                                <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                    @php $cgId = old('car_order', $carousel->car_order); @endphp
                                    {{Form::label('car_order', ucfirst(__('adminforms.carousels.car_order').':'))}}
                                    {{Form::select('car_order', $sorts, $cgId, ['class' => 'form-control', 'id' => 'car-order-select'])}}
                                    <small class="error">{{ strtr($errors->first('car_order'),$tr) }}</small>
                                </div>

                                <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                    @php $cgId = old('car_limit', $carousel->car_limit); @endphp
                                    {{Form::label('car_limit', ucfirst(__('adminforms.carousels.car_limit').':'))}}
                                    {{Form::selectRange('car_limit', 6, 18, $cgId, [ 'class' => 'form-control', 'id' => 'car-limit-select'])}}
                                    <small class="error">{{ strtr($errors->first('car_limit'),$tr) }}</small>
                                </div>

                                <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                    {{Form::label('car_active', ucfirst(__('adminforms.carousels.car_active').':'))}}<br>
                                    @php
                                        $cv = old('car_active', $carousel->car_active);
                                        $actval = !is_null($cv) ? $cv : 1;
                                    @endphp
                                    <label class="radio-inline"><input type="radio" name="car_active" value="1" {{ $actval== 1 ? 'checked': ''}} > {{__('adminforms.common.true')}}</label>
                                    <label class="radio-inline"><input type="radio" name="car_active" value="0" {{ $actval== 0 ? 'checked': ''}} > {{__('adminforms.common.false')}}</label>
                                    <small class="error">{{ strtr($errors->first('car_active'),$tr) }}</small>
                                </div>


                                <div class="col-xs-12">
                                    <hr>
                                </div>


                                <div class="col-xs-12" id="itemsblock">
                                    <div class="row">

                                        @php
                                            $oldType = old('car_type',$carousel->car_type);
                                        @endphp


                                        @php $style = ($oldType == 'b' || $oldType == 'cb') ? ' style="display: block;"' : ''; @endphp
                                        <div class="col-xs-12 col-sm-6 col-md-3 pbb caroptsel" id="type_b" {!! $style !!}>
                                            @php $cgId = old('brand_id', $carousel->brand_id); @endphp
                                            {{Form::label('brand_id', ucfirst(__('adminforms.carousels.brand_id').':'))}}
                                            {{Form::select('brand_id', $brands, $cgId, ['placeholder' => __('adminforms.common.select'), 'class' => 'form-control', 'id' => 'group-select'])}}
                                            <small class="error">{{ strtr($errors->first('brand_id'),$tr) }}</small>
                                        </div>

                                        @php $style = ($oldType == 'c' || $oldType == 'cb') ? ' style="display: block;"' : ''; @endphp
                                        <div class="col-xs-12 col-sm-6 col-md-3 pbb caroptsel" id="type_c" {!! $style !!}>
                                            @php $cgId = old('cat_id', $carousel->cat_id); @endphp
                                            {{Form::label('cat_id', ucfirst(__('adminforms.carousels.cat_id').':'))}}
                                            {{Form::select('cat_id', $categories, $cgId, ['placeholder' => __('adminforms.common.select'), 'class' => 'form-control', 'id' => 'group-select'])}}
                                            <small class="error">{{ strtr($errors->first('cat_id'),$tr) }}</small>
                                        </div>

                                        @php $style = ($oldType == 'cg') ? ' style="display: block;"' : ''; @endphp
                                        <div class="col-xs-12 col-sm-6 col-md-3 pbb caroptsel" id="type_cg" {!! $style !!}>
                                            @php $cgId = old('cg_id', $carousel->cg_id); @endphp
                                            {{Form::label('cg_id', ucfirst(__('adminforms.carousels.cg_id').':'))}}
                                            {{Form::select('cg_id', $cgroups, $cgId, ['placeholder' => __('adminforms.common.select'), 'class' => 'form-control', 'id' => 'group-select'])}}
                                            <small class="error">{{ strtr($errors->first('cg_id'),$tr) }}</small>
                                        </div>

                                        @php $style = ($oldType == 'pos') ? ' style="display: block;"' : ''; @endphp
                                        <div class="col-xs-12 col-sm-6 col-md-3 pbb caroptsel" id="type_pos" {!! $style !!}>
                                            @php $cgId = old('pos_id', $carousel->pos_id); @endphp
                                            {{Form::label('pos_id', ucfirst(__('adminforms.carousels.pos_id').':'))}}
                                            {{Form::select('pos_id', $positions, $cgId, ['placeholder' => __('adminforms.common.select'), 'class' => 'form-control', 'id' => 'group-select'])}}
                                            <small class="error">{{ strtr($errors->first('pos_id'),$tr) }}</small>
                                        </div>

                                        @php $style = ($oldType == 'p') ? ' style="display: block;"' : ''; @endphp
                                        <div class="col-xs-12 caroptsel"  id="products" {!! $style !!}>
                                            <label>{{ucfirst(__('adminforms.carousels.type_p').':')}}</label>
                                            @php $oldValues = session()->getOldInput(); @endphp
                                            @if(isset($oldValues['prod_id']))
                                                @foreach($oldValues['prod_id'] as $i => $v)
                                                    <input type="text" name="prod_id[{{$i}}]" value="{{$v}}" class="prod_id" />
                                                @endforeach
                                            @elseif(count($products))
                                                @foreach($products as $p)
                                                    <input type="text" name="prod_id[{{$p->prod_id}}]" value="{{$p->prod_name}}" class="prod_id" />
                                                @endforeach
                                            @else

                                                <input class="prod_id" type="text" value=""  name="prod_id[]">
                                            @endif
                                            <small class="error">{{ strtr($errors->first('prod_id'),$tr) }}</small>

                                        </div>
                                        
                                    </div>
                                </div>


                                <div class="col-xs-12">
                                    <hr>
                                </div>

                                @foreach(config('app.languages') as $lang)

                                    @php

                                        $t_car_title = '';

                                        $translation = $carousel->translation($lang);



                                        if($translation){
                                            $t_car_title = $translation->car_title;
                                        }

                                    @endphp
                                    <div class="col-xs-12"><label class="lang-intro" for="">{{ucfirst(__('adminforms.common.'.$lang.'_version').':')}}</label></div>

                                    <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                        {{Form::label($lang.'_'.'car_title', ucfirst(__('adminforms.carousels.car_title').':'))}}
                                        {{Form::text($lang.'_'.'car_title', old($lang.'_'.'car_title', $t_car_title), ['class' => 'form-control'])}}
                                        <small class="error">{{ strtr($errors->first($lang.'_'.'car_title'),$tr) }}</small>
                                    </div>

                                @endforeach

                            </div><!-- /.row -->
                        </div><!-- /.panel-body -->

                    </div>  <!-- /.box-body -->
                </div>  <!-- /.box -->





            </div> <!-- /.col -->

            <div class="col-xs-12">
                @php $btntext = ($create) ? __('adminforms.carousels.btn_insert') : __('adminforms.carousels.btn_update')@endphp
                {{Form::submit($btntext, ['class' => 'btn btn-success pull-right'])}}
            </div>

            {!! Form::close() !!}

        </div>
        <!-- /.row -->

    </section>
@endsection

@push('css')
    <!-- tags -->
    <link rel="stylesheet" href="{{asset('assets/tags/jquery-ui-1.10.0.custom.css')}}">
    <link rel="stylesheet" href="{{asset('assets/tags/jquery.tageditp.css')}}">
    <!-- /tags -->
@endpush


@push('scripts')
    <script src="{{asset('assets/js/equalpbb.js')}}"></script>

    <script type="text/javascript">
        function openKCFinder(type,field,dir) {
            //field = document.getElementById(field);//type : files / images / flash
            window.KCFinder = {  callBack: function(url) {  window.KCFinder = null;   field.value = url;}};
            window.open("/ckeditor/kcfinder/browse.php?type="+type+"&dir="+dir, "kcfinder_textbox","status=0, toolbar=0, location=0, menubar=0, directories=1,  " + "resizable=1, scrollbars=0, width=800, height=600");}
    </script>


    <!-- tags -->

    <script src="{{asset('assets/tags/jquery-ui.js')}}"></script>
    {{--<script src="{{asset('assets/components/jquery-ui/jquery-ui.min.js')}}"></script>--}}
    <script src="{{asset('assets/tags/jquery.auto-grow-input.min.js')}}"></script>
    <script src="{{asset('assets/tags/jquery.tagedit.js')}}"></script>
    <script src="{{asset('assets/tags/jquery.tageditupdt.js')}}"></script>
    <script src="{{asset('assets/tags/jquery.tageditwp.js')}}"></script>




    <script type="text/javascript" >
        $(function() {

            $("#products").find("input.prod_id").tagedit({
                autocompleteURL: "/panel/ajax-productsidname",
                additionalListClass: "form-control",
                allowEdit: false,
                allowAdd: false,
                autocompleteOptions: { minLength: 2 }
            });
        });

        $(".prod_id").autoGrowInput({ minWidth: 100, maxWidth: 20000, comfortZone: 15 }); // klase tagedit polja ne vrapera


        $(document).ready(function () {
            $('#car-type-select').on('change', function () {
                var val = $.trim($(this).val());


                $('.caroptsel').hide();

                switch(val) {
                    case 'p':
                        $('#products').show();
                        break;
                    case 'cb':
                        $('#type_c,#type_b').show();
                        break;
                    default:
                        $('#type_'+val).show();
                }


            })

        });


    </script>


    <!-- /tags -->

@endpush

