<?php

namespace frontend\models;

use Yii;
use common\models\Customer;
use common\models\Card;
use common\components\Helper;
use common\models\Ticket;

/**
 * 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 CustomerUpdate extends \common\models\Customer
{
	
	public $cardNumber;
	public $partnerCardNumber;
	
	public $password_plain;
	public $password_repeat;
	
	public $replacementCardNumber;
	public $replacementCard;
	public $originalCard;
	
	
	
    /**
     * @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' ],
        		[['replacementCardNumber'], 'filter', 'filter' => function($value){return Helper::fixAsciiChars($value);}],
        		[['replacementCardNumber'], 'validateReplacementCard' ],
        		
        		[['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/frontend','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/frontend','E-mail or phone number required!')
				],
        		
        		[['zip'], 'string', 'max' => 8],
        		
        		[['city'], 'string', 'max' => 30],
        		[['photo_data'] ,'safe'],
        		[['birth_place'] ,'safe'],
        		[['mother_name'] ,'safe'],
        		[['warn_mail_ticket_expire_enabled','newsletter'],'integer']
        ];
    }

    
    public function validateCustomerCard($a,$p){
//     	 Customer::find()->andWhere(  [$this->cardNumber )
    }
    public function validatePartnerCard($a,$p){
//     	 Customer::find()->andWhere(  [$this->cardNumber )
    }
    public function validateReplacementCard($a,$p){
 		$query = Card::find();
 		Card::addCardNumberCondition($query, $this->replacementCardNumber);
 		$rcard = $query->one();
 		
 		if ( !isset($rcard) ){
 			\Yii::info("csere kártya nem található");
 			$this->addError($a,"Csere kártya nem található");
 			return;
 		}
 		\Yii::info("csere kártya megtalálva");
 		$customer = $rcard->customer;
 		
 		if ( isset($customer) ){
 			\Yii::info("A csere kártyát már valaki használja!");
 			$this->addError($a ,"A csere kártya már használatban van");
 			return;
 		}
 		\Yii::info("A csere kártyát megfelel!");
 		$this->replacementCard = $rcard;
 		
    }
    
    public function beforeSave($insert){
    	parent::beforeSave($insert);
    	if ( isset($this->replacementCard ) ){
    		$this->originalCard = $this->card;
    		$this->id_customer_card = $this->replacementCard->id_card;
    	}
    	return true;
    }
    
    public function  afterSave($insert, $changedAttributes){
    	if ( isset($this->replacementCard)){
    		Ticket::updateAll( ['id_card'  => $this->replacementCard->id_card ], ['id_card' => $this->originalCard->id_card] );
    	}
    	if ( !$insert ){
    			Card::updateCardFlagTicket($this->id_customer_card);
    	}
    }

    
}
