<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Contract;
use yii\db\Query;

/**
 * ContractSearch represents the model behind the search form about `common\models\Contract`.
 */
class ContractSearch extends Contract
{
	
	public $customer_name;
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_contract',   'id_customer', 'status', 'flag', 'part_paid', 'part_count', 'part_required', 'id_ticket_type'], 'integer'],
            [['customer_name' ], '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 = new Query();
        
        $query->select([
        		'contract.id_contract as contract_id_contract',
        		'user.username as user_name',
        		'customer.id_customer as customer_id_customer',
        		'customer.name as customer_name',
        		'contract.status as contract_status',
        		'contract.flag as contract_flag',
        		'contract.part_required as contract_part_required',
        		'contract.part_paid as contract_part_paid',
        		'contract.part_count as contract_part_count',
        		'contract.created_at as contract_created_at',
        		'contract.expired_at as contract_expired_at',
        		'contract.started_at as contract_started_at',
        		
        ]);
        $query->from('contract');
        $query->innerJoin('user' ,'user.id = contract.id_user');
        $query->innerJoin('customer' ,'customer.id_customer = contract.id_customer');
        

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        	'sort' =>[
        		'defaultOrder' =>[
        				'contract_id_contract' => SORT_ASC
        		],
        		'attributes' =>[
        			'contract_id_contract' => [
        					'asc' => ['contract.id_contract' => SORT_ASC ],
        					'desc' => ['contract.id_contract' => SORT_DESC],
        			],
        			'user_name' => [
        					'asc' => ['user.username' => SORT_ASC ],
        					'desc' => ['user.username' => SORT_DESC],
        			],
        			'customer_id_customer' => [
        					'asc' => ['customer.id_customer' => SORT_ASC ],
        					'desc' => ['customer.id_customer' => SORT_DESC],
        			],
        			'customer_name' => [
        					'asc' => ['customer.name' => SORT_ASC ],
        					'desc' => ['customer.name' => SORT_DESC],
        			],
        			'contract_status' => [
        					'asc' => ['contract.status' => SORT_ASC ],
        					'desc' => ['contract.status' => SORT_DESC],
        			],
        			'contract_flag' => [
        					'asc' => ['contract.flag' => SORT_ASC ],
        					'desc' => ['contract.flag' => SORT_DESC],
        			],
        			'contract_part_required' => [
        					'asc' => ['contract.part_required' => SORT_ASC ],
        					'desc' => ['contract.part_required' => SORT_DESC],
        			],
        			'contract_part_paid' => [
        					'asc' => ['contract.part_paid' => SORT_ASC ],
        					'desc' => ['contract.part_paid' => SORT_DESC],
        			],
        			'contract_part_count' => [
        					'asc' => ['contract.part_count' => SORT_ASC ],
        					'desc' => ['contract.part_count' => SORT_DESC],
        			],
        			'contract_created_at' => [
        					'asc' => ['contract.created_at' => SORT_ASC ],
        					'desc' => ['contract.created_at' => SORT_DESC],
        			],
        			'contract_expired_at' => [
        					'asc' => ['contract.expired_at' => SORT_ASC ],
        					'desc' => ['contract.expired_at' => SORT_DESC],
        			],
        			'contract_started_at' => [
        					'asc' => ['contract.started_at' => SORT_ASC ],
        					'desc' => ['contract.started_at' => 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([
            'contract.id_contract' => $this->id_contract,
            'contract.id_user' => $this->id_user,
            'contract.id_customer' => $this->id_customer,
            'contract.status' => $this->status,
            'contract.flag' => $this->flag,
            'contract.part_paid' => $this->part_paid,
            'contract.part_count' => $this->part_count,
            'contract.part_required' => $this->part_required,
            'contract.expired_at' => $this->expired_at,
            'contract.created_at' => $this->created_at,
            'contract.updated_at' => $this->updated_at,
            'contract.id_ticket_type' => $this->id_ticket_type,
        ]);
        
        $query->andFilterWhere(['like', 'customer.name', $this->customer_name]);
        
        return $dataProvider;
    }
}
