<?php

namespace app\models;

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

/**
 * Stireport is the model behind the contact form.
 */
class Contactreport extends Model
{
    public $frmdate, $todate; 
     // public $services = ['Client ID','record_date','contact_q10','contact_q1','contact_q2','contact_q3','contact_q4','contact_q5','contact_q6','contact_q7','contact_q8','contact_q9','record_date'];
	public $services = ['Client ID','Client Nickname','Client Address','Client Addr.Pincode','Client Mobile No','Partner Address','Partner Addr.Pincode ','Name of personal contact','Address of this personal contact','Mobile No for this contact','Nickname','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 getStistartdate()
	{
		$Gmodel = Contact::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);
            }
        }
    }
}
