<?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 [
            [['staffcode','staffname'], 'required'],		
            [['staffcode','staffname'], 'string'],			
			//[['staffcode'], 'unique', 'message' => '{attribute} Already Exists'],

            [['staffcode'], 'unique', 'targetAttribute' => ['staffcode'], 'message' => '{attribute} Already Exists'],
						
			[['staffcode'], 'string', 'max' =>6],			
			//[['staffcode'], 'string', 'length' => 3, 'notEqual' => 'Must be {length} digits'],
			
			//['staffcode', 'staffcodeValidates'],
		   // [['artccode'], 'staffARTCValidates'],
		   
		    [['mobileno'], 'integer', 'message' => 'Must be an integer'],
		   
		   	[['mobileno'], 'string', 'max' => 10],
			[['mobileno'], 'string', 'length' => 10, 'notEqual' => 'Must be {length} digits'],
			[['mobileno'], 'mobnoValidates'],
			
            [['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 [		
            'staffcode' => Yii::t('app', 'Staff Code'),
            'staffname' => Yii::t('app', 'Staff Name'),
            'mobileno' => Yii::t('app', 'Mobile Number'),				
            '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 mobnoValidates() {
		if($this->mobileno != "") :		
		if(!preg_match('/^[0-9]*$/', $this->mobileno)) {
			$this->addError('mobileno', 'Invalid Mobile Number');
		}
		endif;
	}
	
		
	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 getStatusdum()
	 {
		return $this->statusdum[$this->status];
		
	 }
	
	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {		
			
			 $this->staffcode = strtoupper($this->staffcode);
			 $this->staffname = strtoupper($this->staffname);			
			 			 
	        return true;
	    } else {
	        return false;
	    }
	}
}
