<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\BoltWithNutMsmLength;
use App\Models\BoltWithNutMsmDiameter;
use App\Models\BoltWithNutMsmValue;
use Illuminate\Support\Facades\Validator;
use App\DataTables\BoltWithNutMsmValueDataTable;
use Illuminate\Support\Facades\Response;
// use Redirect;

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

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $lengths = BoltWithNutMsmLength::all();
        $diameters = BoltWithNutMsmDiameter::all();
        return view('allbolts.boltwithnuts.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/ms/create')->withErrors($validator)->withInput();
        }

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

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $lengths = BoltWithNutMsmLength::all();
        $diameters = BoltWithNutMsmDiameter::all();
        $edits = BoltWithNutMsmValue::where('id','=',$id)->get();
        return view('allbolts.boltwithnuts.show', compact('lengths','diameters','edits'));
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(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/ms/edit',$id)->withErrors($validator)->withInput();
        }

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

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