<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Absence extends Model
{
    use HasFactory;

    protected $table = 'absences';
    
    protected $fillable = [
        'pers_qrcode',
        'ad_id',
        'start_date',
        'end_date',
        'days_count',
        'absence_type',
        'reason',
        'status',
        'proof_file',
        'hr_notes'
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
    ];

    // Calculate absence days automatically
    public static function calculateAbsenceDays($startDate, $endDate)
    {
        $start = Carbon::parse($startDate);
        $end = Carbon::parse($endDate);
        return $start->diffInDays($end) + 1; // +1 to include start day
    }

    // Relationship with HR admin
    public function hr()
    {
        return $this->belongsTo(Admin::class, 'ad_id');
    }

    // Relationship with person (via QR code)
    public function staff()
    {
        return $this->belongsTo(Staff::class, 'pers_qrcode', 'pers_qrcode');
    }

    // Scopes for different statuses
    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopeRejected($query)
    {
        return $query->where('status', 'rejected');
    }

    // Get absence type label
    public function getAbsenceTypeLabelAttribute()
    {
        return [
            'leave' => 'Congé',
            'sickness' => 'Maladie',
            'training' =>