<?php
namespace App\Models\Organization;
use App\Models\BaseModel;

class DepartmentModel extends BaseModel {
  protected $table = 'department';
  protected $primaryKey = 'DepartmentID';
  protected $allowedFields = ['DisciplineID', 'SiteID', 'DepartmentCode', 'DepartmentName', 'CreateDate', 'EndDate']; 
		
  protected $useTimestamps = true;
  protected $createdField = 'CreateDate';
  protected $updatedField = '';
  protected $useSoftDeletes = true;
  protected $deletedField = 'EndDate';
  
  public function getDepartments($filter = []) {
    $this->select('department.*, discipline.DisciplineCode, discipline.DisciplineName, site.SiteCode, site.SiteName')
      ->join('discipline', 'discipline.DisciplineID=department.DisciplineID', 'left')
      ->join('site', 'department.SiteID=site.SiteID', 'left');
      
    if (!empty($filter['DepartmentCode'])) {
      $this->like('department.DepartmentCode', $filter['DepartmentCode'], 'both');
    }
    if (!empty($filter['DepartmentName'])) {
      $this->like('department.DepartmentName', $filter['DepartmentName'], 'both');
    }
    $rows = $this->findAll();

    return $rows;
  }

  public function getDepartment($DepartmentID) {
    $row = $this->select('department.*, discipline.DisciplineCode, discipline.DisciplineName, site.SiteCode, site.SiteName')
      ->join('discipline', 'discipline.DisciplineID=department.DisciplineID', 'left')
      ->join('site', 'site.SiteID=department.SiteID', 'left')
      ->where('department.DepartmentID', $DepartmentID)
      ->first();
    return $row;
  }
}
