<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\CategoryModel;
use DataTables;
use Gate;
class CategoryController extends Controller
{
    function __construct()
    {
         $this->middleware('permission:Category-List');
         $this->middleware('permission:Category-Create', ['only' => ['create','store']]);
         $this->middleware('permission:Category-Edit', ['only' => ['edit','update']]);
         $this->middleware('permission:Category-Delete', ['only' => ['destroy']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('administrator.categories.index');
    }

    public function datatable_get_categories(Request $request)
    {  
        $category_code = $request->code;
        $category_name = $request->category_name;
        $category = CategoryModel::when(request('code'),function($query) use($category_code){
            $query->where("category_code",$category_code);
        })
        ->when(request('category_name'),function($query) use($category_name){
            $query->where("category_name",$category_name);
        })
        ->get();
        return  Datatables::of($category)
           ->addIndexColumn()
           ->editColumn('category_des', function ($category)
           {
               return substr($category->category_des, 0, 150)." ...";
           })
           ->addColumn('action',function($category){
               $btn_delete='';
               $btn_edit = "";
               if(Gate::check('Category-Edit')) {
                   $btn_edit = '<a href="' . route('categories.edit', $category->id) . '" class="button_success fa fa-edit" data-toggle="tooltip" data-placement="top" title="Edit"></a>';
               }
               if ($category->status == 1)
                   if (Gate::check('Category-Delete'))
                       $btn_delete .= ' <a href="#" data-id = "' . $category->id . '" data-toggle="modal" data-target="#form-delete" class="button_danger fa fa-trash confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"></a>';
               return $btn_edit.$btn_delete;
           })
           ->make(true);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('administrator.categories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'category_code'      => 'required',
            'category_name'      => 'required',
        
        ]);
        $category = new CategoryModel;
        $category->category_code = $request->category_code;
        $category->category_name = $request->category_name;
        $category->category_des = $request->category_des;
        $category->save();
        \LogActivity::addToLog('Category created successfully');
        return redirect()->route('categories.index')
        ->with('success','Category created successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category = CategoryModel::find($id);
        return view('administrator.categories.edit',compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $category = CategoryModel::find($id);
        $this->validate($request, [
            'category_code'      => 'required',
            'category_name'      => 'required',
        
        ]);
        $category->category_code = $request->category_code;
        $category->category_name = $request->category_name;
        $category->category_des = $request->category_des;
        $category->update();
        \LogActivity::addToLog('Category updated successfully');
        return redirect()->route('categories.index')
        ->with('success','Category updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $delete_project = CategoryModel::find($request->delete_id);
        $delete_project->delete();
        \LogActivity::addToLog('Delete Category successfully');
        return response()->json(['success' => true, 'message' => 'Delete Category successfully']);
    }
}
