<?php

namespace App\Http\Controllers;

use App\DataTables\ShipsDataTable;
use App\Models\Ship;
use Illuminate\Http\Request;

class ShipController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(ShipsDataTable $dataTable)
    {
        if(auth()->user()->can('show_ships')){
            
          return $dataTable->render('ships.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,[
            'name'=>'required|string|max:191',
            'type'=>'required|string',
            'ship_number'=>'required|string|max:191|unique:ships',   
            'status'=>'boolean'   
        ]);

        return Ship::create($data);
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Ship  $ship
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Ship $ship)
    {
        $data=$this->validate($request,[
            'name'=>'required|string|max:191',
            'type'=>'required|string',
            'ship_number'=>'required|string|max:191|unique:ships,ship_number,'.$ship->id.',id',  
            'status'=>'boolean'   
        ]);
        return $ship->update($data);
    }

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

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