<?php

namespace app\models;

use Yii;
use yii\base\Model;
use app\base\Converter;
use app\models\Users;

/**
 * Followupreport is the model behind the contact form.
 */
class Followupreport extends Model
{
    public $frmdate, $todate, $staff, $rep,$district;
	
	public $report= [1 => 'Block-wise',2 => 'District-wise'];

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['frmdate', 'todate'], 'required'],
			[['staff', 'rep', 'district'], 'safe'],
            [['todate'], 'dateValidate'],
			
			 [['rep'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {	
               return ($model->getUserrole() ==1);			 
			 }],
        ];
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'frmdate' => 'From Date',            
            'todate' => 'To Date',  
			'district' => 'District',  
            'staff' => 'Staff', 
            'rep' => 'Report', 
			
        ];
    }
	
	public function getUserrole()
	{
	    $sloc=Yii::$app->user->identity->signedin_loc;
		$suser=Yii::$app->user->identity->users_name;
		$Usermodel = Users::find()->where(['users_name' => $suser])->one();	   
		
        if ($Usermodel !== null) {		
         return $Usermodel->user_role;			
        } 
	}

    public function dateValidate($attribute, $params) {
        if($this->frmdate != "" && $this->todate != "") {
            $this->frmdate = Converter::toStore($this->frmdate);
            $this->todate = Converter::toStore($this->todate);
            if($this->todate < $this->frmdate) {
                $this->frmdate = Converter::toDisplay($this->frmdate);
                $this->todate = Converter::toDisplay($this->todate);
                $this->addError($attribute, "To Date cannot be prior to From Date");
            } else {
                $this->frmdate = Converter::toDisplay($this->frmdate);
                $this->todate = Converter::toDisplay($this->todate);
            }
        }
    }
}
