<?php

namespace App\Http\Controllers\CMS;


use App\Http\Controllers\Controller;
use App\Models\TravelMagazine;
use Input;
use Validator;

class CMSTravelMagazineController extends Controller
{

/*
* Create a new controller instance.
*
* @return void
*/
    public function __construct()
    {
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'travel_magazines' => TravelMagazine::all(),
        ];
        return view('cms.travel_magazine.index', $data);

    }

    public function detail($id)
    {
        if($id == "new") {
            $model = new TravelMagazine();
            $id = 'new';
            $model->active = 1;

        }else{
            $model = TravelMagazine::findOrFail($id);
            $id = $model->id;
        }
        $data = [
            'travel_magazine' => $model,
            'id' => $id,
        ];
        return view('cms.travel_magazine.detail', $data);

    }


    public function store($id)
    {
        $data = Input::all();
        $data['active'] = isset($data['active']) ? true : false;
        $data['scope'] = isset($data['scope']) ? true : false;
        $rules = array(
            'name' => 'required',
        );
        if($id != "new") {
            $model = TravelMagazine::findOrFail($id);

            $rules = array(
                'name' => 'required',
                'slug' => 'unique:mysql_stern.travel_magazines,slug,'.$model->id,
            );

        }


        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {
            return back()->withErrors($validator);
        }

        if($id == "new") {
            $model = TravelMagazine::create($data);
        }else{
            $model = TravelMagazine::findOrFail($id);
            $model->fill($data);
            $model->save();
        }

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

    }

    public function delete($id){
        $model = TravelMagazine::findOrFail($id);
        $model->delete();
        \Session()->flash('alert-success', __('Eintrag Reisemagazin gelöscht'));
        return redirect(route('cms_travel_magazine'));
    }


}
