<?php

namespace backend\models;

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

/**
 * CardSearch represents the model behind the search form about `common\models\Card`.
 */
class CardSearch extends Card
{
	
	public $searchCustomerName;
	public $searchKeyNumber;
	public $usage;
	public $key_assigned;
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_card', 'status', 'type','usage'], 'integer'],
            [[ 'searchCustomerName', 'number','rfid_key', 'created_at', 'updated_at','searchKeyNumber','key_assigned'], '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 = Card::find();

        $query = new Query();
        
        $query->select([
        		'card.id_card as card_id_card',
        		'card.number as card_number',
        		'card.rfid_key as card_rfid_key',
        		'card.status as card_status', 
        		'card.type as card_type',
        		'customer.name as customer_name' ,
        		'customer.id_customer as customer_id_customer',
        		'key.number as key_number',
        		'card.flag as card_flag',
        		
        		
        ]);
        $query->from(Card::tableName());
        $query->leftJoin(Customer::tableName(), 'customer.id_customer_card = card.id_card');
        $query->leftJoin("card_key_assignment", 'card.id_card = card_key_assignment.id_card');
        $query->leftJoin(Key::tableName(), 'key.id_key = card_key_assignment.id_key');
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        	'sort' =>[
        		'defaultOrder' => [
        				'card_number' => SORT_ASC
        		],
       			'attributes' => [
   					'card_number' => [
   						'asc' => ['card.number' => SORT_ASC ],
    					'desc' => ['card.number' => SORT_DESC],
    				],
   					'card_rfid_key' => [
   						'asc' => ['card.rfid_key' => SORT_ASC ],
    					'desc' => ['card.rfid_key' => SORT_DESC],
    				],
   					'card_status' => [
   						'asc' => ['card.status' => SORT_ASC ],
    					'desc' => ['card.status' => SORT_DESC],
    				],
   					'card_type' => [
   						'asc' => ['card.type' => SORT_ASC ],
    					'desc' => ['card.type' => SORT_DESC],
    				],
   					'customer_name' => [
   						'asc' => ['customer.name' => SORT_ASC ],
    					'desc' => ['customer.name' => SORT_DESC],
    				],
   					'customer_id_customer' => [
   						'asc' => ['customer.id_customer' => SORT_ASC ],
    					'desc' => ['customer.id_customer' => SORT_DESC],
    				],
   					'key_number' => [
   						'asc' => ['key.number' => SORT_ASC ],
    					'desc' => ['key.number' => SORT_DESC],
    				],
   					'card_flag' => [
   						'asc' => ['card.flag' => SORT_ASC ],
    					'desc' => ['card.flag' => SORT_DESC],
    				],
    			]
    		]
        ]);
        

        $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->andFilterWhere([
            'card.status' => $this->status,
            'card.type' => $this->type,
        ]);

        $query->andFilterWhere(['like', 'card.number', $this->number]);
        $query->andFilterWhere(['like', 'card.rfid_key', $this->rfid_key]);
        $query->andFilterWhere(['like', 'customer.name', $this->searchCustomerName]);
        
        if ( $this->usage == '1') {
        	$query->andWhere('customer.id_customer is not null');
        }else  if ( $this->usage == '2') {
        	$query->andWhere('customer.id_customer is  null');
        }
        if ( $this->key_assigned == '1') {
        	$query->andWhere('key.id_key is not null');
        }else  if ( $this->key_assigned == '2') {
        	$query->andWhere('key.id_key is  null');
        }
        
        Key::addKeyConditionOptional($query, $this->searchKeyNumber);

        return $dataProvider;
    }
    
    public function attributeLabels(){
    	$result = parent::attributeLabels();
    	$result +=[
    		'searchCustomerName' => Yii::t('common/card','Customer')
    	];
    	return $result;
    }
    
}
