<?php

namespace App\Http\Controllers;

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

class MessageTypeController 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 = MessageType::latest()->get();
            }
            else
            {
                $data = MessageType::onlyTrashed()->get();
            }

            return Datatables::of($data)
                    ->addIndexColumn()
                    ->addColumn('edit', function($row){
   
                           $btn = '<a href="/messageType/'. $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('messageType.index');
    }

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

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

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

   

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

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

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

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

    public function list()
    {
        $data=MessageType::latest()->get();
        return compact('data');
    }
}
