<?php

namespace app\models;

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

/**
 * Servicereport is the model behind the contact form.
 */
class Servicereport extends Model
{
    public $frmdate, $todate;
    public $services = ['Client ID', 'Visit Date', 'Counseling Service','Nurse Service','OST Service'];

    /**
     * @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 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);
            }
        }
    }
}
