<?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 TicketInstallmentRequestSearchDownloadGiro extends TicketInstallmentRequest
{
	public $start;
	public $end;
	public $timestampStart;
	public $timestampEnd;
	
	public $customer_name;
	public $id_ticket_type;
	
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_ticket_installment_request', 'id_ticket', 'id_customer',  'status' ,'id_ticket_type'], 'integer'],
            [['customer_name'  ], 'safe'],
        	[[ 'start', ], 'date'  , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd'  ],
        	[[ 'end' , ], 'date'    , 'timestampAttribute' => 'timestampEnd' ,'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.request_processed_at as request_processed_at',//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
        ]);
        $query->from("ticket_installment_request");
        $query->innerJoin("customer","customer.id_customer = ticket_installment_request.id_customer");
        $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->andWhere(['ticket_installment_request.status' => TicketInstallmentRequest::$STATUS_MARKED_TO_SEND]);
        
        $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,
//         ]);
//         $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]);
        
        $dataProvider = new ArrayDataProvider([
        		      'allModels' => $query->all(),
//         		      'sort' => [
//         				         'attributes' => ['id', 'username', 'email'],
//         				      ],
//         		      'pagination' => [
//         				          'pageSize' => 10,
//         				     ],
        		  ]);

        return $dataProvider;
    }
}
