<?php

namespace App\Http\Controllers\CMS;


use App\Http\Controllers\Controller;
use App\Models\SidebarWidget;
use Input;


class CMSSidebarController extends Controller
{

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

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'widgets' => SidebarWidget::all()->sortBy('pos'),//Feedback::where('lvl', 1)->get(),
        ];
        return view('cms.sidebar.index', $data);

    }



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

        }else{
            $widget = SidebarWidget::findOrFail($id);
            $id = $widget->id;
        }
        $data = [
            'widget' => $widget,
            'id' => $id,
        ];
        return view('cms.sidebar.detail', $data);
    }


    public function store($id)
    {
        $data = Input::all();
        if($id == "new") {
            $widget = new SidebarWidget();
        }else{
            $widget = SidebarWidget::findOrFail($id);
        }

        $data['active'] = isset($data['active']) ? true : false;
        $data['show_at'] = isset($data['show_at']) ? $data['show_at'] : null;

        $widget->fill($data)->save();

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

    }

    public function delete($id){
        $widget = SidebarWidget::findOrFail($id);
        $widget->delete();
        \Session()->flash('alert-success', __('Sidebar Widget gelöscht'));
        return redirect(route('cms_sidebar'));
    }




}
