<?php

namespace App\Http\Controllers;

use App\Models\Pengunduran;
use Illuminate\Http\Request;

class PengunduranController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['pengunduran'] = Pengunduran::get();
        return view('tindak-lanjut.pengunduran-diri.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validate = $request->validate([
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'hari' => 'required|max:100',
            'tanggal' => 'required|date',
            'alasan' => 'required|string',
        ]);

        $lastSurat = Pengunduran::whereYear('created_at', now()->year)
            ->orderByDesc('id_pengunduran')
            ->first();

        $lastNumber = 0;

        if ($lastSurat && preg_match('/^\d+/', $lastSurat->nomor_surat, $matches)) {
            $lastNumber = (int) $matches[0];
        }

        $newNumber = str_pad($lastNumber + 1, 3, '0', STR_PAD_LEFT);
        $nomorSurat = $newNumber . '/.../SMKN1BJP/KCD.WILVI/.../k.2024';

        // dd([$validate, $nomorSurat]);

        Pengunduran::create([
            'id_siswa' => $validate['id_siswa'],
            'hari' => $validate['hari'],
            'tanggal' => $validate['tanggal'],
            'nomor_surat' => $nomorSurat,
            'alasan' => $validate['alasan'],
        ]);

        $notification = array(
            'message' => "Data Pengunduran siswa berhasil ditambahkan",
            'alert-type' => 'success'
        );

        return redirect()->route('tindak-lanjut.pengunduran-diri.index')->with($notification);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id_pengunduran)
    {
        $validate = $request->validate([
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'hari' => 'required|max:100',
            'tanggal' => 'required|date',
            'alasan' => 'required|string',
        ]);

        $pengunduran = Pengunduran::findOrFail($id_pengunduran);

        $pengunduran->update([
            'id_siswa' => $validate['id_siswa'],
            'hari' => $validate['hari'],
            'tanggal' => $validate['tanggal'],
            'alasan' => $validate['alasan'],
        ]);

        $notification = [
            'message' => 'Data pengunduran siswa berhasil diperbaharui',
            'alert-type' => 'success'
        ];

        return redirect()->route('tindak-lanjut.pengunduran-diri.index')->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id_pengunduran)
    {
        $pengunduran = Pengunduran::findOrFail($id_pengunduran);

        $pengunduran->delete();

        $notificaton = array(
            'message' => 'Data pengunduran siswa berhasil dihapus',
            'alert-type' => 'success'
        );

        return redirect()->route('tindak-lanjut.pengunduran-diri.index')->with($notificaton);
    }
}
