<?php

namespace App\Models;

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

class Anggota extends Model
{
    use HasFactory;

    
    protected $table = 'anggota';

   
    protected $fillable = [
        'nama',
        'email',
        'alamat',
        'no_hp',
        'user_id',
        'status',
        'nip_nim',
         'foto',  
    ];

    
    public function borrowTransactions()
    {
        return $this->hasMany(BorrowTransaction::class, 'anggota_id');
    }

    
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    
    public function getFormattedNameAttribute()
    {
        return ucwords(strtolower($this->nama));
    }

    
    public function getFormattedNipNimAttribute()
    {
        return $this->nip_nim ? strtoupper($this->nip_nim) : null;
    }

    
    public function hasActiveBorrow()
    {
        return $this->borrowTransactions()
                    ->whereIn('status', ['dipinjam', 'menunggu', 'disetujui'])
                    ->exists();
    }
}
