<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\models\Screening;


/**
 * This is the model class for table "{{%preartincenmaster}}".
 *
 * @property integer $preart_inc_id
 * @property string $preart_inc_survey
 * @property string $preart_inc_part_id
 * @property integer $preart_inc_visitdate
 * @property integer $preart_inc_q1
 * @property integer $preart_inc_q2
 * @property integer $preart_inc_q3
 * @property string $preart_inc_q4
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 * @property integer $record_date
 */
class Preartincenmaster extends \yii\db\ActiveRecord
{
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%preartincenmaster}}';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['preart_inc_id'], 'required', 'on' => 'generate', 'message' => 'Select At-least one row'],
			[['preart_inc_survey', 'preart_inc_part_id'], 'required'],
			[['preart_inc_visitdate', 'preart_inc_q1', 'preart_inc_q2', 'preart_inc_q3', 'create_time', 'create_user', 'update_time', 'update_user', 'record_date'], 'integer'],
			[['preart_inc_survey', 'preart_inc_part_id'], 'string', 'max' => 11],
			[['preart_inc_q4'], 'string', 'max' => 50],
			[['status'], 'string', 'max' => 1],
			[['status'], 'default', 'value' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
		];
	}
	
	public function getScrdate()
	{		
	  $model = Screening::find()->where(["mem_scrn_part_id" => $this->preart_inc_part_id])->orderBy(['record_date' => SORT_ASC])->one();
	   if ($model !== null && $model->record_date !== null) {
	   if(!is_integer($model->record_date))
		$date = Converter::toStore($model->record_date);
      else
		$date = $model->record_date;
	
	   return $date;	
		
	 }
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'preart_inc_id' => Yii::t('app', 'ID'),
			'preart_inc_survey' => Yii::t('app', 'Survey'),
			'preart_inc_part_id' => Yii::t('app', 'ICC Client ID'),
			'preart_inc_visitdate' => Yii::t('app', 'Visit Date'),
			'preart_inc_q1' => Yii::t('app', 'Payment Status'),
			'preart_inc_q2' => Yii::t('app', 'Amount'),
			'preart_inc_q3' => Yii::t('app', 'Paid Date'),
			'preart_inc_q4' => Yii::t('app', 'Voucher Number'),
			'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'),
			'record_date' => Yii::t('app', 'Record Date'),
		];
	}
}
