<?php
namespace App\Models\Specimen;

use App\Models\BaseModel;

class ContainerDefModel extends BaseModel {
  protected $table      = 'containerdef';
  protected $primaryKey = 'ConDefID';
  protected $allowedFields = ['SiteID', 'ConCode', 'ConName', 'ConDesc', 'Additive', 'ConClass', 'Color', 'CreateDate', 'EndDate']; 
	
	protected $useTimestamps = true;
  protected $createdField = 'CreateDate';
  protected $updatedField = '';
  protected $useSoftDeletes = true;
  protected $deletedField  = 'EndDate';
	

	public function getContainers($filter = []) {
		$builder = $this->select('containerdef.*, vscol.VValue as ColorTxt, vscla.VValue as ConClassTxt, vsadd.VValue as AdditiveTxt')
			->join('valueset vscol', 'vscol.VID=containerdef.Color', 'left')
			->join('valueset vscla', 'vscla.VID=containerdef.ConClass', 'left')
			->join('valueset vsadd', 'vsadd.VID=containerdef.Additive', 'left');

		if (!empty($filter['ConCode'])) {
			$builder->like('containerdef.ConCode', $filter['ConCode'], 'both');
		}
		if (!empty($filter['ConName'])) {
			$builder->like('containerdef.ConName', $filter['ConName'], 'both');
		}

		$rows = $builder->findAll();
		return $rows;
	}

  public function getContainer($ConDefID) {
    $row = $this->select('containerdef.*, vscol.VValue as ColorTxt, vscla.VValue as ConClassTxt, vsadd.VValue as AdditiveTxt')
      ->join('valueset vscol', 'vscol.VID=containerdef.Color', 'left')
      ->join('valueset vscla', 'vscla.VID=containerdef.ConClass', 'left')
      ->join('valueset vsadd', 'vsadd.VID=containerdef.Additive', 'left')
      ->where('ConDefID', $ConDefID)->first();
    return $row;
  }
}