<?php

namespace App\Http\Controllers;

use App\Models\PON;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class PonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'pons' => PON::orderBy('id', 'desc')->paginate(20),
        ];

        return view('pon.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('pon.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string|max:255|unique:p_o_n_s,name',
            'type' => 'nullable|string|max:255',
            'remarks' => 'nullable|string',
        ]);

        PON::create([
            'name' => $request->name,
            'type' => $request->type,
            'remarks' => $request->remarks,
        ]);

        Toastr::success('PON info created successfully', 'Success');

        return redirect()->route('pons.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $pon = PON::findOrFail($id);

        return view('pon.edit', [
            'pon' => $pon,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|string|max:255|unique:p_o_n_s,name,' . $id,
            'type' => 'nullable|string|max:255',
            'remarks' => 'nullable|string',
        ]);

        $pon = PON::findOrFail($id);
        $pon->name = $request->name;
        $pon->type = $request->type;
        $pon->remarks = $request->remarks;
        $pon->save();

        Toastr::success('PON info updated successfully', 'Success');

        return redirect()->route('pons.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // $pon = PON::findOrFail($id);
        // $pon->delete();

        // Toastr::success('PON info deleted successfully', 'Success');

        // return redirect()->route('pons.index');
    }
}

