<?php

namespace backend\models;

use Yii;
use common\models\Customer;
use common\models\Card;

/**
 * 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
 * @property string $cardNumber
 */
class CustomerCreate extends \common\models\Customer
{
	
	public $cardNumber;
	public $partnerCardNumber;
	
	public $password_plain;
	public $password_repeat;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'customer';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        		[['cardNumber'], 'required' ],
        		[['cardNumber'], 'string', 'max' => 10],
        		[['cardNumber'], 'validateCustomerCard' ],
        		
        		[['partnerCardNumber'], 'string', 'max' => 10],
        		[['partnerCardNumber'], 'validatePartnerCard' ],
        		
        		[['name'], 'required' ],
        		[['name'], 'string', 'max' => 128],

        		[['email'], 'string', 'max' => 255],
        		[['email'], 'email' ],
        		[['email'], 'unique' ],
        		[['email'], 'required', 'when' => function($model) {
        			return  !isset(  $model->email )  || empty($model->phone) ;
        		} ,
        		'whenClient' => "function (attribute, value) {
				    return false;
				}",
        		'message' => Yii::t('customer/backend','E-mail or phone number required!')
        		],
        		
        		
        		[['password_plain','password_repeat'], 'string', 'max' => 32],
        		
        		[['sex'], 'integer'],
        		
        		[[ 'birthdate',  ], 'date'     ],
        		[[ 'date_stundent_card_expire',  ], 'date'     ],
        		
        		[[ 'description', 'address'], 'string', 'max' => 255],
        		
        		[['phone', 'tax_number', 'country'], 'string', 'max' => 20],
        		
        		[['bank_account'], 'string', 'max' => 24],
        		[['bank_name'], 'string', 'max' => 100],
        		
        		[['phone'], 'required', 'when' => function($model) {
        			return   !isset(  $model->email )  || empty(  $model->email ) ;
        		} ,
        		'whenClient' => "function (attribute, value) {
				    return false;
				}",
        			'message' => Yii::t('customer/backend','E-mail or phone number required!')
        		],
        		
        		[['zip'], 'string', 'max' => 8],
        		
        		[['city'], 'string', 'max' => 30]
        ];
    }

    
    public function validateCustomerCard($a,$p){
    	$card =  null;
    	
    	if ( !empty($this->cardNumber)){
    	 	$card = Card::readCard($this->cardNumber,true);
    	}
    	 
    	if ( $card == null ){
    	 	$this->addError($a,Yii::t('common/customer', "Bérlet kártya nem üres vagy hibás kártyaszám"));
    	}else{
    	 	$this->id_customer_card = $card->id_card;
    	 }
    	 
//     	$this->addError($a,Yii::t('common/customer', "Bérlet kártya nem üres vagy hibás kártyaszám"));
    }
    
    public function validatePartnerCard($a,$p){
    	if ( !empty($this->partnerCardNumber) ){
	    	$card = Card::readCard($this->partnerCardNumber,true);
	    	 if ( $card == null ){
	    	 	$this->addError($a,Yii::t('common/customer', "Bérlet kártya nem üres vagy hibás kártyaszám"));
	    	 }else{
	    	 	$this->id_partner_card = $card->id_card;
	    	 }
    	}
    }
    
}
