@extends('layouts.admin')

@php
    if($create){
        $title = __('adminforms.products.title_insert');
        $formpar = array('route' => 'admin.product.store', 'files' => true, 'id' => 'prodform');
    } else {
        $title = __('adminforms.products.title_update');
        $formpar = array('route' => array('admin.product.update', $product->prod_id), 'method' => 'PUT', 'files' => true, 'id' => 'prodform');
    }

    $tr = [
        'prod id'       => __('adminforms.products.prod_id'),
        'brand id'      => __('adminforms.products.brand_id'),
        'brand name'    => __('adminforms.products.brand_name'),
        'cat id'        => __('adminforms.products.cat_id'),
        'cat name'      => __('adminforms.products.cat_name'),
        'prod name'     => __('adminforms.products.prod_name'),
        'prod sku'      => __('adminforms.products.prod_sku'),
        'prod price'    => __('adminforms.products.prod_price'),
        'prod weight'   => __('adminforms.products.prod_weight'),
        'prod timg'     => __('adminforms.products.prod_timg'),
        'prod simg'     => __('adminforms.products.prod_simg'),
        'prod mimg'     => __('adminforms.products.prod_mimg'),
        'prod bimg'     => __('adminforms.products.prod_bimg'),
        'prod sdesc'    => __('adminforms.products.prod_sdesc'),
        'prod atdes'    => __('adminforms.products.prod_atdes'),
        'prod status'   => __('adminforms.products.prod_status'),
        'pos id'        => __('adminforms.products.pos_id'),
        'pos name'      => __('adminforms.products.pos_name'),
        'prod admin'    => __('adminforms.products.prod_admin'),
        'prod desc'     => __('adminforms.products.prod_desc'),
        'prod mdesc'    => __('adminforms.products.prod_mdesc'),
        'pd name'       => __('adminforms.products.pd_name'),
        'pd path'       => __('adminforms.products.pd_path'),
        'pd type'       => __('adminforms.products.pd_type'),
        'prod title'    => __('adminforms.products.prod_title'),
        'prod metadesc' => __('adminforms.products.prod_metadesc'),
        'prod keyw'     => __('adminforms.products.prod_keyw'),
        'prod video'    => __('adminforms.products.prod_video'),
        'images'        => __('adminforms.products.images'),
        'slug'             => __('adminforms.common.slug'),
    ];
@endphp

