<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    protected $connection = 'sqlsrv';
    protected $table = 'HR_eID';
    protected $primaryKey = 'EmpId';
    public $incrementing = true;
    public $timestamps = false;

    protected $fillable = [
        'EmpId',
        'staff_code',
        'dept_name',
        'FullNamh',
        'FullNeng',
        'sex',
        'country_name',
        'job_title',
        'sub_city',
        'college',
        'house_number',
        'woreda',
        'photo',
        'sig',
        'firstName',
        'fatheName',
        'grandName',
        'firstamName',
        'fatheramName',
        'grandamName',
        'salary',
        'mobile',
        'birth_date',
        'empDate',
        'empLetter',
        'empStatus',
        'SNunmber',
        'issue_date',
        'expDate',
        'PrintCouont',
        'PrintDate',
        'PrintUser',
        'PrintApproved',
        'PrintRemark',
        'ModifyCount',
        'PrintFlag',
        'CSN',
        'image',
        'JobTitle',
        'Department',
        'SubCity',
        'Nationality',
        'updated',
        'Staff_Type' // Add the new column
    ];

    protected $casts = [
        'salary' => 'decimal:2',
        'birth_date' => 'date',
        'empDate' => 'date',
        'issue_date' => 'date',
        'expDate' => 'date',
        'PrintDate' => 'date',
        'PrintCouont' => 'integer',
        'ModifyCount' => 'integer',
        'PrintFlag' => 'integer',
        'updated' => 'boolean',
    ];

    // Accessor for photo
    public function getPhotoAttribute($value)
    {
        if ($value && !empty($value)) {
            if (is_resource($value)) {
                $value = stream_get_contents($value);
            }
            return 'data:image/jpeg;base64,' . base64_encode($value);
        }
        return null;
    }

    // Accessor for signature
    public function getSigAttribute($value)
    {
        if ($value && !empty($value)) {
            if (is_resource($value)) {
                $value = stream_get_contents($value);
            }
            return 'data:image/jpeg;base64,' . base64_encode($value);
        }
        return null;
    }
}
