<?php

namespace App\Http\Controllers;

use App\Models\Sp;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class SpController extends Controller
{
    public function index()
    {
        $data['sp'] = Sp::get();
        return view('tindak-lanjut.sp.index', $data);
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        $validate = $request->validate([
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'hari' => 'required|max:100',
            'tanggal' => 'required|date',
            'waktu' => 'required|date_format:H:i',
            'jenis_sp' => 'required|max:100',
            'permasalahan' => 'required|string',
        ]);

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

        Sp::create([
            'id_siswa' => $validate['id_siswa'],
            'hari' => $validate['hari'],
            'tanggal' => $validate['tanggal'],
            'waktu' => $validate['waktu'],
            'jenis_sp' => $validate['jenis_sp'],
            'nomor_surat' => $nomorSurat,
            'permasalahan' => $validate['permasalahan'],
        ]);

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

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

    public function show(string $id)
    {
        //
    }

    public function edit(string $id_sp)
    {
        //
    }

    public function update(Request $request, string $id_sp)
    {
        $validate = $request->validate([
            'id_siswa' => 'required|exists:siswas,id_siswa',
            'hari' => 'required|max:100',
            'tanggal' => 'required|date',
            'waktu' => 'required|',
            'jenis_sp' => 'required|max:100',
            'permasalahan' => 'required|string',
        ]);

        $sp = Sp::findOrFail($id_sp);

        $sp->update([
            'id_siswa' => $validate['id_siswa'],
            'hari' => $validate['hari'],
            'tanggal' => $validate['tanggal'],
            'waktu' => $validate['waktu'],
            'jenis_sp' => $validate['jenis_sp'],
            'permasalahan' => $validate['permasalahan'],
        ]);

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

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

    public function destroy(string $id_sp)
    {
        $sp = Sp::findOrFail($id_sp);

        $sp->delete();

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

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

    public function print($id_sp)
    {
        $sp = Sp::with('siswa')->findOrFail($id_sp);

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

        $template = 'tindak-lanjut.sp.cetak_' . $jenis;

        $pdf = Pdf::loadView($template, compact('sp'))
            ->setPaper('A4', 'portrait');

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