<?php

namespace App\Http\Controllers;


use Request;
use App\Models\Attribute;
use App\Models\AttributeType;
use App\Models\ProductAttribute;


class AttributeController extends Controller
{

    public function __construct()
    {
        $this->middleware('copyreader');
    }

    public function index()
    {

        $data = [
            'attribute_types' => AttributeType::all(),
            'attributes' => Attribute::all(),
            'trans' => array_keys(config('localization.supportedLocales')),
        ];
        return view('admin.attribute.index', $data);
    }



    public function store()
    {

        $data = Request::all();
        if(isset($data['action'])){

            if( $data['action']  === "attribute-type"){
                if($data['id'] == "new"){
                    $model = AttributeType::create([
                        'parent_id' => null,
                        'name' => $data['name'],
                        'description' => $data['description'],
                        'pos' => $data['pos'],
                        'active' => isset($data['active']) ? true : false,
                    ]);
                }else{
                    $model = AttributeType::find($data['id']);
                    $model->parent_id = null;
                    $model->name = $data['name'];
                    $model->description = $data['description'];
                    $model->pos = $data['pos'];
                    $model->active = isset($data['active']) ? true : false;
                    $model->save();
                }
            }
            if($data['action']  === "attribute"){
                if($data['id'] == "new"){
                    $model = Attribute::create([
                        'parent_id' => null,
                        'attribute_type_id' => $data['attribute_type_id'],
                        'name' => $data['name'],
                        'value' => $data['value'],
                        'pos' => $data['pos'],
                        'active' => isset($data['active']) ? true : false,
                    ]);
                }else{
                    $model = Attribute::find($data['id']);
                    $model->parent_id = null;
                    $model->name = $data['name'];
                    $model->value = $data['value'];
                    $model->attribute_type_id = $data['attribute_type_id'];
                    $model->pos = $data['pos'];
                    $model->active = isset($data['active']) ? true : false;
                    $model->save();
                }
            } 
        }
       
        /*if(!empty($data['trans'])){
            $trans = [];
            foreach ($data['trans'] as $lang => $value){
                if($value && $value != null){
                    $trans[$lang] = $value;
                }
            }
            if(count($trans)){
                $model->trans_name = $trans;
                $model->save();
            }
        }*/

        \Session()->flash('alert-save', '1');
        return redirect(route('admin_product_attributes'));


    }


    public function delete($attr, $id){

        if($attr === 'type'){
            if(Attribute::where('attribute_type_id', $id)->count()){
                \Session()->flash('alert-error', 'Attribute Type wird bei den Attributen verwendet');
                return redirect(route('admin_product_attributes'));
            }
            /*   if(AttributeType::where('parent_id', $id)->count()){
                    \Session()->flash('alert-error', 'Eintrag wird als Main Attribute verwendet');
                    return redirect(route('admin_product_attributes'));
                }
            */
            $model = AttributeType::findOrFail($id);
            $model->delete();
            \Session()->flash('alert-success', 'Attribute Type gelöscht');
            return redirect(route('admin_product_attributes'));
        }

        if($attr === 'attr'){
            if(ProductAttribute::where('attribute_id', $id)->count()){
                \Session()->flash('alert-error', 'Attribute wird bei den Produkten verwendet');
                return redirect(route('admin_product_attributes'));
            }
         /*   if(Attribute::where('parent_id', $id)->count()){
                \Session()->flash('alert-error', 'Eintrag wird als Main Attribute verwendet');
                return redirect(route('admin_product_attributes'));
            }
         */
            $model = Attribute::findOrFail($id);
            $model->delete();
            \Session()->flash('alert-success', 'Eintrag gelöscht');
            return redirect(route('admin_product_attributes'));
        }

     
    }

}