<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\AccessoriesModel;
use DataTables;
use DB;
use Gate;
use Illuminate\Support\Facades\Crypt;
class AccessoriesController extends Controller
{
    function __construct()
    {
         $this->middleware('permission:Accessories-Feature-List');
         $this->middleware('permission:Accessories-Feature-Create', ['only' => ['create','store']]);
         $this->middleware('permission:Accessories-Feature-Edit', ['only' => ['edit','update']]);
         $this->middleware('permission:Accessories-Feature-Delete', ['only' => ['delete_accessories']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('administrator.accessories.index');
    }

    public function datatable_get_accessories()
    {
        $property = AccessoriesModel::all();
        return  Datatables::of($property)
            
            ->editColumn('action', function ($property) {
                if(Gate::check('Accessories-Feature-Edit')) {
                    return '<a href="' . Route("accessories.edit", Crypt::encrypt($property->id)) . '" class="button_success fa fa-edit"></a>';
                }
            })
            ->addColumn('status', function($property) {
                if($property->status == 0){
                    return ' 
                    <a style="color:red" href="#" class="viewPopLink" role="button" data-id="'.$property->id.'" data-status="'.$property->status.'" data-toggle="modal tooltip" data-target="#myModal" data-placement="top" title="Done"><span class="fa fa-check"></span> Deactive</a>
                    ';    
                }else{
                    return ' 
                     <a style="color:green" href="#" class="viewPopLink" role="button" data-id="'.$property->id.'" data-status="'.$property->status.'" data-toggle="modal tooltip" data-target="#myModal" data-placement="top" title="Done"><span class="fa fa-check"></span> Activated</a>
                    ';    
                }
               
            })
             ->addIndexColumn()
            ->rawColumns(['status' => 'status','action' => 'action'])
        
       
        ->make(true);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('administrator.accessories.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, [
            'title'      => 'required',
            'description'     => 'required',
        
        ]);
        $property = new AccessoriesModel;
        $property->title = $request->title;
        $property->description = $request->description;
        $property->save();
        \LogActivity::addToLog('Accessory created successfully');
        return redirect()->route('accessories.index')
        ->with('success','Accessory 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)
    {
        $decrypt_id = Crypt::decrypt($id);
        $accessorie = AccessoriesModel::find($decrypt_id);
        return view('administrator.accessories.edit',compact('accessorie'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $property = AccessoriesModel::find($id);
        $this->validate($request, [
            'title'      => 'required',
            'description'     => 'required',
        
        ]);
        $property->title = $request->title;
        $property->description = $request->description;
        $property->save();
        \LogActivity::addToLog('Accessory updated successfully');
        return redirect()->route('accessories.index')
        ->with('success','Accessory updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // public function destroy($id)
    // {
    //     $property = AccessoriesModel::find($id)->delete();
    //     return back();
    // }

    public function delete_accessories(Request $request, $id){
        $status = 0;
        if($request->status == 0){
            $status = 1;
        }else{
            $status = 0;
        }
        AccessoriesModel::where('id',$id)
        ->update(['status'=> $status]);
        return redirect()->back();
    }

}
