<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\models\Artcentre;
use app\models\Vll;

/**
 * This is the model class for table "{{%locationmapping}}".
 *
 * @property integer $locmap_id
 * @property string $staffcode
 * @property string $loc_mapng_mstr_id
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Stafflocationmapping extends \yii\db\ActiveRecord
{
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return 'stafflocationmapping';
	}
	
	
	/**
	 * @inheritdoc
	 */
	public function behaviors()
	{
		return [
			[
				'class' => TimestampBehavior::className(),
				'createdAtAttribute' => 'create_time',
				'updatedAtAttribute' => 'update_time',
				'value' => date('U'),
			]
		];
	}

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['staffcode', 'artccode'], 'required'],
			[['create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
			[['staffcode', 'artccode'], 'string', 'max' => 50],
			[['status'], 'string', 'max' => 1],
		  //[['artccode'], 'unique', 'targetAttribute' => ['artccode','staffcode' ], 'message' => 'This Staff has already been mapped with this ARTC 
          //     Centre.'],
			//[['artccode'], 'validArtccode'],
			 
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
			[['artcname','staffname','vllname'], 'safe'],
		];
	}
	
 /*
	public function validArtccode($attribute, $params){
    if(!empty($this->artccode) && is_array($this->artccode)){
       $isduplicate = $this->isDuplicate($this->artccode);
       if($isduplicate){
           $this->addError('artccode', 'Artccode must be unique!');
       }
    }

	private function isDuplicate($arr){
		if(count(array_unique($arr)) < count($arr)){
			return true;
		}
		else {
			return false;
		}
	}
  */
	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'locmap_id' => Yii::t('app', 'Loc Mapng ID'),
			'staffcode' => Yii::t('app', 'Staff Code'),
			'artccode' => Yii::t('app', 'ARTC Centre'),
			'artcname'=> Yii::t('app', 'Name of the ART Centre'),
			'staffname' => Yii::t('app', 'Staff Name'),
			'vllname' => Yii::t('app', 'VL Lab Linked'),
			'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 attributes() 
   { 
      return array_merge(parent::attributes(), 
      [ 
       'artcname', 	
	   'staffname', 
	   'vllname',
      ]
     );
   }
	
  
    public function getArtcs()
    {
        return $this->hasOne(\app\models\Artcentre::className(), ['artccode' => 'artccode']);
    }
   
   public function getStaffs()
    {
        return $this->hasOne(\app\models\Staff::className(), ['staffcode' => 'staffcode']);
    }
	
	 public function getVlls()
    {      		
	    $Vmodel = $this->hasOne(Vll ::className(),['vllcode' => 'vllcode'])
       ->viaTable(Artcentre::tableName(),['artccode' => 'artccode'] );
        return $Vmodel;
    }
	
	 public function getArtcmaster($id)
	{
		$Gmodel = \app\models\Artcentre::find()->where(["artccode" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->artccode;
		 else
			return "-";
	}
	
	
	
	public function getArtcname($id)
	{
		$Gmodel = \app\models\Artcentre::find()->where(["artcname" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->artcname;
		 else
			return "-";
	}
	
	 public function getStaffmaster($id)
	{
		$Gmodel = \app\models\Staff::find()->where(["staffcode" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->staffcode;
		 else
			return "-";
	}
	
	public function getStaffname($id)
	{
		$Gmodel = \app\models\Staff::find()->where(["staffname" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->staffname;
		 else
			return "-";
	}
	
	public function getVllname($id)
	{
		$Gmodel = \app\models\Vll::find()->where(["name_vl_lab_linked" => $id])->one();
		if($Gmodel !== null) 
		    return $Gmodel->name_vl_lab_linked;
		 else
			return "-";
	}
}
