<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Excesses;
use App\Http\Controllers\AdminController;

class ExcessesController extends AdminController {

    private $action = "excesses";
    private $folder = "excesses";
    private $title = "Excesses";
    private $heading = "Excess";

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

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/" . $this->action;
        $this->breadcrumbs[1]['name'] = $this->title;
    }

    public function index() {

        $model = Excesses::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["action"] = $this->action;
        $data["folder"] = $this->folder;
        $data["heading"] = $this->heading;
        $data["title"] = $this->title;

        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/' . $this->folder . '/index', $data);
    }

    public function create(Request $request) {

        $data["pageHeader"] = false;
        $data["model"] = new Excesses();
        $data["title"] = $this->title;
        $data["subTitle"] = "Create";

        $data["action"] = $this->action;
        $data["folder"] = $this->folder;
        $data["heading"] = $this->heading;

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/' . $this->folder . '/create', $data);
    }

    public function save(Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
                    'title' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            unset($input["_token"]);

            $model = new Excesses();
            $input["created_at"] = date("Y-m-d H:i:s");
            $id = $model->insertGetId($input);

            \Session::flash('success', $this->heading . ' added successfully!');
            return redirect('admin/' . $this->action);
        }
    }

    public function edit($id) {
        $data["model"] = Excesses::find($id);
        $data["pageHeader"] = false;
        $data["action"] = $this->action;
        $data["folder"] = $this->folder;
        $data["heading"] = $this->heading;
        $data["subTitle"] = "Edit";
        $data["title"] = $this->title;
        $data["id"] = $id;
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/' . $this->folder . '/edit', $data);
    }

    public function update($id, Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
                    'title' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            unset($input["_token"]);

            if (!isset($input["status"])) {
                $input["status"] = 0;
            }

            $input["updated_at"] = date("Y-m-d H:i:s");
            Excesses::where('id', $id)->update($input);
            \Session::flash('success', $this->heading . ' updated successfully!');
            return redirect('admin/' . $this->action);
        }
    }

    public function delete($id) {


        Excesses::where('id', $id)->delete();
        \Session::flash('error', $this->heading . ' deleted successfully!');
        return redirect('admin/' . $this->action);
    }

    public function show($id) {

        $q = new Excesses();
        $data['model'] = $q->find($id);
        $data["title"] = $this->title;

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $this->title;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/' . $this->folder . '/show', $data);
    }

}
