<?php

namespace App\Http\Controllers;

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

class KarierController extends Controller
{
    public function index()
    {
        $karier = Karier::selectRaw('MAX(id_karier) as id_karier')
            ->groupBy('id_siswa')
            ->pluck('id_karier');

        $data['karier'] = Karier::with('siswa')
            ->whereIn('id_karier', $karier)
            ->get();

        $data['allkarier'] = Karier::with('siswa')->get()->groupBy('id_siswa');

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

    public function create()
    {
        $data['karier'] = Karier::pluck('name', 'id');
        return view('karier.create', $data);
    }

    public function store(Request $request)
    {
        $validate = $request->validate([
            'tanggal' => 'required|max:255',
            'kampus' => 'required|max:100',
            'prodi' => 'required|max:100',
            'keterangan' => 'required|max:100',
            'id_siswa' => 'required',
        ]);

        $karier = Karier::create([
            'tanggal' => $validate['tanggal'],
            'kampus' => $validate['kampus'],
            'prodi' => $validate['prodi'],
            'keterangan' => $validate['keterangan'],
            'id_siswa' => $validate['id_siswa'],
        ]);

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

        return redirect()->route('karier.index')->with($notification);
    }

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

    public function edit(string $id_karier)
    {
        $data['karier'] = Karier::findOrFail($id_karier);
        return view('karier.edit', $data);
    }

    public function update(Request $request, string $id_karier)
    {
        $validate = $request->validate([
            'tanggal' => 'required|max:255',
            'kampus' => 'required|max:100',
            'prodi' => 'required|max:100',
            'keterangan' => 'required|max:100',
            'id_siswa' => 'required',
        ]);

        $karier = Karier::findOrFail($id_karier);

        $karier->update([
            'tanggal' => $validate['tanggal'],
            'kampus' => $validate['kampus'],
            'prodi' => $validate['prodi'],
            'keterangan' => $validate['keterangan'],
            'id_siswa' => $validate['id_siswa'],
        ]);

        $notificaton = array(
            'message' => 'Data BK-Karier berhasil diperbaharui',
            'alert-type' => 'success'
        );

        return redirect()->route('karier.index')->with($notificaton);
    }

    public function destroy(string $id_karier)
    {
        $karier = Karier::findOrFail($id_karier);

        $karier->delete();

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

        return redirect()->route('karier.index')->with($notificaton);
    }

    public function print()
    {
        $karier = Karier::all();

        $pdf = Pdf::loadView('karier.print', compact('karier'))
            ->setPaper('A4', 'landscape');

        return $pdf->stream('Data Karier Siswa.pdf');
    }
}
