<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Facilities;
use App\Http\Controllers\AdminController;
use App\Media;
class FacilitiesController extends AdminController {

    private $action = "whats_covered";
    private $folder = "whats_covered";
    private $title = "What Covered";
    private $heading = "What Covered";

    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 = Facilities::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 Facilities();
        $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',
                    'description' => 'required'
        ]);

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

            unset($input["_token"]);

            $model = new Facilities();
            $input["created_at"] = date("Y-m-d H:i:s");
            
            if (!empty($request->image) && $request->image != null) {
                $media_id = Media::getMediaID($request->image);
                $input["image"] = $request->image;
                $input["image_media_id"] = $media_id;
            }
            $id = $model->insertGetId($input);

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

    public function edit($id) {
        $data["model"] = Facilities::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',
                    'description' => 'required'
        ]);

        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");
            
            if (!empty($request->image) && $request->image != null) {
                $media_id = Media::getMediaID($request->image);
                $input["image"] = $request->image;
                $input["image_media_id"] = $media_id;
            }
            Facilities::where('id', $id)->update($input);
            \Session::flash('success', $this->heading . ' updated successfully!');
            return redirect('admin/whats_covered');
        }
    }

    public function delete($id) {


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

    public function show($id) {

        $q = new Facilities();
        $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);
    }

}
