<?php

namespace backend\models;

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

/**
 * TicketInstallmentRequestSearch represents the model behind the search form about `common\models\TicketInstallmentRequest`.
 */
class TicketInstallmentRequestSearch extends TicketInstallmentRequest
{
	public $start;
	public $end;
	public $timestampStart;
	public $timestampEnd;
	public $processedStart;
	public $processedEnd;
	public $timestampProcessedStart;
	public $timestampProcessedEnd;
	public $sentStart;
	public $sentEnd;
	public $timestampSentStart;
	public $timestampSentEnd;
	
	public $customer_name;
	public $id_ticket_type;
	
	public $id_ugiro;
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'id_contract', 'id_ticket_installment_request', 'id_ticket', 'id_customer',  'status' ,'id_ticket_type','id_ugiro'], 'integer'],
            [['customer_name'  ], 'safe'],
        	[[ 'start', ], 'date'  , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd'  ],
        	[[ 'end' , ], 'date'    , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
        	[[ 'processedStart', ], 'date'  , 'timestampAttribute' => 'timestampProcessedStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd'  ],
        	[[ 'processedEnd' , ], 'date'    , 'timestampAttribute' => 'timestampProcessedEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
        	[[ 'sentStart', ], 'date'  , 'timestampAttribute' => 'timestampSentStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd'  ],
        	[[ 'sentEnd' , ], 'date'    , 'timestampAttribute' => 'timestampSentEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
        ];
    }

    /**
     * @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();
        
        $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');
        }
        
        $query->select([
        'ticket_installment_request.id_ticket_installment_request as request_id_ticket_installment_request', //id
        'ticket_installment_request.request_target_time_at as request_request_target_time_at',//target time
        'ticket_installment_request.money as request_money',//money
        'ticket_installment_request.status as request_status',//status
        'ticket_installment_request.request_sent_at as request_sent_at',//sent_at
        'ticket_installment_request.priority as request_priority',//sent_at
        'ticket_installment_request.number as request_number',//sent_at
        'ticket_installment_request.request_processed_at as request_processed_at',//request_processed_at
        'ticket_installment_request.id_contract as request_id_contract',//request_processed_at
        'customer.id_customer as customer_id_customer',//id_customer
        'customer.name as customer_name',//customer_name
        'ticket_type.name as ticket_type_name',//ticket_type_name
        'ticket.status as ticket_status',//ticket_status
        'ticket.start as ticket_start',//ticket_start
        'ticket.end as ticket_end',//ticket_send
        'ticket.id_ticket as ticket_id_ticket',//id_ticket
        'ugiro_request_assignment.id_ugiro as ugiro_id_ugiro' ,//köteg azonosító
        'card.number as card_number'
        ]);
        $query->from("ticket_installment_request");
        $query->innerJoin("customer","customer.id_customer = ticket_installment_request.id_customer");
        $query->leftJoin("card","card.id_card = customer.id_customer_card");
        $query->leftJoin("ticket","ticket.id_ticket = ticket_installment_request.id_ticket");
        $query->leftJoin("ticket_type","ticket.id_ticket_type = ticket_type.id_ticket_type");
        $query->leftJoin("ugiro_request_assignment","ticket_installment_request.id_ticket_installment_request = ugiro_request_assignment.id_request");

        $query->orderBy(["ticket_installment_request.request_target_time_at" => SORT_ASC]);

        $query->andFilterWhere([
        		'ticket_installment_request.id_ticket_installment_request' => $this->id_ticket_installment_request,
        		'ticket.id_ticket' => $this->id_ticket,
        		'customer.id_customer' => $this->id_customer,
        		'ticket_installment_request.status' => $this->status,
        		'ticket_type.id_ticket_type' => $this->id_ticket_type,
        		'ticket_installment_request.id_contract' => $this->id_contract,
        ]);
        $query->andFilterWhere(['like', 'customer.name', $this->customer_name]);
        //target time
        $query->andFilterWhere(['>=', 'ticket_installment_request.request_target_time_at', $this->timestampStart]);
        $query->andFilterWhere(['<', 'ticket_installment_request.request_target_time_at', $this->timestampEnd]);
        //sent time
        $query->andFilterWhere(['>=', 'ticket_installment_request.request_sent_at', $this->timestampSentStart]);
        $query->andFilterWhere(['<', 'ticket_installment_request.request_sent_at', $this->timestampSentEnd]);
        //processed time
        $query->andFilterWhere(['>=', 'ticket_installment_request.request_processed_at', $this->timestampProcessedStart]);
        $query->andFilterWhere(['<', 'ticket_installment_request.request_processed_at', $this->timestampProcessedEnd]);
        
        if ( isset($this->id_ugiro ) && !empty($this->id_ugiro)){
        	$query->andWhere(["ugiro_request_assignment.id_ugiro" => $this->id_ugiro]);
        }
        
        $dataProvider = new ArrayDataProvider([
        		      'allModels' => $query->all(),
//         		      'sort' => [
//         				         'attributes' => ['id', 'username', 'email'],
//         				      ],
//         		      'pagination' => [
//         				          'pageSize' => 10,
//         				     ],
        		  ]);

        return $dataProvider;
    }
}
