<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Categories;
use App\Http\Controllers\AdminController;
use App\Aws\FileSystem;
use App\WebsiteFaqs;
use App\Media;
use App\CategoryPlan;
use App\Facilities;
class CategoriesController extends AdminController {

    private $action = "categories";
    private $folder = "categories";
    private $title = "Categories";
    private $heading = "Category";

    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 = Categories::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 Categories();
        $data["title"] = $this->title;
        $data["subTitle"] = "Create";
        $data["faqs"] = array();
//        $data["plans"] = array();
        $data["action"] = $this->action;
        $data["folder"] = $this->folder;
        $data["heading"] = $this->heading;
        $data["facilities"] = Facilities::where("status", 1)->orderBy("position", "asc")->pluck('title','id');
        $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|max:200'
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            $faqs = !empty($input["faqs"]) ? $input["faqs"] : null;
            unset($input["_token"]);
            unset($input["image"]);
            unset($input["faqs"]);
        
            $model = new Categories();
            $input["created_at"] = date("Y-m-d H:i:s");

            $input["position"] = isset($input["position"]) ? $input["position"] : 0;
             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);
            WebsiteFaqs::saveFaqs($this->action, $id, $faqs);
//             CategoryPlan::savePlans($id,$plans);
//            if ($request->hasFile('image')) {
//
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeImage($fileName, $file, Config("params.dir_categories_image"), Config("params.dir_categories_image") . "thumbnails/", 1366, 768);
//                Categories::where('id', $id)->update(array("image" => $input['image']));
//            }
            
            \Session::flash('success', $this->heading . ' added successfully!');
            return redirect('admin/categories');
        }
    }

    public function edit($id) {
        $data["model"] = Categories::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;
        $data["faqs"] = WebsiteFaqs::where("type", $this->action)->where("type_id", $id)->get();
        $data["facilities"] = Facilities::where("status", 1)->orderBy("position", "asc")->pluck('title','id');
//        $data["plans"] = CategoryPlan::where("type_id", $id)->get();
        $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 {

            $faqs = !empty($input["faqs"]) ? $input["faqs"] : null;
//            $plans = empty($input["links"]) ? array() : $input["links"];

            $model = Categories::find($id);
            
//             $input["facility_id"] =implode(",",$input["facility_id"]);
            unset($input["faqs"]);
            unset($input["_token"]);
            if (!isset($input["status"])) {
                $input["status"] = 0;
            }
         
            $path = public_path() . "/";
            $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;
               
            }
           
            Categories::where('id', $id)->update($input);

            WebsiteFaqs::saveFaqs($this->action, $id, $faqs);
//            CategoryPlan::savePlans($id,$plans);

//            if ($request->hasFile('image')) {
//
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeImage($fileName, $file, Config("params.dir_categories_image"), Config("params.dir_categories_image") . "thumbnails/", 1366, 768);
//                Categories::where('id', $id)->update(array("image" => $input['image']));
//
//                deleteDirectory($path . Config("params.dir_categories_image") . $model->image);
//                deleteDirectory($path . Config("params.dir_categories_image") . "thumbnails/" . $model->image);
//            }
            


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

    public function delete($id) {


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

    public function show($id) {

        $q = new Categories();
        $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);
    }
    
/*    public function add(Request $request) {
        $input = $request->all();
    

        $rules = [
            'title' => 'required',
            'image' => 'required',
        ];

        $messages = [];

        $validator = Validator::make($input, $rules, $messages);
        $response = array();

        if ($validator->fails()) {
            $errors = $validator->errors();
            $response['errors'] = $errors;
            $response['error'] = true;
            $header = 402;
        } else {
            unset($input["_token"]);
            
            $folder = Config("params.media_path.large");
            $thumb = Config("params.media_path.thumbnail");
            $path = public_path($folder);
            $fileName = $input["image"];
            $array = explode(".", $fileName);
            $type = end($array);
            $response["image"] = $fileName;
//            $response["size"] = filesize($path . "/" . $fileName);
           
            if($type=='svg'){
                $img = url($folder) . "/" . $fileName; 
                 
            }else{
               $img= url($thumb) . "/" . $fileName; 
               
            }
            $response["title"] =$input['title'];
          
            $response["url"]=$img;
            
           $response["id"] ='';
          
           
            $response['error'] = false;
            $header = 200;
           
        }
        return response()->json($response, $header);
    } */

}
