<?php

namespace common\models;

use Yii;
use common\models\BaseFitnessActiveRecord;

/**
 * This is the model class for table "customer".
 *
 * @property integer $id_customer
 * @property integer $id_customer_card
 * @property integer $id_user
 * @property integer $id_partner_card
 * @property integer $id_proposer
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $phone
 * @property integer $sex
 * @property string $date_stundent_card_expire
 * @property string $birthdate
 * @property string $image
 * @property string $description
 * @property string $tax_number
 * @property string $country
 * @property string $zip
 * @property string $city
 * @property string $address
 * @property string $created_at
 * @property string $updated_at
 */
class Customer extends BaseFitnessActiveRecord
{
	
	const STATUS_DELETED = 0;
	const STATUS_ACTIVE = 10;
	
	const SEX_UNKNOWN = 0;
	const SEX_MAN = 10;
	const SEX_WOMAN = 20;

	
	public static $ENABLED = 1;
	
	public $photo_data;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'customer';
    }
    

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_customer_card', 'id_user', 'id_partner_card', 'id_proposer', 'sex'], 'integer'],
            [['date_stundent_card_expire', 'birthdate', 'created_at', 'updated_at'], 'safe'],
            [['name'], 'string', 'max' => 128],
            [['email', 'image', 'description', 'address'], 'string', 'max' => 255],
            [['password'], 'string', 'max' => 32],
            [['phone', 'tax_number', 'country'], 'string', 'max' => 20],
            [['zip'], 'string', 'max' => 8],
            [['city'], 'string', 'max' => 30]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_customer' => Yii::t('common/customer', 'Id Customer'),
            'id_customer_card' => Yii::t('common/customer', 'Id Customer Card'),
            'id_user' => Yii::t('common/customer', 'Id User'),
            'id_partner_card' => Yii::t('common/customer', 'Id Partner Card'),
            'id_proposer' => Yii::t('common/customer', 'Id Proposer'),
            'name' => Yii::t('common/customer', 'Name'),
            'email' => Yii::t('common/customer', 'Email'),
            'password' => Yii::t('common/customer', 'Password'),
            'phone' => Yii::t('common/customer', 'Phone'),
            'sex' => Yii::t('common/customer', 'Sex'),
            'date_stundent_card_expire' => Yii::t('common/customer', 'Date Stundent Card Expire'),
            'birthdate' => Yii::t('common/customer', 'Birthdate'),
            'image' => Yii::t('common/customer', 'Image'),
            'description' => Yii::t('common/customer', 'Description'),
            'tax_number' => Yii::t('common/customer', 'Tax Number'),
            'country' => Yii::t('common/customer', 'Country'),
            'zip' => Yii::t('common/customer', 'Zip'),
            'city' => Yii::t('common/customer', 'City'),
            'address' => Yii::t('common/customer', 'Address'),
            'created_at' => Yii::t('common/customer', 'Created At'),
            'updated_at' => Yii::t('common/customer', 'Updated At'),
            'customerCardNumber' => Yii::t('common/customer', 'Card number'),
            'cardNumber' => Yii::t('common/customer', 'Card number'),
            'mother_name' => Yii::t('common/customer', 'Anyja neve'),
            'birth_place' => Yii::t('common/customer', 'Születési hely'),
        ];
    }
    
    
    
    static function statuses() {
    	return [
    			self::STATUS_ACTIVE => Yii::t('common/account', 'Active'),
    			self::STATUS_DELETED => Yii::t('common/account', 'Inactive'),
    	];
    }
    
    public function getStatusHuman(){
    	$result = null;
    	$s = self::statuses($this->status);
    	if ( array_key_exists($this->status, $s)){
    		$result = $s[$this->status];
    	}
    	return $result;
    }
    
    static function sexes() {
    	return [
    			self::SEX_UNKNOWN => Yii::t('common/customer', 'Unknown sex'),
    			self::SEX_MAN => Yii::t('common/customer', 'Man'),
    			self::SEX_WOMAN => Yii::t('common/customer', 'Woman'),
    	];
    }
    
    public function getSexHuman(){
    	$result = null;
    	$s = self::sexes( );
    	if ( array_key_exists($this->sex, $s)){
    		$result = $s[$this->sex];
    	}
    	return $result;
    }
    
    public function isInactive(){
    	return $this->status == self::STATUS_DELETED;
    }
    
    
    public function getCard(){
    	return $this->hasOne ( Card::className (), [
    			'id_card' => 'id_customer_card'
    	] );
    }
    public function getUser(){
    	return $this->hasOne ( User::className (), [
    			'id' => 'id_user'
    	] );
    }
    public function getImage1(){
    	return $this->hasOne ( Image::className (), [
    			'id_image' => 'id_image'
    	] );
    }
    
    public function getCustomerCardNumber(){
    	$result = null;
    	$card = $this->card;
    	if ( $card != null) {
    		$result = $card->number;
    	}
    	return $result;
    }
    public function getUsername(){
    	$result = null;
    	$user = $this->user;
    	if ( $user != null) {
    		$result = $user->username;
    	}
    	return $result;
    }
    
    public function getFullAddress(){
    	$zip = $this->zip;
    	$city = $this->city;
    	$address = $this->address;
    	
    	$result = $zip . " " .$city . ", ". $address;
    	return $result;
    	
    }
    
    public function afterSave($insert, $changedAttributes){
    		if ( !$insert ){
    			Card::updateCardFlagTicket($this->id_customer_card);
    		}
    }
    
    
    
}
