<?php

namespace App\Http\Controllers;

use App\DataTables\CarDataTable;
use App\Models\Car;
use Illuminate\Http\Request;

class CarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CarDataTable $dataTable)
    {
        if(auth()->user()->can('show_cars')){
            
           return $dataTable->render('cars.index');
        }
        else{
             return abort(404);

        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $this->validate($request, [
            'type' => 'required|string|max:191',
            'model' => 'required|string|max:191',
            'status' => 'required|boolean',
        ]);

        return  Car::create($data);
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Car  $car
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Car $car)
    {

        $data = $this->validate($request, [
            'type' => 'required|string|max:191',
            'model' => 'required|string|max:191',
            'status' => 'required|boolean',
        ]);
        
        return $car->update($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Car  $car
     * @return \Illuminate\Http\Response
     */
    public function destroy(Car $car)
    {
        return $car->delete();
    }

    public function search(){
        return Car::paginate();
    }
}
