<?php

namespace common\modules\school\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\modules\school\models\Lessons;

/**
 * SearchLessons represents the model behind the search form about `common\modules\school\models\Lessons`.
 */
class SearchLessons extends Lessons 
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'course_id', 'number'], 'integer'],
            [['video_id', 'title'], '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)
    {
        if ($params){
            $params = array_filter($params);
            $query = Lessons::findByCondition($params);
        }
        else {
           $query = Lessons::find(); 
        }
        $this->load($params);
        /*if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }*/
        $query->andFilterWhere([
            'id' => $this->id,
            'course_id' => $this->course_id,
            'number' => $this->number,
        ]);

        $query->andFilterWhere(['like', 'video_id', $this->video_id])
            ->andFilterWhere(['like', 'title', $this->title]);
        $query->orderBy(['id' => SORT_ASC]);
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination'=>array(
            'pageSize'=>Lessons::PAGE_SIZE ),
        ]);

        return $dataProvider;
    }
}
