<?php

namespace app\controllers;

use app\models\base\NewsTopic;
use app\models\base\Story;
use app\models\base\Tags;
use app\models\News;
use yii\sphinx\Query;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

class SearchController extends Controller
{

    public function actionIndex($topic = NULL,$q=NULL, $index = NULL)
    {
        $topic = NewsTopic::findOne(['url'=>$topic]);
        if(!$topic instanceof NewsTopic){
            $topic = new \stdClass();
            $topic->id = NULL;
        }
        $tag = \Yii::$app->request->get('tag',NULL);
		$story = \Yii::$app->request->get('story',NULL);
        if( !is_null($tag) ){
			$tag = $tag*1;
			$news_query = \app\models\News::find()->alias('n')->joinWith('tags t')->andFilterWhere(['t.id'=>$tag])->andWhere(['n.active'=>'Y'])->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])->groupBy('n.id');
			//return $this->render("index",["topic"=>$topic, "news_query"=>$news_query]);
			//echo $news_query->createCommand()->rawSql;
		}else if( !is_null($story) ){
			$story = $story*1;
			$news_query = \app\models\News::find()->alias('n')->joinWith('story t')->andFilterWhere(['t.id'=>$story])->andWhere(['n.active'=>'Y'])->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])->groupBy('n.id');
			//echo $news_query->createCommand()->rawSql;
		}else{
			$news_query = \app\models\News::find()->alias('n')->joinWith('topics t')->andFilterWhere(['t.id'=>$topic->id])->andWhere(['n.active'=>'Y'])->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])->groupBy('n.id');
		}
        $dateRange = \Yii::$app->request->get('daterange',NULL);
//		if( trim($q) == '' && !(!is_null($dateRange) && $dateRange!="") )  return $this->redirect('/news');
        if(!is_null($dateRange) && $dateRange!=""){
            $dateRange = explode(" — ",$dateRange);
            $dateRange[0] = date("Y-m-d 00:00:01",strtotime($dateRange[0]));
            $dateRange[1] = isset($dateRange[1])?date("Y-m-d 23:59:59",strtotime($dateRange[1])):date("Y-m-d 23:59:59",strtotime($dateRange[0]));
            $news_query->andWhere(["BETWEEN","dt_pub",$dateRange[0],$dateRange[1]]);
        }else{
			if( (is_null($q) || trim($q) == '') && (is_null($tag) && is_null($story)) ){
				//$news_query = \app\models\News::find()->alias('n')->andWhere(['n.id'=>-1]);
				if(\Yii::$app->request->get("page",0)>500) {
					\Yii::$app->request->setQueryParams(["page"=>500]);
				}
				$news_query = \app\models\front\News::findFilter();
			}
		}
        if(!is_null($q) && trim($q) != '' && is_null($tag) ){
            switch ($index){
                case 'video':
                    $index_name = 'amic_news_t137';
                    break;
                default:
                    $index_name = 'amic_news';
            }
            $news_query->andWhere([
                "n.id"=>(new Query())
                    ->from($index_name)
                    ->select(['id'])
                    ->addOptions(['max_matches' => 1000])
                    ->addOptions(['field_weights'=> ['title' => 10, 'body' => 8, 'lead'=>5] ])
                    ->match($q)
                    ->limit(1000)
                    ->column()
            ]);
        }
        return $this->render("index",["topic"=>$topic, "news_query"=>$news_query]);
    }
}
