<?php

namespace App\Http\Controllers;

use App\DataTables\RoomTypeDataTable;
use App\Models\RoomType;
use Illuminate\Http\Request;

class RoomTypeController extends Controller
{
    public function index(RoomTypeDataTable $dataTable)
    {
      if(auth()->user()->can('show_tyoe_of_rooms')){

        return $dataTable->render('room-types.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 = $request->validate([
            'name' => 'required',
            'code' => 'required|max:3',
            'capacity' => 'integer|required',
            'status' => 'boolean',
            'is_double' => 'boolean'
        ]);
        return RoomType::create($data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->validate([
            'name' => 'required',
            'code' => 'required|max:3',
            'capacity' => 'integer|required',
            'status' => 'boolean|required',
             'is_double' => 'boolean'

        ]);
        return RoomType::findOrFail($id)->update($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return RoomType::find($id);
    }

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

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