@section('title'){{$title}}@endsection
@section('header'){{$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
            </div>

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

            <div class="col-xs-12">
                @if($errors->any())
                    <div style="font-weight: bold; color: #9e0505">{{__('adminforms.common.correct_errors')}}</div>
                    @foreach ($errors->all() as $error)
                        @php
                        $error = strtr($error, $tr);
                        $error = preg_replace('/(images)\.(\d+)/i', $tr['images'], $error);
                        $error = preg_replace('/\.(\d+)/i', '', $error);
                        @endphp
                        <div style="color: #9e0505;">{{$error}}</div>
                    @endforeach
                    <br>
                @endif

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

                    <div class="panel-body">
                        <div class="row">

                            <div class="col-xs-12">
                                @php $btntext = ($create) ? __('adminforms.products.btn_insert') : __('adminforms.products.btn_update')@endphp
                                {{Form::button($btntext, ['class' => 'btn btn-success pull-right', 'id' => 'prodsbmtbutt'])}}
                                <br clear="all">
                                <hr>
                            </div>

                            <div class="col-xs-12 col-sm-6 col-md-6 pbb">
                                {{Form::label('prod_name', ucfirst(__('adminforms.products.prod_name').':'))}}
                                {{Form::text('prod_name', old('prod_name', $product->prod_name ), ['class' => 'form-control', 'id' => 'prodname'])}}
                                <small class="error">{{ strtr($errors->first('prod_name'),$tr) }}</small>
                            </div>


                            <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                @php $pgId = old('brand_id', $product->brand_id); @endphp
                                {{Form::label('brand_id', ucfirst(__('adminforms.products.brand_id').':'))}}
                                {{Form::select('brand_id', $brands, $pgId, ['placeholder' => __('adminforms.products.select_brand'), 'class' => 'form-control', 'id' => 'brandid'])}}
                                <small class="error">{{ strtr($errors->first('brand_id'),$tr) }}</small>
                            </div>

                            <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                @php $pgId = old('cat_id', $product->cat_id); @endphp
                                {{Form::label('cat_id', ucfirst(__('adminforms.products.cat_id').':'))}}
                                {{Form::select('cat_id', $catergories, $pgId, ['placeholder' => __('adminforms.products.select_category'), 'class' => 'form-control', 'id' => 'catid'])}}
                                <small class="error">{{ strtr($errors->first('cat_id'),$tr) }}</small>
                            </div>

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

                                @php
                                    $t_slug             = '';

                                    $route = $product->route($lang);

                                    if($route){
                                        $t_slug             = $route->slug;
                                    }

                                @endphp

                                <div class="col-xs-12 col-sm-6 col-md-6 pbb">
                                    {{Form::label($lang.'_'.'slug', strtoupper($lang).' '.ucfirst(__('adminforms.common.slug').__('adminforms.common.slug_info').':'))}}
                                    {{Form::text($lang.'_'.'slug', old($lang.'_'.'slug', $t_slug ), ['class' => 'form-control', 'id' => $lang.'slug'])}}
                                    <small class="error">{{ strtr($errors->first($lang.'_'.'slug'),$tr) }}</small>
                                </div>
                            @endforeach

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

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

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

                            <div class="col-xs-12 col-sm-6 col-md-3 pbb">
                                @php $pgId = old('pos_id', $product->pos_id); @endphp
                                {{Form::label('pos_id', ucfirst(__('adminforms.products.pos_id').':'))}}
                                {{Form::select('pos_id', $positions, $pgId, ['placeholder' => __('adminforms.products.select_position'), 'class' => 'form-control', 'id' => 'brand-select'])}}
                                <small class="error">{{ strtr($errors->first('pos_id'),$tr) }}</small>
                            </div>

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

                            






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



                <div class="box">

                    <div class="panel panel-default">

                        <div class="panel-heading">
                            <h3 class="panel-title"><i class="fa fa-google"></i> {{__('adminforms.products.meta_block')}} - <small>{{__('adminforms.products.meta_block_info')}}</small></h3>
                        </div>

                        <div class="panel-body">
                            <div class="row">

                                <div class="col-xs-12 col-sm-6 col-md-4">
                                    {{Form::label('prod_title', ucfirst(__('adminforms.products.prod_title').':'))}}
                                    {{Form::textarea('prod_title', old('prod_title', isset($product->metatags->prod_title) ? $product->metatags->prod_title : ''), ['class' => 'form-control', 'rows' => 3])}}
                                    <small class="error">{{ strtr($errors->first('prod_title'),$tr) }}</small>
                                </div>

                                <div class="col-xs-12 col-sm-6 col-md-4">
                                    {{Form::label('prod_metadesc', ucfirst(__('adminforms.products.prod_metadesc').':'))}}
                                    {{Form::textarea('prod_metadesc', old('prod_metadesc', isset($product->metatags->prod_metadesc) ? $product->metatags->prod_metadesc : ''), ['class' => 'form-control', 'rows' => 3])}}
                                    <small class="error">{{ strtr($errors->first('prod_metadesc'),$tr) }}</small>
                                </div>

                                <div class="col-xs-12 col-sm-6 col-md-4">
                                    {{Form::label('prod_keyw', ucfirst(__('adminforms.products.prod_keyw').':'))}}
                                    {{Form::textarea('prod_keyw', old('prod_keyw', isset($product->metatags->prod_keyw) ? $product->metatags->prod_keyw : ''), ['class' => 'form-control', 'rows' => 3])}}
                                    <small class="error">{{ strtr($errors->first('prod_keyw'),$tr) }}</small>
                                </div>




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


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


                <div class="box">

                    <div class="panel panel-default">

                        <div class="panel-heading">
                            <h3 class="panel-title"><i class="fa fa-picture-o"></i> {{__('adminforms.products.images_block')}}</h3>
                        </div>

                        <div class="panel-body">

                            @include('admin.products.prodimages', ['prodimages' => $prodimages, 'prodatrdata' => $prodatrdata ])

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


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


                <div class="box">

                    <div class="panel panel-default">

                        <div class="panel-heading">
                            <h3 class="panel-title"><i class="fa fa-file-pdf"></i> {{__('adminforms.products.documents_block')}}</h3>
                        </div>

                        <div class="panel-body">

                            @include('admin.products.proddocuments', ['documents' => $documents ])

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


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




                <div class="row">
                    <div class="col-md-9">

                        <div class="box">

                            <div class="panel panel-default">

                                <div class="panel-heading">
                                    <h3 class="panel-title"><i class="fa fa-list-alt"></i> {{__('adminforms.products.descriptions_block')}}</h3>
                                </div>

                                <div class="panel-body">
                                    @php
                                    @endphp
                                    <div class="row">

                                        <div class="col-xs-12">
                                            {{Form::label('prod_mdesc', ucfirst(__('adminforms.products.prod_mdesc').':'))}}
                                            <br><small class="error">{{ strtr($errors->first('prod_mdesc'),$tr) }}</small>
                                            {{Form::textarea('prod_mdesc', old('prod_mdesc', isset($product->description->prod_mdesc) ? $product->description->prod_mdesc : ''), ['class' => 'form-control editor', 'id' => 'prodmdesc'])}}
                                        </div>

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

                                        <div class="col-xs-12">
                                            {{Form::label('prod_desc', ucfirst(__('adminforms.products.prod_desc').':'))}}
                                            <br><small class="error">{{ strtr($errors->first('prod_desc'),$tr) }}</small>
                                            {{Form::textarea('prod_desc', old('prod_desc', isset($product->description->prod_desc) ? $product->description->prod_desc : ''), ['class' => 'form-control editor', 'id' => 'prodspec'])}}
                                        </div>



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


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




                    </div><!-- / left column -->
                    <div class="col-md-3">

                        <div class="box">

                            <div class="panel panel-default">

                                <div class="panel-heading">
                                    <h3 class="panel-title"><i class="fa fa-youtube-play"></i> {{__('adminforms.products.video_block')}}</h3>
                                </div>

                                <div class="panel-body">
                                    <div class="row">

                                        <div class="col-xs-12">
                                            {{Form::label('prod_video', ucfirst(__('adminforms.products.prod_video').':'))}}
                                            {{Form::textarea('prod_video', old('prod_video', isset($product->video->prod_video) ? $product->video->prod_video : ''), ['class' => 'form-control', 'rows' => 3])}}
                                            <small class="error">{{ strtr($errors->first('prod_video'),$tr) }}</small>
                                        </div>

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


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


                        <div class="box">

                            <div class="panel panel-default">

                                <div class="panel-heading">
                                    <h3 class="panel-title"><i class="fa fa-list-ul"></i> {{__('adminforms.products.attributes_block')}}</h3>
                                </div>

                                <div class="panel-body">
                                    <div class="row">
                                        <div id="filters" style="padding: 10px;">
                                            @include('admin.products.attributesselects', ['catatrids' => $catatrids, 'prodatrdata' => $prodatrdata ])
                                        </div>

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


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


                    </div><!-- / right column -->
                </div>




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


            <br clear="all">
            <hr>
            <div class="col-xs-12">
                @php $btntext = ($create) ? __('adminforms.products.btn_insert') : __('adminforms.products.btn_update')@endphp
                {{Form::button($btntext, ['class' => 'btn btn-success pull-right', 'id' => 'prodsbmtbutt2'])}}
            </div>



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

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

    </section>
@endsection

@push('css')

@endpush


@push('scripts')
    <script src="{{asset('assets/js/equalpbb.js')}}"></script>
    <script src="{{asset('ckeditor/ckeditor.js')}}"></script>
    <script>
        // omogucavanje na mobilnim uredjajima
        //CKEDITOR.env.isCompatible = true;
    </script>
    <script src="{{asset('ckeditor/ckeditor_autogrow.js')}}"></script>
    <script src="{{asset('ckeditor/adapters/jquery.js')}}"></script>
    <script>
        CKEDITOR.config.jqueryOverrideVal = true;
        CKEDITOR.config.extraPlugins = "ckeditor_autogrow";
        CKEDITOR.config.baseFloatZIndex = 102000;
    </script>
    <script type="text/javascript">
        $(document).ready(function(){
            //alert('ready');
            if($(".editor").length) {
                $("#prodmdesc").ckeditor({toolbar: 'Basic', minHeight: 100, width: "100%"});
                $("#prodspec").ckeditor({toolbar: 'Basic', minHeight: 350, width: "100%"});
            }
        });
    </script>

    <script>

        function string_to_slug(str) {




            var str = str.replace(/\s/g, '-').replace(/\//g, '-').replace(/-{2,}/g, '-').toLowerCase();
            str = str.replace(/^\s+|\s+$/g, '');

            // Make the string lowercase
            str = str.toLowerCase();

            // Remove accents, swap ñ for n, etc
            var from = "ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;čćžšđČĆŽŠĐ";
            var to   = "AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------cczsdCCZSD";
            for (var i=0, l=from.length ; i<l ; i++) {
                str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
            }

            // Remove invalid chars
            str = str.replace(/[^a-z0-9 -]/g, '')
            // Collapse whitespace and replace by -
                .replace(/\s+/g, '-')
                // Collapse dashes
                .replace(/-+/g, '-');

            var protectedWords = ['panel','user','cisco'];
            $.each(protectedWords, function (index, item) {
                if (str.match("^"+item)) {
                    var ln = item.length;
                    str = str.slice(ln);
                    if(str.match("^-")){
                        str = str.slice(1);
                    }
                    alert('Url ne moze da pocinje sa rezervisanom rečju '+item+' pa je stoga uklonjen sa pocetka url-a');
                }
            });

            return str;
        }

        $(document).ready(function(){


            @php
            $functions = '';
            @endphp

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

            @php
                $functions .= 'get'.$lang.'Route();'."\n";
            @endphp

            function get{{$lang}}Route(){
                var cval = $.trim($('#prodname').val());
                var bname = $.trim($('#brandid').find('option:selected').text());
                var string = bname+' '+cval;
                var cslug = string_to_slug(string);
                $('#{{$lang}}slug').val(cslug);
            }
            {{--
            $('#{{$lang}}slug').blur(function () {
                var cval = $.trim($(this).val());
                var cval = cval.replace(/\s/g, '-').replace(/\//g, '-').replace(/-{2,}/g, '-').toLowerCase();
                var cval = string_to_slug(cval);
                $(this).val(cval);
            });
            --}}
            @endforeach

            {{--{{$functions}}--}}

            $('#prodname').keyup(function () {
                {{$functions}}
            });
            $('#prodname').blur(function () {
                {{$functions}}
            });
            $('#brandid').change(function () {
                {{$functions}}
            });
        });
    </script>

    <script src="{{asset('assets/js/bootstrap-filestyle.min.js')}}"></script>
    @include('admin.products.onpagescripts')
@endpush