<?php

namespace app\models;

use Yii;
use app\behaviours\ActiveRecordLogableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%labdeptmaster}}".
 *
 * @property integer $staff_id
 * @property string $dept_code
 * @property string $occu_name
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Staff extends \yii\db\ActiveRecord
{    
     public $statusdum = [1 => "Y", 0 => "N", ""=>""];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'staff_master';
    }
	
	
    /**
     * @inheritdoc
     */
	public function behaviors()
	{
		return [
			
			[
				'class' => TimestampBehavior::className(),
				'createdAtAttribute' => 'create_time',
				'updatedAtAttribute' => 'update_time',
				'value' => date('U'),
			],
		];
	}

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['artccode','district','staffcode','staffname'], 'required'],		
            [['artccode','district','staffcode','staffname'], 'string'],			
			//[['staffcode'], 'unique', 'message' => '{attribute} Already Exists'],

            [['staffcode'], 'unique', 'targetAttribute' => ['staffcode', 'artccode', 'district'], 'message' => '{attribute} Already Exists'],
						
			[['staffcode'], 'string', 'max' =>6],			
			 [['staffcode'], 'string', 'length' => 3, 'notEqual' => 'Must be {length} digits'],
			
			//['staffcode', 'staffcodeValidates'],
			[['artccode'], 'staffARTCValidates'],
			
            [['create_time', 'create_user', 'update_time', 'update_user'], 'integer'],			
            [['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [		    
			'artccode' => Yii::t('app', 'ARTC Code'),
            'district' => Yii::t('app', 'District'),
            'staffcode' => Yii::t('app', 'Staff Code'),
            'staffname' => Yii::t('app', 'Staff Name'),			
            'status' => Yii::t('app', 'Status'),
            'create_time' => Yii::t('app', 'Created Time'),
            'create_user' => Yii::t('app', 'Created By'),
            'update_time' => Yii::t('app', 'Last Updated Time'),
            'update_user' => Yii::t('app', 'Last Updated By'),
        ];
    }
	
		
	public function staffcodeValidates() {
		$this->staffcode = strtoupper($this->staffcode);
		if(!preg_match('/^[A-Za-z]{1}[0-9]*$/', $this->staffcode)) {
			$this->addError('staffcode', 'Staff Code is not Valid. Code should be like A1234');
		}
	}
	
	public function staffARTCValidates($attribute, $params) {
	  if($this->artccode != "" && $this->district != "" ) : 		
		$Master = Artcentre::find()->where(["status" => 1])->andWhere(["artccode" => $this->artccode])->andWhere(["district" => $this->district ])->one();
		if($Master == null)
		{
			$this->addError($attribute, 'Wrong ARTC Chosen. This ARTC is not in Chosen District');
		}
	  endif;	
	}	
	
		
	 public function getStatusdum()
	 {
		return $this->statusdum[$this->status];
		
	 }
	 
	 public function getArtmaster($id)
	{
		$Gmodel = \app\models\Artcentre::find()->where(["artccode" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->artccode;
		 else
			return "-";
	}
	
	public function getDistrictmaster($id)
	{
		$Gmodel = \app\models\District::find()->where(["district" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->district;
		 else
			return "-";
	}
	
	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {		
			
			 $this->staffcode = strtoupper($this->staffcode);
			 $this->staffname = strtoupper($this->staffname);			
			 			 
	        return true;
	    } else {
	        return false;
	    }
	}
}
