<?php
    $prodattrs = [];
    $oldcatatrids = [];
    $old_cat_id = old('cat_id', NULL);
    if(!is_null($old_cat_id) && trim($old_cat_id) != ''){
        $oldcatatrids = \App\Category::find(trim($old_cat_id))->attributes->pluck('atr_id')->toArray();
    }
?>

<?php if(!empty($oldcatatrids)): ?>
    <?php
    $oldatrvalids  = old('atr_val_id',   NULL);
    $oldmatrvalids = old('m_atr_val_id', NULL);
    $oldprodattrs  = [];

    if(is_array($oldatrvalids) && count($oldatrvalids)){
         foreach($oldatrvalids as $atr => $valid){
            $oldprodattrs[$atr] = $valid;
         }
    }

    if(is_array($oldmatrvalids) && count($oldmatrvalids)){
        foreach($oldmatrvalids as $atr => $vals){
            $oldprodattrs[$atr] = $vals;
         }
    }

    ?>


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

        <?php
            $data = App\Attribute::getAttributeExtendedData($atrid);
        ?>

        <?php if(count($data) > 0): ?>
            <?php
                if(isset($oldprodattrs[$atrid])){
                    $prodatrval = $oldprodattrs[$atrid];
                } else {
                    $prodatrval = ($data[0]->atr_type == 's' ) ? '' : array();
                }
            ?>

            <?php if($data[0]->atr_type == 's' ): ?> 

            <div class="form-group">
                <label><?php echo e($data[0]->atr_displ_name); ?></label>
                <select name="atr_val_id[<?php echo e($data[0]->atr_id); ?>]" class="form-control input-sm atsel">
                    <option value=""><?php echo e(__('adminforms.products.select_atr_s')); ?></option>
                    <?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($v->atr_val_id); ?>" <?php echo ($v->atr_val_id == $prodatrval)? 'selected="selected"' : ''; ?> ><?php echo e(ucfirst($v->atr_value)); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div><!--form-group-->

            <?php else: ?>  

            <div class="form-group">
                <label><?php echo e($data[0]->atr_displ_name); ?> - <small><?php echo e(__('adminforms.products.select_atr_m')); ?></small></label>
                <select name="m_atr_val_id[<?php echo e($data[0]->atr_id); ?>][]" class="form-control input-sm atsel multiple" multiple="multiple">
                    <?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($v->atr_val_id); ?>" <?php echo (in_array($v->atr_val_id,$prodatrval))? 'selected="selected"' : ''; ?> ><?php echo e(ucfirst($v->atr_value)); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div><!--form-group-->

            <?php endif; ?>

        <?php endif; ?>


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


<?php elseif(!empty($catatrids)): ?>


    <?php
        if(count($prodatrdata) > 0){
            foreach($prodatrdata as $row){
                if($row->atr_type == 's'){
                    $prodattrs[$row->atr_id] = $row->atr_val_id;
                } else {
                    $prodattrs[$row->atr_id][] = $row->atr_val_id;
                }
            }
        }
    ?>


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

        <?php
            $data = App\Attribute::getAttributeExtendedData($atrid);
        ?>

        <?php if(count($data) > 0): ?>
            <?php
                if(isset($prodattrs[$atrid])){
                    $prodatrval = $prodattrs[$atrid];
                } else {
                    $prodatrval = ($data[0]->atr_type == 's' ) ? '' : array();
                }
            ?>

            <?php if($data[0]->atr_type == 's' ): ?> 

                <div class="form-group">
                <label><?php echo e($data[0]->atr_displ_name); ?></label>
                <select name="atr_val_id[<?php echo e($data[0]->atr_id); ?>]" class="form-control input-sm atsel">
                <option value=""><?php echo e(__('adminforms.products.select_atr_s')); ?></option>
                <?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($v->atr_val_id); ?>" <?php echo ($v->atr_val_id == $prodatrval)? 'selected="selected"' : ''; ?> ><?php echo e(ucfirst($v->atr_value)); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                </div><!--form-group-->

            <?php else: ?>  

                <div class="form-group">
                <label><?php echo e($data[0]->atr_displ_name); ?> - <small><?php echo e(__('adminforms.products.select_atr_m')); ?></small></label>
                <select name="m_atr_val_id[<?php echo e($data[0]->atr_id); ?>][]" class="form-control input-sm atsel multiple" multiple="multiple">
                <?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($v->atr_val_id); ?>" <?php echo (in_array($v->atr_val_id,$prodatrval))? 'selected="selected"' : ''; ?> ><?php echo e(ucfirst($v->atr_value)); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                </div><!--form-group-->

            <?php endif; ?>

        <?php endif; ?>


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

<?php else: ?>
    <div class="text-center"><?php echo e(__('adminforms.products.attr_cat_not_sel')); ?></div>
<?php endif; ?>