<?php

namespace App\Http\Controllers;

use App\TutorialType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use DataTables;

class TutorialTypeController extends Controller
{
     /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth',['except'=>['list']]);
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // public function index()
    // {
    //     //
    // }

    public function index(Request $request)
    {
       
        if ($request->ajax()) {
            if($request->status=="active")
            {
                $data = TutorialType::latest()->get();
            }
            else
            {
                $data = TutorialType::onlyTrashed()->get();
            }

            return Datatables::of($data)
                    ->addIndexColumn()
                    ->addColumn('edit', function($row){
   
                           $btn = '<a href="/tutorialType/'. $row->id .'/edit" class="edit btn btn-primary btn-sm">Edit</a>';
     
                            return $btn;
                    })
                    ->addColumn('delete', function($row){
                        if(is_null($row->deleted_at))
                        {
                        $btn = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="deleteData btn btn-danger btn-sm">Delete</a>';
                        }
                        else
                        {
                        $btn = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="restoreData btn btn-success btn-sm">Restore</a>';
                        }    
                         return $btn;
                 })
                    ->rawColumns(['edit','delete'])
                    ->make(true);
        }
      
       return view('tutorialType.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('tutorialType.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (TutorialType::Create($request->all())) {
            return true;
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\TutorialType  $tutorialType
     * @return \Illuminate\Http\Response
     */
    public function show(TutorialType $tutorialType)
    {
        return $tutorialType;
    }

   

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\TutorialType  $tutorialType
     * @return \Illuminate\Http\Response
     */
    public function edit(TutorialType $tutorialType)
    {
        return view('tutorialType.edit', compact('tutorialType'));  
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\TutorialType  $tutorialType
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, TutorialType $tutorialType)
    {
        if ($tutorialType->fill($request->all())->save()) {
            return true;
        }
    }

    /**
     * restore the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\TutorialType  $tutorialType
     * @return \Illuminate\Http\Response
     */
    public function restore(Request $request)
    {
         TutorialType::withTrashed()
        ->where('id', $request->id)
        ->restore();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\TutorialType  $tutorialType
     * @return \Illuminate\Http\Response
     */
    public function destroy(TutorialType $tutorialType)
    {
        $tutorialType->deleted_by = Auth::id();;
        $tutorialType->save();
        if($tutorialType->delete()){
            return true;
        }
    }

    public function list()
    {
        $data=TutorialType::latest()->get();

        return compact('data');
        
    }
}
