<?php

namespace modules\custom\models;

use Yii;

use yii\helpers\ArrayHelper;
use modules\custom\models\Contacts;
use modules\activities\models\Activities;
/**
 * This is the model class For table "Accounts".
 *
 * @property integer $accountid
 * @property string $Account_Name
 * @property string $Address1
 * @property string $Address2
 * @property string $Town
 * @property string $County
 * @property string $Post_Code
 * @property string $Telephone_no
 * @property string $Fax_no
 * @property integer $Contact
 * @property string $Email
 * @property string $status
 * @property Contacts $contact
 * @property Contacts[] $contacts
 */
class Accounts extends \common\models\Base {

    public $dates = ['created_at', 'updated_at', 'deleted_at'];
	public $uniqueidentifiers = ['actid'];
    public $securefields = [];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'accounts';
    }

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        $localrules = [
            [['accountid', 'contact'], 'integer'],
            [['account_name', 'address1', 'address2', 'status', 'email','town', 'county'], 'string', 'max' => 50],
            [['post_code', 'telephone_no', 'fax_no'], 'string', 'max' => 15],
            [['notes'], 'string'],
            ['status', 'default', 'value' => 'Active'],
            ['id', 'default', 'value' => self::Tempid()],
//			[['account_name'], 'unique'],
			// Validators
			[['account_name', 'address1', 'town', 'county', 'post_code', 'telephone_no', 'fax_no', 'email'], 'required'],
            ['accountid', 'default', 'value' => function($model){
                $count = self::find()
                ->select(['accountid'])
                ->orderby(['accountid' => SORT_DESC])
                ->one();
                return $count ? $count->accountid + 1 : 1001;
            }
            ]
        ];
        return array_merge($localrules, parent::rules());
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        $localLabels = [
            'accountid' => 'Account Number',
            'name' => 'Account  Name',
            'address1' => 'Address Line 1',
            'address2' => 'Address Line 2',
            'town' => 'Town',
            'county' => 'County',
            'post_code' => 'Post  Code',
            'telephone_no' => 'Telephone No',
            'fax_no' => 'Fax No',
            'ontact' => 'Contact',
            'email' => 'Email',
            'status' => 'Status',
        ];
        return array_merge($localLabels, parent::attributeLabels());
    }
	
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getContact() {
        return $this->hasOne(Contacts::class, ['_id' => 'contact']);
    }

    public function getContacts() {
        return $this->hasMany(Contacts::class, ['accountid' => 'accountid']);
    }
    
    public function getJobs(){
        return $this->hasMany(Activities::class, ['relatedid' => 'id']);
    }

    public function getJobcount(){
        return $this->jobs !== false ? count($this->jobs) : 0; 
    }

    public function afterSave($a, $changed){
        //$this->data = $this->data;
        //yii::debug(Yii::$app->controller->action->id, 'action');
        //yii::debug(stripos(Yii::$app->controller->action->id, 'import'),'action');
        return parent::afterSave($a, $changed);
    }
 
    
        public static function getActive(){
        return ArrayHelper::map(
                self::find()
                ->where(['status' => 'Active'])
                ->orderby(['account_name' => SORT_ASC])
                ->distinct()
                ->all(), 
                'id', 
                'account_name'
            );
        }
        
    public static function Active()
    {
        return ArrayHelper::map(
            self::find()
                ->select(['status', 'account_name', 'accountid'])
                ->where(['status' => 'Active'])
                ->orderby(['account_name' => SORT_ASC])
                ->distinct()
                ->all(), 
                'accountid', 
                'account_name'   
        );
    }
        

}