<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\LengthInch;
use App\Models\DiameterInch;
use App\Models\BoltWithNutsInch;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
use App\DataTables\BoltWithNutsInchDataTable;

class BoltWithNutsInchController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(BoltWithNutsInchDataTable $dataTable)
    {
        return $dataTable->render('allbolts.boltwithnutsinch.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $lengths = LengthInch::all();
        $diameters = DiameterInch::all();
        return view('allbolts.boltwithnutsinch.create', compact('lengths','diameters'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // dd($request->all());
        $validator = Validator::make($request->all(),
        [
            'length' => 'required',
            'diameter' => 'required',
            'value' => 'required|max:255',
        ],
        [
            'length.required' => 'The Length field is required',
            'diameter.required' => 'The Diameter field is required',
            'value.required' => 'The Value field is required',
            'value.max' => 'The Value field enter only 255 characters',
        ]);

        if ($validator->fails()){
            return redirect('admin/boltwithnuts/inch/create')->withErrors($validator)->withInput();
        }

        // Retrieve the validated input...
        $validated = $validator->validated();
        $bncreate = new BoltWithNutsInch($validated);
        $bncreate->save();
        return redirect('admin/boltwithnuts/inch')->with('success', 'The Bolt with Nuts-Inch has been created.');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $lengths = LengthInch::all();
        $diameters = DiameterInch::all();
        $shows = BoltWithNutsInch::where('id','=',$id)->get();
        return view('allbolts.boltwithnutsinch.show', compact('lengths','diameters','shows'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $lengths = LengthInch::all();
        $diameters = DiameterInch::all();
        $edits = BoltWithNutsInch::where('id','=',$id)->get();
        return view('allbolts.boltwithnutsinch.edit', compact('lengths','diameters','edits'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(),
        [
            'length' => 'required',
            'diameter' => 'required',
            'value' => 'required|max:255',
        ],
        [
            'length.required' => 'The Length field is required',
            'diameter.required' => 'The Diameter field is required',
            'value.required' => 'The Value field is required',
            'value.max' => 'The Value field enter only 255 characters',
        ]);

        if ($validator->fails()){
            return redirect('admin/boltwithnuts/inch/edit',$id)->withErrors($validator)->withInput();
        }

        // Retrieve the validated input...
        $validated = $validator->validated();
        $bnupdate = BoltWithNutsInch::find($request->id);
        $bnupdate->update($validated);
        return redirect('admin/boltwithnuts/inch')->with('success', 'The Bolt with Nuts-Inch has been updated.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Request $request)
    {
        $bolt = BoltWithNutsInch::find($request->id);
        $bolt->delete();
        \Session::flash('success', __('Bolt with Nuts-Inch deleted succesfully'));
        $data            = array();
        return Response::json([
            'success' => true,
            'data'    => $data,
        ]);
    }
}
