<?php

namespace frontend\models;

use Yii;
use yii\base\Model;
use common\models\Transfer;
use common\models\Account;
use common\components\TransferPayout;

/**
 * ContactForm is the model behind the contact form.
 */
class CustomerCartForm extends Model
{

	public $items = []; 
	public $transfers;
	public $payment_method;
	public $money = 0;
	public $selected = [];
	public $customer;
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        		['selected', 'each', 'rule' => ['integer']],
        		[['money' ,'payment_method'],'integer'],
        		[['payment_method'],'validatePaymentMethod'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
        ];
    }

    public function validatePaymentMethod( $attribute, $params ){
    	if ( !empty($this->payment_method)){
//     		echo $this->payment_method;
    		$arr  = Transfer::paymentMethods();
    		if ( !array_key_exists($this->payment_method, $arr) ){
    			 $this->addError($attribute, "Érvénytelen fizetési mód");
    		}
    	}
    }
    
    
    public function payout(){
    	$valid = $this->validate();
    	
    	if ( !$valid ){
    		return false;
    	}
    	
    	if ( isset($this->selected) && count($this->selected) > 0 ){
    		
    		$connection = \Yii::$app->db;
    		$transaction = $connection->beginTransaction();
    		try {
    			$tp = new TransferPayout( [
    					'idUser' => \Yii::$app->user->id,
    					'idTransfers' => $this->selected,
    					'idAccount' => Account::readDefault (),
    					'cartType' => 'customer',
    					'overridePaymentMethod' => $this->payment_method,
    					'idCustomer' => $this->customer->id_customer
    			] );
    		
    			$tp->payout ();
    		
    			$transaction->commit ();
    			\Yii::$app->session->setFlash ( 'success', 'A vásárló kosár kiválasztott tranzakciói ki lettek kifizetve!');
    			return true;
    			 
    		} catch ( Exception $e ) {
    			$transaction->rollback ();
    			Yii::error ( "faled to save :" . $e->getMessage () );
    		} catch ( \Exception $e ) {
    			$transaction->rollback ();
    			Yii::error ( "faled to save :" . $e->getMessage () );
    		}
    		return false;
    		
    	}else{
	    	\Yii::$app->session->setFlash('danger', 'Nem választott ki terméket');
    		return false;
    	}
    }
    
    public function changePaymentMethod($item){
    	if ( !empty($this->payment_method)){
    		$item->payment_method = $this->payment_method;
    	}
    }
    
    public function run(){
    	$this->readTransfers();
    }
    
    public function readTransfers( ) {
    	$this->transfers = $this->loadTransfers();
    }

    public function loadTransfers($id_tranfer_array = null){
    	$query = Transfer::find();
    	$query->innerJoin("shopping_cart", "shopping_cart.id_transfer = transfer.id_transfer");
//     	$query->andWhere(["transfer.id_user" => \Yii::$app->user->id]);
    	$query->andWhere(["shopping_cart.id_customer" => $this->customer->id_customer]);
    	if (isset($id_tranfer_array)){
    		$query->andWhere(["in", "transfer.id_transfer" , $id_tranfer_array  ]);
    	}
    	return  $query->all();
    }
    
}
