<?php
namespace App\Http\Controllers;
use App\Models\Employee; use App\Models\LeaveRequest; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\View\View;
class LeaveRequestController extends Controller
{
 public function index(): View { return view('leaves.index',['leaves'=>LeaveRequest::with('employee')->latest()->paginate(12)]); }
 public function create(): View { return view('leaves.create',['employees'=>Employee::where('status','active')->orderBy('full_name')->get()]); }
 public function store(Request $request): RedirectResponse { $data=$request->validate(['employee_id'=>'required|exists:employees,id','type'=>'required|in:annual,sick,permission,other','start_date'=>'required|date','end_date'=>'required|date|after_or_equal:start_date','reason'=>'required','attachment'=>'nullable|file|mimes:pdf,jpg,jpeg,png|max:5120']); if($request->hasFile('attachment'))$data['attachment']=$request->file('attachment')->store('leave-attachments','public'); LeaveRequest::create($data); return redirect()->route('leaves.index')->with('success','Pengajuan berhasil dikirim.'); }
	public function update(Request $request, LeaveRequest $leaveRequest): RedirectResponse { abort_unless(auth()->user()?->role === 'admin', 403, 'Hanya admin yang dapat memproses pengajuan.'); $leaveRequest->update($request->validate(['status'=>'required|in:approved,rejected,pending','review_note'=>'nullable'])); return back()->with('success','Status pengajuan diperbarui.'); }
 public function destroy(LeaveRequest $leaveRequest): RedirectResponse { $leaveRequest->delete(); return back()->with('success','Pengajuan dihapus.'); }
}
