<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Customer;
use common\models\Card;

/**
 * CustomerSearch represents the model behind the search form about `common\models\Customer`.
 */
class CustomerSearch extends Customer
{
	
	public $cardNumber;
	
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        		[['cardNumber', 'name', 'email'] ,'safe']
//             [['id_customer', 'id_customer_card', 'id_user', 'id_partner_card', 'id_proposer', 'sex'], 'integer'],
//             [['name', 'email', 'password', 'phone', 'date_stundent_card_expire', 'birthdate', 'image', 'description', 'tax_number', 'country', 'zip', 'city', 'address', 'created_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Customer::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $dataProvider->sort ->attributes['customerCardNumber'] =[
        		'asc' => ['card.number' => SORT_ASC ],
        		'desc' => ['card.number' => SORT_DESC ],
        ];
        $dataProvider->sort->defaultOrder = [
        		 'name' => SORT_ASC, 
        ];
        
        
 
        
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->leftJoin(Card::tableName(), " customer.id_customer_card = card.id_card" );
        
 

        $query->andFilterWhere(['like', 'customer.name', $this->name])
            ->andFilterWhere(['like', 'customer.email', $this->email])
            ->andFilterWhere(['like', 'card.number', $this->cardNumber])
            ->andFilterWhere(['like', 'address', $this->address]);

        return $dataProvider;
    }
    
    public function attributeLabels( ) {
    	$labels = parent::attributeLabels();
    	return $labels;
    }
}
