<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Aws\FileSystem;
use App\BlogCategories;
use App\Media;
class BlogCategoriesController extends AdminController
{
    public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/blog_categories";
        $this->breadcrumbs[1]['name'] = "Blog Categories";
    }
    
    public function index() {
        $blog_categories = BlogCategories::get();

        $data["pageHeader"] = false;
        $data["model"] = $blog_categories;
        $data["title"] = "Blog Categories";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/blog_categories/index', $data);
    }
    
    public function create() {
        $data["pageHeader"] = false;
        $data["model"] = new BlogCategories();
        $data["title"] = "Blog Categories";
        $data["subTitle"] = "Create";

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

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/blog_categories/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"]);
            if (empty($input["key"])) {
                $input["key"] = slugify($input["title"]);
            } else {
                $input["key"] = slugify($input["key"]);
            }
            if (!isset($input["Status"])) {
                $input["Status"] = 0;
            }
            $model = new BlogCategories();
            $input["created_at"] = date("Y-m-d H:i:s");
            $id = $model->insertGetId($input);
           
//              if (!empty($request->image) && $request->image != null) {
//                $media_id = Media::getMediaID($request->image);
//                $input["image"] = $request->image;
//                $input["image_media_id"] = $media_id;
//            }
                     

            \Session::flash('success', 'Blog Categories added successfully!');
            return redirect('admin/blog_categories');
        }
    }
    
     public function show($id) {

        $q = new BlogCategories();
        $data['model'] = $q->find($id);
        $data["title"] = "Blog Categories";
     
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->title;

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

        return view('admin/blog_categories/show', $data);
    }
    
    public function edit($id) {

        $q = new BlogCategories();
        $banners = $q->find($id);
     
        $data['model'] = $banners;

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

        return view('admin/blog_categories/edit', $data)->with('id', $id);
    }
    
    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 (empty($input["key"])) {
                $input["key"] = slugify($input["title"]);
            } else {
                $input["key"] = slugify($input["key"]);
            }
            if (!isset($input["Status"])) {
                $input["Status"] = 0;
            }
            
//             if (!empty($request->image) && $request->image != null) {
//                $media_id = Media::getMediaID($request->image);
//                $input["image"] = $request->image;
//                $input["image_media_id"] = $media_id;
//            }

            $input["updated_at"] = date("Y-m-d H:i:s");
            BlogCategories::where('id', $id)->update($input);
            \Session::flash('success', 'Blog Categories updated successfully!');
            return redirect('admin/blog_categories');
        }
    }
    
    public function delete($id) {


        BlogCategories::where('id', $id)->delete();
        \Session::flash('error', 'Blog Categories updated successfully!');
        return redirect('admin/blog_categories');
    }
}
