<?php
    if($create){
        $title = __('adminforms.attributes.title_insert');
        $formpar = array('route' => 'admin.attribute.store', 'id' => 'attributeForm');
    } else {
        $title = __('adminforms.attributes.title_update');
        $formpar = array('method' => 'PUT', 'route' => array('admin.attribute.update', $attribute->atr_id) , 'id' => 'attributeForm');
    }

    $tr = [
        
        'atr id'                => __('adminforms.attributes.atr_id'),
        'atr type'              => __('adminforms.attributes.atr_type'),

        'atr val id'            => __('adminforms.attributes.atr_val_id'),

    ];

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

        $tr[$lang.' atr name']       = $lang.' '.__('adminforms.attributes.atr_name');
        $tr[$lang.' atr displ name'] = $lang.' '.__('adminforms.attributes.atr_displ_name');
        $tr[$lang.' atr value']      = $lang.' '.__('adminforms.attributes.atr_value');

    }

?>

<?php $__env->startSection('title'); ?><?php echo e($title); ?><?php $__env->stopSection(); ?>
<?php $__env->startSection('header'); ?><?php echo e($title); ?><?php $__env->stopSection(); ?>
<?php $__env->startSection('header_desc'); ?> <?php $__env->stopSection(); ?>
<?php $__env->startSection('breadcumbs'); ?> <?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <!-- Main content -->
    <section class="content">

        <div class="row">

            <div class="col-xs-12">
                <?php 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;
                        <?php echo e(session('error')); ?>


                    </div>
                <?php endif; ?>
            </div>

            <?php echo Form::open($formpar); ?>


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



                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-list"></i> <?php echo e(__('adminforms.attributes.general_block')); ?></h3>
                    </div><!-- /. panel-heading -->

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

                            

                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                <?php echo e(Form::label('atr_type', ucfirst(__('adminforms.attributes.atr_type').':'))); ?><br>
                                <?php
                                    $cv = old('atr_type', $attribute->atr_type);
                                    $actval = !is_null($cv) ? $cv : 's';
                                ?>
                                <label class="radio-inline"><input type="radio" name="atr_type" value="s" <?php echo e($actval== 's' ? 'checked': ''); ?> > <?php echo e(__('adminforms.attributes.atr_type_s')); ?></label>
                                <label class="radio-inline"><input type="radio" name="atr_type" value="m" <?php echo e($actval== 'm' ? 'checked': ''); ?> > <?php echo e(__('adminforms.attributes.atr_type_m')); ?></label>
                                <small class="error"><?php echo e(strtr($errors->first('atr_type'),$tr)); ?></small>
                            </div>




                            <?php $__currentLoopData = config('app.languages'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <?php

                                    $t_atr_name       = '';
                                    $t_atr_displ_name = '';


                                    if($attribute->translation($lang)){
                                        $t_atr_name       = $attribute->translation($lang)->atr_name;
                                        $t_atr_displ_name = $attribute->translation($lang)->atr_displ_name;
                                    }

                                ?>

                                <div class="col-xs-12"><label class="lang-intro" for=""><?php echo e(ucfirst(__('adminforms.common.'.$lang.'_version').':')); ?></label></div>

                                <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                    <?php echo e(Form::label($lang.'_'.'atr_name', strtoupper($lang).' '.ucfirst(__('adminforms.attributes.atr_name').':'))); ?>

                                    <?php echo e(Form::text($lang.'_'.'atr_name', old($lang.'_'.'atr_name', $t_atr_name ), ['class' => 'form-control'])); ?>

                                    <small class="error"><?php echo e(strtr($errors->first($lang.'_'.'atr_name'),$tr)); ?></small>
                                </div>

                                <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                    <?php echo e(Form::label($lang.'_'.'atr_displ_name', strtoupper($lang).' '.ucfirst(__('adminforms.attributes.atr_displ_name').':'))); ?>

                                    <?php echo e(Form::text($lang.'_'.'atr_displ_name', old($lang.'_'.'atr_displ_name', $t_atr_displ_name ), ['class' => 'form-control', 'onclick' => "openKCFinder('images',this, 'images/stickers')", 'style' => "cursor:pointer" ])); ?>

                                    <small class="error"><?php echo e(strtr($errors->first($lang.'_'.'atr_displ_name'),$tr)); ?></small>
                                </div>


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

                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>




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


                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-list"></i> <?php echo e(__('adminforms.attributes.value_block')); ?></h3>
                    </div><!-- /. panel-heading -->

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

                            <?php $oldValues = session()->getOldInput();?>

                            <div class="col-xs-12 text-center" id="valspacer"><strong>Vrednosti</strong><hr><br></div>


                            <?php if(!empty($values)): ?>

                                <?php $__currentLoopData = $values; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                    <div class="form-group valfield col-xs-12">
                                        <?php echo e(Form::label('atr_value', ucfirst(__('adminforms.attributes.atr_value').':'), ['class' => 'col-sm-2 control-label'])); ?>

                                        <div class="col-sm-9 form-inline">

                                            <?php $__currentLoopData = config('app.languages'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                            &nbsp; <label for=""> <?php echo e(strtoupper($lang)); ?> </label>
                                            <?php echo e(Form::text('ov[atr_value]['.$value->atr_val_id.']['.$lang.']', old('ov.atr_value.'.$value->atr_val_id.'.'.$lang, $value->translation($lang)->atr_value), ['class' => 'form-control input-sm av'])); ?>


                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                        </div>
                                        <div class="col-sm-1">
                                            <?php if($value->products_count == 0): ?>
                                                <span class="rem" style="display: block; font-size: 18px; color: #9f191f; cursor: pointer;"><i class="fa fa-times-circle-o"></i></span>
                                            <?php else: ?>
                                                <?php echo e(__('adminforms.common.in_use')); ?> (<?php echo e($value->products_count); ?>)
                                            <?php endif; ?>
                                        </div>
                                        <br clear="all" />
                                        <?php $__currentLoopData = config('app.languages'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <small class="error"><?php echo e(preg_replace('/(ov)\.(atr_value)\.(\d+)\.('.$lang.')/i', $tr[$lang.' atr value'],$errors->first('ov.atr_value.'.$value->atr_val_id.'.'.$lang))); ?></small>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                    </div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>

                          <?php if(isset($oldValues['nv'])): ?>


                                <?php $__currentLoopData = $oldValues['nv']['atr_value']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $val): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                                <div class="form-group valfield col-xs-12">
                                    <?php echo e(Form::label('atr_value', ucfirst(__('adminforms.attributes.atr_value').':'), ['class' => 'col-sm-2 control-label'])); ?>

                                    <div class="col-sm-9 form-inline">
                                        <?php $__currentLoopData = config('app.languages'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            &nbsp; <label for=""> <?php echo e(strtoupper($lang)); ?> </label>
                                            <?php echo e(Form::text('nv[atr_value]['.$i.']['.$lang.']', $oldValues['nv']['atr_value'][$i][$lang], ['class' => 'form-control input-sm av'])); ?>

                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </div>
                                    <div class="col-sm-1">
                                        <span class="rem" style="display: block; font-size: 18px; color: #9f191f; cursor: pointer;"><i class="fa fa-times-circle-o"></i></span>
                                    </div>
                                    <br clear="all" />
                                    <?php $__currentLoopData = config('app.languages'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <small class="error"><?php echo e(preg_replace('/(nv)\.(atr_value)\.(\d+)\.('.$lang.')/i', $tr[$lang.' atr value'],$errors->first('nv.atr_value.'.$i.'.'.$lang))); ?></small>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </div>

                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>




                            <div id="adrembutwrap">
                                <br />
                                <div class="col-sm-12 text-center"><span class="btn btn-default" id="addtrow"><?php echo e(__('adminforms.attributes.btn_new_field')); ?></span></div>
                                <br clear="all" />
                            </div>

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

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





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

            <br clear="all">
            <hr>
            <div class="col-xs-12">
                <?php $btntext = ($create) ? __('adminforms.attributes.btn_insert') : __('adminforms.attributes.btn_update')?>
                <?php echo e(Form::button($btntext, ['class' => 'btn btn-success pull-right', 'id' => 'afsb'])); ?>

            </div>

            <?php echo Form::close(); ?>


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

    </section>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('css'); ?>
<?php $__env->stopPush(); ?>


<?php $__env->startPush('scripts'); ?>
    <script src="<?php echo e(asset('assets/js/equalpbb.js')); ?>"></script>
    <script type="text/javascript">
        $(document).ready(function() {



            $('#addtrow').click( function() {

                var nonvf = $('.nvf').length;



                if(nonvf) {
                   var i = 0;
                    $('.nvf').each(function(){
                        var ci = $(this).data('num');
                        if(ci > i) {i = ci;}
                    });
                    var index = i+1;
                } else {
                    var index = 0;
                }

                //alert(nonvf+' '+index);

                var fhtml =  '<div class="form-group valfield nvf col-xs-12" data-num="'+index+'" >'
                    +'<label for="atr_value" class="col-sm-2 control-label"><?php echo e(__("adminforms.attributes.atr_value").": "); ?></label>'
                    +'<div class="col-sm-9 form-inline">'
                        <?php
                            foreach(config('app.languages') as $lang){
                            echo "+'&nbsp; <label> ".strtoupper($lang).": </label> '
                                 +'<input class=\"form-control input-sm av\" name=\"nv[atr_value]['+index+'][".$lang."]\" type=\"text\">'";
                            }
                        ?>
                    +'</div>'
                    +'<div class="col-sm-1">'
                    +'<span class="rem" style="display: block; font-size: 18px; color: #9f191f; cursor: pointer;"><i class="fa fa-times-circle-o"></i></span>'
                    +'</div>'
                    +'<br clear="all">'
                    +'</div>';


                if($('.valfield').length){
                    $('.valfield:last').after(fhtml);
                } else {
                    $('#valspacer').after(fhtml);
                }
            });


            $(document).on('click', '.rem', function () {
                if(confirm('<?php echo e(__('adminforms.attributes.value_delete_confirm')); ?>')){
                    var target = $(this).closest('.valfield').remove();
                }
            })

        });

        // Provera vrednosti pre submita
        $(document).ready(function() {
            $('#afsb').click(function(){
                var emptyvals = 0;
                $('.av').each(function(){
                    var val = $.trim($(this).val());
                    if(val == ''){emptyvals++;}
                });
                if(emptyvals > 0){
                    $("#attributeForm").submit();//alert('Niste uneli sve vrednosti');
                } else {
                    $("#attributeForm").submit();
                }
            });
        });
    </script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('layouts.admin', \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>