<?php
namespace App\Models\Specimen;

use App\Models\BaseModel;
use App\Libraries\ValueSet;

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.*');

		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();
    $rows = ValueSet::transformLabels($rows, [
			'Color' => 'container_cap_color',
			'ConClass' => 'container_class',
			'Additive' => 'additive',
		]);
		return $rows;
	}

  public function getContainer($ConDefID) {
    $row = $this->select('containerdef.*')
      ->where('ConDefID', $ConDefID)->first();
    
    if (!$row) return null;
    
    $row = ValueSet::transformLabels([$row], [
      'Color' => 'container_cap_color',
      'ConClass' => 'container_class',
      'Additive' => 'additive',
    ])[0];
    
    return $row;
  }
}
