<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Alternatif;
use App\Models\Penilaian;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Exception;

class AlternatifController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){

        if(auth()->user()->role === 'admin') {
            $alternatifs = Alternatif::paginate(10); // Tambahkan pagination untuk efisiensi
        return view('admin.alternatif.index', compact('alternatifs'));
        } else {
           
            return view('user.alternatif.index',);
        }
 
    }

    public function store(Request $request){
        $validated = $request->validate([
            'nama_alternatif' => 'required|string|max:255',
            'usia' => 'required|numeric',
            'pendidikan' => 'required',
            'ipk' => 'required|numeric',
            'jurusan' => 'required',
            'lama_studi' => 'required|numeric',
            'pengalaman' => 'required|numeric',
            'serkom' => 'required',
        ]);
    
        Alternatif::create($validated);
    
        return redirect()->route('alternatif.index')->with('msg', 'Data pelamar berhasil ditambahkan!');

    }


    public function edit($id)
    {
        $data['alternatif'] = Alternatif::findOrFail($id);
        return view('admin.alternatif.edit', $data);
    }


    public function update(Request $request, $id)
{
    $validated = $request->validate([
        'nama_alternatif' => 'required|string|max:255',
        'usia' => 'required|integer|min:18',
        'pendidikan' => 'required|string',
        'ipk' => 'required|numeric|min:0|max:4',
        'jurusan' => 'required|string',
        'lama_studi' => 'required|numeric|min:0|max:10',
        'pengalaman' => 'required|integer|min:0|max:50',
        'serkom' => 'required|boolean',
    ]);

    try {
        $alternatif = Alternatif::findOrFail($id);
        $alternatif->update($validated);
        return back()->with('msg','Berhasil Mengubah Data');
    } catch (Exception $e) {
        \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
        die("Gagal");
    }
}


    public function destroy($id){

        try {

            $alternatif = Alternatif::findOrFail($id);
            $alternatif->delete();
            Penilaian::truncate();

        } catch (Exception $e) {
            \Log::emergency("File:" . $e->getFile(). "Line:" . $e->getLine(). "Message:" . $e->getMessage());
            die("Gagal");
        }

    }

    public function downloadPDF() {
        setlocale(LC_ALL, 'IND');
        $tanggal = Carbon::now()->formatLocalized('%A, %d %B %Y');
        $alternatif = Alternatif::with('penilaian.crips')->get();

        $pdf = Pdf::loadView('admin.alternatif.alternatif-pdf',compact('alternatif','tanggal'));
        $pdf->setPaper('A3', 'potrait');
        return $pdf->stream('alternatif.pdf');
    }
}
