<?php

namespace app\models;

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

/**
 * Attendancereport is the model behind the contact form.
 */
class Attendancereport extends Model
{
    public $frmdate, $todate;	

 public $services = ['Client ID','OST ID','TI ID','interviewer','visit','visit_date','visit_in','out_interviewer','visit_out','remarks','status','create_time','create_user','update_time','update_user','record_date'];
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['frmdate', 'todate'], 'required'],
            [['todate'], 'dateValidate'],
        ];
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'frmdate' => 'Beginning Date',            
            'todate' => 'End Date',            
        ];
    }
	
	public function getAttnstartdate()
	{
		$Gmodel = Attandance::find()->orderBy(['record_date' => SORT_ASC])->one();
		if($Gmodel !== null) {	
			 
			 if(!is_integer($Gmodel->record_date))
					$date = Converter::toStore($Gmodel->record_date);
				else
				  $date  = $Gmodel->record_date;		
				return Converter::toDisplay($date);	
		} else
			 $date = strtotime(date("Y/m/d"));
             return Converter::toDisplay($date);				
	}

    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, "End Date cannot be prior to Beginning Date");
            } else {
                $this->frmdate = Converter::toDisplay($this->frmdate);
                $this->todate = Converter::toDisplay($this->todate);
            }
        }
    }
}
