<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Presensi;
use Carbon\Carbon;

class IzinController extends Controller
{
    public function index(Request $request)
    {
        $query = Presensi::with('user')
            ->whereIn('status', ['Izin', 'Sakit', 'Disetujui', 'Ditolak'])
            ->orderBy('created_at', 'desc');

        if ($request->has('date')) {
            $date = Carbon::parse($request->date);
            $query->whereDate('created_at', $date);
        }

        $izins = $query->get();

        return view('admin.izin', compact('izins'));
    }

    public function destroy($id)
    {
        $izin = Presensi::findOrFail($id);
        $izin->delete();

        return redirect()->route('admin.izin')
            ->with('success', 'Data izin berhasil dihapus');
    }

    public function approve($id)
    {
        $izin = Presensi::findOrFail($id);
        $izin->status = 'Disetujui';
        $izin->save();

        return redirect()->route('admin.izin')
            ->with('success', 'Izin berhasil disetujui');
    }

    public function reject($id)
    {
        $izin = Presensi::findOrFail($id);
        $izin->status = 'Ditolak';
        $izin->save();

        return redirect()->route('admin.izin')
            ->with('success', 'Izin berhasil ditolak');
    }
} 