<?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 $district_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 District extends \yii\db\ActiveRecord
{    
    public $statusdum = [1 => "Y", 0 => "N", ""=>""];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'district_master';
    }
	
	
    /**
     * @inheritdoc
     */
	public function behaviors()
	{
		return [
			
			[
				'class' => TimestampBehavior::className(),
				'createdAtAttribute' => 'create_time',
				'updatedAtAttribute' => 'update_time',
				'value' => date('U'),
			],
		];
	}

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['state','district'], 'required'],
            [['state','district'], 'string'],
			[['district_uid'], 'safe'],	  
            [['state_id'], 'safe'],				
			[['district'], 'unique', 'message' => '{attribute} Already Exists'],			
			[['district_uid'], 'string', 'max' => 3],	
			
			[['district_uid'], 'string', 'length' => 3, 'notEqual' => 'Must be {length} digits', 'when' => function ($model) {
				return $model->district_uid !='' ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#district-district_uid').val() != '' ;
			}"],
			
			[['district_uid'], 'unique', 'message' => '{attribute} Already Exists', 'when' => function ($model) {
				return $model->district_uid !='' ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#district-district_uid').val() != '' ;
			}"],
			
			[['district_uid'], 'uidValidates'],
			[['state'], 'stateuidValidates'],
			
            [['create_time', 'create_user', 'update_time', 'update_user'], 'integer'],			
            [['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
        ];
    }
	
	public function uidValidates() {
		if($this->district_uid != "") :		
		if(!preg_match('/^[a-zA-Z]*$/', $this->district_uid)) {
			$this->addError('district_uid', 'UID Code should be contain Alphabets only');
		}
		endif;
	}
	
	public function stateuidValidates() {		
		if($this->state != "" && $this->district != "" && $this->district_uid != "") : 				
		$Statemaster = State::find()->where(["status" => 1])->andWhere(["state" => $this->state])->andWhere(["<>","state_id",""])->one();
		if($Statemaster == null)
		{
			$this->addError('state', 'State UID should not be blank to this district belong to this State. Please fill up the State UID in the State master first.');
		}
		endif;
	}
		

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
		    'state_id' => Yii::t('app', 'State ID'),
            'state' => Yii::t('app', 'State'),
            'district' => Yii::t('app', 'District Name'),
			'district_uid' => Yii::t('app', 'District ID'),
            '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 getStatusdum()
	 {
		return $this->statusdum[$this->status];
		
	 }
	 
	 public function getStatemaster($id)
	{
		$Gmodel = \app\models\State::find()->where(["state" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->state;
		 else
			return "-";
	}
	
	public function getStatename()
    {
    	return $this->hasOne(\app\models\State::className(), ['state' => 'state']);
    }
	
	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {		
			
			 $this->district_uid = strtoupper($this->district_uid);
			
	        return true;
	    } else {
	        return false;
	    }
	}
}
