<?php

namespace App\Http\Controllers;


use App\Models\Attribute;
use App\Models\ProductAttribute;
use App\Models\Shipping;
use App\Models\UserLevel;
use App\Models\UserLevelMargin;
use Request;
use Validator;


class UserLevelController extends Controller
{

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

    public function index()
    {

        $data = [
            'values' => UserLevel::all(),
            'trans' => array_keys(config('localization.supportedLocales')),
        ];
        return view('admin.level.index', $data);
    }

    public function edit($id)
    {
        if($id === "new"){
            $user_level = new UserLevel();
            $user_level->active = 1;

        }else{
            $user_level = UserLevel::findOrFail($id);

        }
        $data = [
            'value' => $user_level,
        ];
        return view('admin.level.edit', $data);


    }
    public function store()
    {

        $data = Request::all();

        if($data['action'] === 'user_level') {
            if ($data['id'] === "new") {
                $user_level = UserLevel::create([
                    'name' => $data['name'],
                    'pos' => $data['pos'],
                    'content' => $data['content'],
                    'partner_provision' => isset($data['partner_provision']) ? true : false,
                    'active' => isset($data['active']) ? true : false,
                ]);
            } else {
                $user_level = UserLevel::find($data['id']);
                $user_level->name = $data['name'];
                $user_level->pos = $data['pos'];
                $user_level->content = $data['content'];
                $user_level->partner_provision = isset($data['partner_provision']) ? true : false;
                $user_level->active = isset($data['active']) ? true : false;
                $user_level->save();
            }

            if (!empty($data['trans'])) {
                $trans = [];
                foreach ($data['trans'] as $lang => $value) {
                    if ($value && $value != null) {
                        $trans[$lang] = $value;
                    }
                }
                if (count($trans)) {
                    $user_level->trans_name = $trans;
                    $user_level->save();
                }
            }
        }

        if($data['action'] === 'user_level_margin') {
            $user_level = UserLevel::findOrFail($data['user_level_id']);
            $rules = array('price_from' => 'required');
            $ret = ['value' => $user_level];
            $validator = Validator::make(Request::all(), $rules);
            if ($validator->fails()) {
                return view('admin.level.edit', $ret)->withErrors($validator);
            }
            if ($data['id'] === "new") {
                $user_level_margin = UserLevelMargin::create($data);
            } else {
                $user_level_margin = UserLevelMargin::findOrFail($data['id']);
                if($user_level_margin->user_level_id != $user_level->id){
                    abort(404);
                }
                $user_level_margin->fill($data);
                $user_level_margin->save();
            }
        }

        \Session()->flash('alert-save', '1');
        return redirect(route('admin_level_edit', [$user_level->id]));

    }

    public function delete($do, $id)
    {
        if($do === 'user_level_margin'){
            $model = UserLevelMargin::findOrFail($id);
            $id = $model->user_level->id;
            $model->delete();
            \Session()->flash('alert-success', "Eintrag gelöscht");
            return redirect(route('admin_level_edit', [$id]));
        }

    }


    /*public function delete($id){

        if(ProductAttribute::where('attribute_id', $id)->count()){
            \Session()->flash('alert-error', 'Eintrag wird als Produktattribute 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'));
    }
    */

}