<?php

namespace App\Http\Controllers;

use App\DataTables\ServiceDataTable;
use App\Models\Service;

use Illuminate\Http\Request;

class ServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(ServiceDataTable $dataTable)
    {
        if(auth()->user()->can('show_services')){
    
            return $dataTable->render('services.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, [
            'ship_id' => 'numeric',
            'room_type_id' => 'nullable|numeric',
            'car_id' => 'nullable|numeric',
            'article_id' => 'nullable|numeric',
            'origin_port' => 'required',
            'destination_port' => 'required',
            'price' => 'numeric',
            'price_two' => 'numeric',
            'qty'=> 'numeric'

        ]);
        return Service::create($data);
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Service  $service
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Service $service)
    {
        $service->status = !$service->status;
        $service->save();
        return $service;
    }

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