<?php

namespace App\Http\Controllers\Masters;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ServicesType;
use App\DataTables\ServicesTypeDataTable;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;

class ServicesTypeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(ServicesTypeDataTable $dataTable)
    {
        return $dataTable->render('master.servicestype.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('master.servicestype.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'remark' => 'required',
        ]);

        $input = $request->all();
        $servicestyp_add = new ServicesType($input);
        $servicestyp_add->save();
        return redirect('services/type')->with('success', 'Services Type Created Successfully');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $servicestypes = ServicesType::where('id','=',$id)->get();
        return view('master.servicestype.show', compact('servicestypes'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $servicestypes = ServicesType::where('id','=',$id)->get();
        return view('master.servicestype.edit', compact('servicestypes'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request)
    {
        // dd($request->all());
        $validated = $request->validate([
            'name' => 'required|max:255',
            'remark' => 'nullable',
        ]);

        $input = $request->all();
        $servicestype_update = ServicesType::find($request->id);
        $servicestype_update->update($input);
        return redirect('services/type')->with('success', 'Services Type Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Request $request)
    {
        $servicestype_delete = ServicesType::find($request->id);
        $servicestype_delete->delete();
        Session::flash('success', __('Services Type Data Delete Succesfully'));
        $data            = array();
        return Response::json([
            'success' => true,
            'data'    => $data,
        ]);
    }
}
