<?php

namespace App\Models\Infrastructure;

use App\Models\BaseModel;

class EquipmentListModel extends BaseModel {
    protected $table = 'equipmentlist';
    protected $primaryKey = 'EID';
    protected $allowedFields = [
        'IEID',
        'DepartmentID',
        'InstrumentID',
        'InstrumentName',
        'WorkstationID',
        'isEnable',
        'EquipmentRole',
        'CreateDate',
        'EndDate'
    ];

    protected $useTimestamps = true;
    protected $createdField = 'CreateDate';
    protected $updatedField = '';
    protected $useSoftDeletes = true;
    protected $deletedField = 'EndDate';

    public function getEquipmentLists($filter = []) {
        $builder = $this->builder();
        $builder->select('equipmentlist.*, department.DepartmentName, workstation.WorkstationName')
            ->join('department', 'department.DepartmentID = equipmentlist.DepartmentID', 'left')
            ->join('workstation', 'workstation.WorkstationID = equipmentlist.WorkstationID', 'left');

        if (!empty($filter['IEID'])) {
            $builder->like('equipmentlist.IEID', $filter['IEID'], 'both');
        }
        if (!empty($filter['InstrumentName'])) {
            $builder->like('equipmentlist.InstrumentName', $filter['InstrumentName'], 'both');
        }
        if (!empty($filter['DepartmentID'])) {
            $builder->where('equipmentlist.DepartmentID', $filter['DepartmentID']);
        }
        if (!empty($filter['WorkstationID'])) {
            $builder->where('equipmentlist.WorkstationID', $filter['WorkstationID']);
        }
        if (isset($filter['isEnable'])) {
            $builder->where('equipmentlist.isEnable', $filter['isEnable']);
        }

        $rows = $builder->get()->getResultArray();
        return $rows;
    }

    public function getEquipmentList($EID) {
        $builder = $this->builder();
        $row = $builder->select('equipmentlist.*, department.DepartmentName, workstation.WorkstationName')
            ->join('department', 'department.DepartmentID = equipmentlist.DepartmentID', 'left')
            ->join('workstation', 'workstation.WorkstationID = equipmentlist.WorkstationID', 'left')
            ->where('equipmentlist.EID', $EID)
            ->get()
            ->getRowArray();

        return $row;
    }
}
