<?php

namespace App\Http\Controllers;



use App\Models\Country;
use Request;


class CountryController extends Controller
{

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

    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $data = [
            'values' => Country::all(),
        ];
        return view('admin.country.index', $data);
    }


    public function edit($id)
    {
        if($id === "new"){
            $model = new Country();
            $model->active = true;
        }else{
            $model = Country::findOrFail($id);
        }
        $data = [
            'country' => $model,
            'trans' => array_keys(config('localization.supportedLocales')),

        ];
        return view('admin.country.edit', $data);
    }

    public function store()
    {

        $data = Request::all();

        $data['active'] = isset($data['active']) ? true : false;
        $data['switch'] = isset($data['switch']) ? true : false;
        $data['own_eur'] = isset($data['own_eur']) ? true : false;
        $data['currency'] = isset($data['currency']) ? true : false;
        $data['currency_faktor'] = $data['currency_faktor'] == "" ? null : reFormatNumber($data['currency_faktor']);

        if(!isset($data['attr'])){
            $data['attr'] = [];
        }
        if($data['id'] === "new"){
            $model = Country::create([
               /* 'parent_id' => null,
                'name' => $data['name'],
                'pos' => $data['pos'],
                'active' => isset($data['active']) ? true : false,
               */
            ]);
        }else{
            $model = Country::find($data['id']);
            $model->fill($data);
            $model->save();
        }


        \Session()->flash('alert-save', '1');
        return redirect(route('admin_country_edit', $model->id));


    }

}