<?php

namespace modules\custom\models;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use frontend\models\Users;

/**
 * This is the model class For table "contacts".
 *
 * @property integer $Id
 * @property string $Firstname
 * @property string $lastname
 * @property string $homephone
 * @property string $mobileno
 * @property string $email
 * @property string $efirstname
 * @property string $elastname
 * @property string $ephone
 * @property string $emobile
 * @property string $erelationship
 * @property string $address1
 * @property string $address2
 * @property string $town
 * @property string $county
 * @property string $postcode
 * @property string $drivinglno
 * @property string $drivinglexpiry
 * @property string $drivingcategories
 * @property string $sentidno
 * @property string $sentidexpiry
 * @property string $Created
 * @property string $eemail
 * @property string $isengineer
 * @property integer $accountid
 *
 * @property Accounts[] $accounts
 * @property Accounts $account
 */
class Contacts extends \common\models\Base {

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = [];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'contacts';
    }
    
    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['accountid', '_id'], 'integer'],
            [['firstname', 'lastname', 'homephone', 'mobileno', 'efirstname', 'elastname', 'ephone', 'emobile', 'erelationship', 'address1', 'address2', 'town', 'county', 'postcode', 'drivinglno', 'drivinglexpiry', 'drivingcategories', 'sentidno', 'sentidexpiry', 'contacttype'], 'string', 'max' => 45],
			[['email'], 'string', 'max' => 255],
	//		[['']]
			//Defaults
			[['deleted'], 'boolean'],
			[['created_at', 'updated_at', 'deleted_at'], 'safe'],
			[['created_by', 'updated_by','id'],'safe'],
			//Redundant
            [['created_by2', 'updated_by2'], 'integer'],
            ['id', 'default', 'value' => self::Tempid()],
            ['language', 'default', 'value' => 'en'],
            ['status', 'default', 'value' => 'Active'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'firstname' => 'First Name',
            'lastname' => 'Last Name',
            'homephone' => 'Home Phone',
            'mobileno' => 'Mobile Number',
            'email' => 'E-mail Address',
            'efirstname' => 'Emergency First Name',
            'elastname' => 'Emergency Last Name',
            'ephone' => 'Emergency Phone Number',
            'emobile' => 'Emergency Mobile Number',
            'erelationship' => 'Relationship',
            'address1' => 'Address 1',
            'address2' => 'Address 2',
            'town' => 'Town',
            'county' => 'County',
            'postcode' => 'Postcode',
            'drivinglno' => 'Driving License No',
            'drivinglexpiry' => 'Driving License Expiry',
            'drivingcategories' => 'Driving Categories',
            'sentidno' => 'Sentinal ID Number',
            'sentidexpiry' => 'Sentinal ID Expiry Date',
            'Created' => 'Created',
            'eemail' => 'Emergency E-Mail Address',
            'isengineer' => 'IS Engineer',
            'accountid' => 'Account Number',
            'notes' => 'Notes',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAccounts() {
        return $this->hasMany(Accounts::class, ['contact' => '_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAccount() {
        return $this->hasOne(Accounts::class, ['accountid' => 'accountid']);
    }
    public function getFullname() {
		return 
		strpos(str_replace(' ', '_', trim($this->firstname)), '_') !== false && strpos(str_replace(' ', '_', trim($this->lastname)), '_') !== false
		? $this->firstname : $this->firstname.' '.$this->lastname ;
		
	}

  	public static function getcontacts($cat_id) {
        return Contacts::find()->where(['accountid'=>$cat_id, 'contacttype' => "Primary Contact"])->select(['_id','firstname AS name'])->asArray()->all();
	}
    
    public static function getcontactswas($cat_id) {
		$data = Contacts::find()->where(['accountid'=>$cat_id])->select(['_id','firstname AS name'])->asArray()->all();
        return $data;
	} 
	public function beforeSave($insert) {
		if (Yii::$app->name !== 'app-console' ) {
		if ($this->_id ) {
			$count = SELF::find()->where(['=','_id',$this->_id ])->orderby('_id DESC')->Count();
			if ($count > 1) {
				$this->_id = SELF::find()->orderby('_id DESC')->One()['_id'] + +1;
			}
			}
		if (!$this->_id ) {$this->_id = SELF::find()->orderby('_id DESC')->One()['_id'] + +1;}
    }
        
	return parent::beforeSave($insert);
	}
}
   