<?php

namespace App\Http\Controllers;

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

class IzinP1Controller extends Controller
{
    public function index()
    {
        $data = IzinP1::with('mahasiswa')->orderBy('created_at', 'desc')->get();
        return view('izin_p1.index', compact('data'));
    }

    public function create()
    {
        $mahasiswa = Mahasiswa::all();
        // Generate nosrt
        $lastData = IzinP1::orderBy('id', 'desc')->first();
        $nosrt = $lastData ? intval(explode('/', $lastData->nosrt)[0]) + 1 : 1;
        $nosrt = str_pad($nosrt, 3, '0', STR_PAD_LEFT) . '/UN20.1.3/PP/2026';
        
        return view('izin_p1.create', compact('mahasiswa', 'nosrt'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'nosrt' => 'required',
            'nim' => 'required',
            'judul' => 'required',
            'tanggal' => 'required|date'
        ]);

        $data = $request->except('_token');
        
        // Handle checkboxes (if not present in request, set to false)
        for ($i = 1; $i <= 10; $i++) {
            $data['spp_'.$i] = $request->has('spp_'.$i);
            $data['krs_'.$i] = $request->has('krs_'.$i);
        }
        $data['spi'] = $request->has('spi');
        $data['spf'] = $request->has('spf');
        $data['rekap_nilai'] = $request->has('rekap_nilai');

        IzinP1::create($data);

        return redirect()->route('izin_p1.index')->with('success', 'Data Izin P1 berhasil ditambahkan');
    }

    public function edit(string $id)
    {
        $izin = IzinP1::findOrFail($id);
        $mahasiswa = Mahasiswa::all();
        return view('izin_p1.edit', compact('izin', 'mahasiswa'));
    }

    public function update(Request $request, string $id)
    {
        $request->validate([
            'nosrt' => 'required',
            'nim' => 'required',
            'judul' => 'required',
            'tanggal' => 'required|date'
        ]);

        $izin = IzinP1::findOrFail($id);
        
        $data = $request->except('_token', '_method');
        
        // Handle checkboxes (if not present in request, set to false)
        for ($i = 1; $i <= 10; $i++) {
            $data['spp_'.$i] = $request->has('spp_'.$i);
            $data['krs_'.$i] = $request->has('krs_'.$i);
        }
        $data['spi'] = $request->has('spi');
        $data['spf'] = $request->has('spf');
        $data['rekap_nilai'] = $request->has('rekap_nilai');

        $izin->update($data);

        return redirect()->route('izin_p1.index')->with('success', 'Data Izin P1 berhasil diupdate');
    }

    public function destroy(string $id)
    {
        IzinP1::destroy($id);
        return redirect()->route('izin_p1.index')->with('success', 'Data dihapus');
    }

    public function cetakPdf(string $id)
    {
        $p1 = IzinP1::with('mahasiswa')->findOrFail($id);
        $pdf = Pdf::loadView('izin_p1.pdf', compact('p1'))->setPaper('legal', 'portrait');
        return $pdf->stream('Izin_P1_' . $p1->nim . '.pdf');
    }
}
