<?php

namespace App\Models\ValueSet;

use App\Models\BaseModel;

class ValueSetModel extends BaseModel {
  protected $table      = 'valueset';
  protected $primaryKey = 'VID';
  protected $allowedFields = ['SiteID', 'VSetID', 'VOrder', 'VValue', 'VDesc', 'VCategory', 'CreateDate', 'EndDate']; 
	
	protected $useTimestamps = true;
  protected $createdField = 'CreateDate';
  protected $updatedField = '';
	protected $useSoftDeletes = true;
	protected $deletedField = 'EndDate';

	public function getValueSets($param = null, $VSetID = null) {
    $this->select("valueset.*, valuesetdef.VSName as VCategoryName")
      ->join('valuesetdef', 'valueset.VSetID = valuesetdef.VSetID', 'LEFT');

    if ($VSetID !== null) {
        $this->where('valueset.VSetID', $VSetID);
    }

    if ($param !== null) {
        $this->groupStart()
             ->like('valueset.VValue', $param, 'both')
             ->orLike('valueset.VDesc', $param, 'both')
             ->orLike('valuesetdef.VSName', $param, 'both')
             ->groupEnd();
    }

    return $this->findAll();
  }

	public function getValueSet($VID) {
		$rows = $this->select("valueset.*, valuesetdef.VSName")
			->join('valuesetdef', 'valuesetdef.VSetID = valueset.VSetID', 'LEFT')
			->find($VID);

		return $rows;
	}

  public function getValueSetByValueSetDef($VSetID) {
		$rows = $this->where('VSetID', (int) $VSetID)->findAll();
		return $rows;
	}
  

}
