<?php

namespace App\Http\Controllers;

use App\Models\PindahSekolah;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;

class PindahSekolahController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['pindahsekolah'] = PindahSekolah::get();
        return view('tindak-lanjut.pindah-sekolah.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',
            'keterangan' => 'required|max:100',
            'tujuan' => 'required|string',
        ]);

        $lastSurat = PindahSekolah::whereYear('created_at', now()->year)
            ->orderByDesc('id_pindah_sekolah')
            ->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]);

        PindahSekolah::create([
            'id_siswa' => $validate['id_siswa'],
            'hari' => $validate['hari'],
            'tanggal' => $validate['tanggal'],
            'keterangan' => $validate['keterangan'],
            'nomor_surat' => $nomorSurat,
            'tujuan' => $validate['tujuan'],
        ]);

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

        return redirect()->route('tindak-lanjut.pindah-sekolah.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_pindah_sekolah)
    {
        $validate = $request->validate([
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'hari' => 'required|max:100',
            'tanggal' => 'required|date',
            'keterangan' => 'required|max:100',
            'tujuan' => 'required|string',
        ]);

        $sp = PindahSekolah::findOrFail($id_pindah_sekolah);

        $sp->update([
            'id_siswa' => $validate['id_siswa'],
            'hari' => $validate['hari'],
            'tanggal' => $validate['tanggal'],
            'keterangan' => $validate['keterangan'],
            'tujuan' => $validate['tujuan'],
        ]);

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

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

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id_pindah_sekolah)
    {
        $pindahsekolah = PindahSekolah::findOrFail($id_pindah_sekolah);

        $pindahsekolah->delete();

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

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

    public function print($id_pindah_sekolah)
    {
        $pindahsekolah = PindahSekolah::with('siswa')->findOrFail($id_pindah_sekolah);

        $jenis = strtolower($pindahsekolah->jenis_sp);

        $pdf = Pdf::loadView('tindak-lanjut.pindah-sekolah.print', compact('pindahsekolah'))
            ->setPaper('A4', 'portrait');

        return $pdf->stream('durat_pindah_sekolah.pdf');
    }
}
