<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Moc;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
use App\DataTables\MocDataTable;
use Session;

class MocController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(MocDataTable $dataTable)
    {
        return $dataTable->render('moc.index');
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),
        [
            'mocname' => 'required|string|unique:mocs,mocname|max:255',
            'mocvalue' => 'required|max:255'
        ],
        [
            'mocname.required' => 'The MOC name field is required',
            'mocname.max' => 'The MOC name field enter only 255 characters',
            'mocname.unique' => 'The MOC name has already been taken.',
            'mocvalue.required' => 'The MOC value field is required',
            'mocvalue.max' => 'The MOC value field enter only 255 characters'
        ]);

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

        // Retrieve the validated input...
        $validated = $validator->validated();
        $moccreate = new Moc($validated);
        $moccreate->save();
        return redirect('admin/moc')->with('success', 'The MOC has been created.');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $shows = Moc::findorfail($id);
        return view('moc.show',compact('shows'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $edits = Moc::findorfail($id);
        return view('moc.edit',compact('edits','id'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $validator = Validator::make($request->all(),
        [
            'mocname' => 'required|string|max:255|unique:mocs,mocname,'.$id,
            'mocvalue' => 'required|max:255'
        ],
        [
            'mocname.required' => 'The MOC name field is required',
            'mocname.max' => 'The MOC name field enter only 255 characters',
            'mocname.unique' => 'The MOC name has already been taken.',
            'mocvalue.required' => 'The MOC value field is required',
            'mocvalue.max' => 'The MOC value field enter only 255 characters'
        ]);

        if ($validator->fails()){
            return back()->withErrors($validator)->withInput();
        }
        // Retrieve the validated input...
        $validated = $validator->validated();
        $mocupdate = Moc::find($id);
        $mocupdate->update($validated);
        return redirect('admin/moc')->with('success', 'The MOC has been updated.');
    }

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