<?php
    
    namespace frontend\controllers;
    
    use common\models\event\Event;
    use common\models\event\Tag;
    use yii\data\ActiveDataProvider;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\helpers\VarDumper as  d;
    
    /**
     * Class BlogController
     *
     * @package frontend\controllers
     */
    class EventController extends Controller
    {
        public function actionIndex($q = '')
        {
            $tags = Tag::find()
                       ->with(
                           [
                               'language',
                           ]
                       )
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
    
    
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => Event::find()
                                           ->orderBy(
                                               [
                                                   'created_at' => SORT_DESC,
                                               ]
                                           )
                                           ->joinWith('language')
                                           ->where([ 'event.status' => true ])
                                           ->andFilterWhere(
                                               [
                                                   'ilike',
                                                   'event_lang.title',
                                                   $q,
                                               ]
                                           )
                                           ->distinct(),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
       
            return $this->render(
                'index',
                [
                    'tags'         => $tags,
                    'dataProvider' => $dataProvider,
                ]
            );
        }
    
        public function actionView($id)
        {
            $model = $this->findModel($id);
    
            $tags = Tag::find()
                       ->with([ 'language' ])
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
    
            return $this->render(
                'view',
                [
                    'tags'  => $tags,
                    'model' => $model,
                ]
            );
        }
        
    
        public function actionTag($id)
        {
            $tags = Tag::find()
                       ->with(
                           [
                               'language',
                           ]
                       )
                       ->orderBy([ 'sort' => SORT_ASC ])
                       ->all();
            
            $model = Tag::find()
                        ->where(
                            [
                                'id' => $id,
                            ]
                        )
                        ->with(
                            [
                                'events',
                            ]
                        )
                        ->one();
    
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $model->getEvents()
                                          ->with(
                                              [
                                                  'language',
                                              ]
                                          )
                        ->where(['event.status' => true])
                                          ->orderBy(
                                              [
                                                  'created_at' => SORT_DESC,
                                              ]
                                          ),
                    'pagination' => [
                        'pageSize' => 3,
                    ],
                ]
            );
    
            return $this->render(
                'tag',
                [
                    'tags'         => $tags,
                    'dataProvider' => $dataProvider,
                    'model'        => $model,
                ]
            );
        }
    
        /**
         * @param $id
         *
         * @return Event
         * @throws \yii\web\NotFoundHttpException
         */
        protected function findModel($id)
        {
            /**
             * @var Event | null $model
             */
            $model = Event::find()
                          ->where([ 'id' => $id ])
                          ->with(
                                [
                                    'language',
                                    'tags.language',
                                ]
                            )
                          ->andWhere([ 'status' => true ])
                          ->one();
    
            if (empty($model)) {
                throw new NotFoundHttpException(\Yii::t('app', 'Article not found'));
            } else {
                return $model;
            }
        }
    }