<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\base\Converter;

/**
 * This is the model class for table "{{%ostmaster}}".
 *
 * @property integer $ost_master_id
 * @property string $ost_master_survey
 * @property string $ost_master_part_id
 * @property string $ost_master_q1
 * @property integer $ost_master_q2
 * @property integer $ost_master_q3
 * @property integer $ost_master_q4
 * @property integer $ost_master_q5
 * @property integer $ost_master_q6
 * @property string $ost_master_q7
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Ostmaster extends \yii\db\ActiveRecord
{
	public $oldenddate;
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%ostmaster}}';
	}

	/**
	 * @inheritdoc
	 */
	public function behaviors()
	{
		return [
			[
				'class' => TimestampBehavior::className(),
				'createdAtAttribute' => 'create_time',
				'updatedAtAttribute' => 'update_time',
				'value' => date('U'),
			]
		];
	}

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['ost_master_survey', 'ost_master_loc','ost_master_part_id', 'ost_master_q1', 'ost_master_q2', 'ost_master_q4', 'ost_master_q5', 'ost_master_q6', 'ost_master_q7'], 'required', 'message' => 'Cannot be blank.'],
			[['ost_master_q4', 'ost_master_q5', 'ost_master_q6', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],
			[['ost_master_q7'], 'number', 'message' => 'Must be a Numeric'],
			[['ost_master_survey', 'ost_master_part_id'], 'string', 'max' => 11],
			[['ost_master_q1'], 'string', 'max' => 25],
			// ['ost_master_q1', 'unique', 'message' => 'OST ID Already Exists'],
			// ['ost_master_q1', 'unique', 'targetAttribute' => 'ost_master_part_id', 'message' => 'OST ID Already Exists'],
			// [['ost_master_part_id', 'ost_master_q1'], 'unique', 'targetAttribute' => ['ost_master_part_id', 'ost_master_q1'], 'message' => 'The combination of Ost Master Part ID and Ost Master Q1 has already been taken.'],
			[['ost_master_q2', 'ost_master_q3', 'oldenddate'], 'safe'],
			// [['ost_master_q4', 'ost_master_q5', 'ost_master_q6'], 'string', 'max' => 3],
			[['status'], 'string', 'max' => 1],
			// ['ost_master_q3', 'compare', 'compareAttribute' => 'ost_master_q2', 'operator' => '>=', 'message' => 'OST End Date cannot be lesser than OST Start Date'],
			// ['ost_master_q2', 'compare', 'compareAttribute' => 'oldenddate', 'operator' => '>=', 'message' => 'OST Start Date cannot be lesser than Previous OST End Date'],
			['ost_master_q1', 'ostid'],
			['ost_master_q2', 'svalidateDates'],
			['ost_master_q3', 'validateDates'],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
		];
	}

	public function ostid() {
		if($this->ost_master_q1 != "") {
			$Model = parent::find()->where(["<>", "ost_master_part_id", $this->ost_master_part_id])->andWhere(["ost_master_q1" => $this->ost_master_q1])->one();
			if($Model !== null && count($Model) != 0)
				$this->addError('ost_master_q1', 'OST ID Already Exists');
		}
	}

	public function svalidateDates() {
		if(strtotime($this->ost_master_q2) <= strtotime($this->oldenddate)) {
			$this->addError('ost_master_q2', 'OST Start Date cannot be lesser than OST Previous End Date');
		}
	}

	public function validateDates() {
		if(strtotime($this->ost_master_q3) <= strtotime($this->ost_master_q2)) {
			$this->addError('ost_master_q3', 'OST End Date cannot be lesser than OST Start Date');
		}
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'ost_master_id' => Yii::t('app', 'Ost Master ID'),
			'ost_master_survey' => Yii::t('app', 'Survey'),
			'ost_master_loc' => Yii::t('app', 'Location'),
			'ost_master_part_id' => Yii::t('app', 'ICC Client ID'),
			'ost_master_q1' => Yii::t('app', '1. OST ID'),
			'ost_master_q2' => Yii::t('app', '2. OST Start Date'),
			'ost_master_q3' => Yii::t('app', '3. OST End Date'),
			'ost_master_q4' => Yii::t('app', '4. Quantity of 2mg pills'),
			'ost_master_q5' => Yii::t('app', '5. Quantity of 0.4mg pills'),
			'ost_master_q6' => Yii::t('app', '6. Quantity of 0.2mg pills'),
			'ost_master_q7' => Yii::t('app', '7. Total daily dose, mg'),
			'status' => Yii::t('app', 'Status'),
			'create_time' => Yii::t('app', 'Create Time'),
			'create_user' => Yii::t('app', 'Create User'),
			'update_time' => Yii::t('app', 'Update Time'),
			'update_user' => Yii::t('app', 'Update User'),
		];
	}

	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {
			$this->ost_master_q2 = Converter::toStore($this->ost_master_q2);
			if($this->ost_master_q3 != "")
				$this->ost_master_q3 = Converter::toStore($this->ost_master_q3);
			return true;
		}
	}

	public function afterFind()
	{
		parent::afterFind();
		if(Yii::$app->controller->action->id != "view")
			$this->ost_master_q2 = Converter::toDisplay($this->ost_master_q2);
			if($this->ost_master_q3 != 0)
				$this->ost_master_q3 = Converter::toDisplay($this->ost_master_q3);
	}
}
