<?php

namespace mobile\controllers;

use common\models\CategoryCity;
use common\models\House;
use common\models\HouseDetail;
use mobile\base\CommonController;
use common\models\VideoCategory;
use common\models\Video;
use Yii;
use mobile\base\Help;

class VideoController extends CommonController
{

    public $enableCsrfValidation = false;

    public function actionHome()
    {
        return $this->render('home');
    }


    public function actionDetails()
    {
        return $this->render('details');
    }


    /*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#Date:2019-3-28 Author:eit Title:视频页面*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*/
    public function actionVideolist()
    {
        $province = $this->province();
        return $this->render('videolist', ['province' => $province]);
    }


    /**
     * @title ajax获取区域视频
     */
    public function actionGetvideo()
    {
//        $model = new Video();
        $input = Yii::$app->request->get();
        if (isset($input['city']) && !empty($input['city'])) {
            $city = CategoryCity::find()->andWhere(['del' => 1, 'pid' => $input['city']])->select(['id'])->column();
            $city[] = $input['city'];
//            $model->city = $city;
        }
//        $videolist = $model->homeList($input);

        $videolist = House::find();
        $videolist->andWhere(['NOT', ['pfg_house_detail.video_url' => '']]);
        $videolist->andWhere(['pfg_house.del' => 1]);
        $videolist->andWhere(['pfg_house.is_view' => 1]);
        if (!empty($city)) {
            $videolist->andWhere(['pfg_house.city' => $city]);
        }
        $videolist->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.thumb']);
        $videolist->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $data = $videolist->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC])->asArray()->all();

        if (!empty($data)) {
            foreach ($data as &$val) {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
            }
            return Help::JsonCode(Help::SUCCESS, '成功', $data);
        }
    }


    /**
     * 视频搜索接口@2019.8.20
     */
    public function actionSearch()
    {
        $post = Yii::$app->request->post();
//        $list = (new Video())->getText($post);
        $videolist = House::find();
        $videolist->andWhere(['NOT', ['pfg_house_detail.video_url' => '']]);
        $videolist->andWhere(['pfg_house.del' => 1]);
        $videolist->andWhere(['pfg_house.is_view' => 1]);
        if (!empty($post['text'])) {
            $videolist->andWhere(['like', 'pfg_house.name', $post['text']]);
        }
        $videolist->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.thumb']);
        $videolist->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $list = $videolist->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC])->asArray()->all();
        if (!empty($list)) {
            foreach ($list as &$val) {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
            }
            return Help::JsonCode(Help::SUCCESS, '成功', $list);
        }
        return Help::JsonCode(Help::ERROR, '暂无数据');
    }


    /**
     * @title ajax统计视频播放次数
     */
    public function actionPlays()
    {
        $model = new Video();
        $input = Yii::$app->request->get();

        if (is_numeric($input['id'])) {
            $row = $model->FindById($input['id']);
            if (!empty($row)) {
                if (isset($input['thumbs_up'])) {
                    $row->thumbs_up = ++$row->thumbs_up;
                } else {
                    $row->plays = ++$row->plays;
                }
                $row->save(false);
                return true;
            }
        }
    }


    /**
     * @title 视频详情
     */
    public function actionVideodetails()
    {
        $hotplay = $this->HotPlays(Yii::$app->request->get());
        $model = new House();
        $input = Yii::$app->request->get();

        $info = $model::find()
            ->andWhere(['pfg_house.id' => $input['hid'], 'pfg_house.del' => 1, 'pfg_house.is_view' => 1])
            ->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid')
            ->leftJoin('pfg_category_city', 'pfg_house.city = pfg_category_city.id')
            ->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.thumb', 'pfg_house.characteristic', 'pfg_house_detail.video_url',
                'pfg_house_detail.features', 'pfg_category_city.city_name'])
            ->asArray()->one();


//        /***2020.6.18 lyy 根据$input['lt'] 判断是否是获取直播视频***/
//        if (isset($input['lt']) && $input['lt'] == 1) {
//            $live = \common\models\Live::find()
//                ->andWhere(['hid' => $input['hid']])
//                ->asArray()->one();
//            $info['video_url'] = $live['video_url'];
//            $info['title'] = $live['title'];
//            $info['thumb'] = $live['img'];
//        } else {
//            $info['thumb'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['video'] . $info['thumb'];
//        }
//        if (isset($input['hid']) && !empty($input['hid'])) {
//            $infos = $this->getHouseCity($input['hid']);
//            $info['city'] = $infos['city'];
//            $info['characteristic'] = $infos['characteristic'];
//        }
        if (!empty($info)) {
            $info['characteristic'] = $this->Subject(json_decode($info['characteristic']), 2);
            return $this->render('videodetails', ['hotplay' => $hotplay, 'info' => $info]);
        }
        return false;
    }


    /**
     * @title 楼盘详情的区域
     */
    public function getHouseCity($hid)
    {
        $modelHouse = new \common\models\House();
        $houseinfo = $modelHouse->FindById($hid);
        $houseinfo['characteristic'] = $this->Subject(json_decode($houseinfo['characteristic']), 2);
        $modelCity = new \common\models\CategoryCity();
        $cityData = $modelCity->FindById($houseinfo['city']);
        $Data['city'] = $cityData['city_name'];
        $Data['characteristic'] = $houseinfo['characteristic'];
        return $Data;
    }


    /**
     * 获取热播视频
     */
    private function HotPlays($input)
    {
//        $model = new Video();
//        $model->allow = 1;
//        $input['limit'] = 4;
//        $input['page'] = 1;
//        $videolist = $model->homeList($input);
//        if (!empty($videolist)) {
//            foreach ($videolist as &$val) {
//                $val['thumb'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['video'] . $val['thumb'];
//            }
//            return $videolist;
//        }
//        return false;
        $data = House::find()->andWhere(['NOT', ['pfg_house_detail.video_url' => '']])
            ->andWhere(['<>', 'pfg_house.id', $input['hid']])
            ->andWhere(['pfg_house.del' => 1])
            ->andWhere(['pfg_house.is_view' => 1])
            ->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.thumb'])
            ->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid')
            ->orderBy('RAND()')->limit(4)->asArray()->all();
        if (!empty($data)) {
            return $data;
        }
        return '';
    }


    /**
     * @title 获取省份
     */
    private function province()
    {
        $model = House::find()->andWhere(['NOT', ['pfg_house_detail.video_url' => '']])->andWhere(['del' => 1, 'is_view' => 1])
            ->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid')->select(['city'])->column();
        $city = CategoryCity::find()->andWhere(['del' => 1, 'id' => $model])->select(['pid'])->column();
        $upcity = CategoryCity::find()->andWhere(['del' => 1, 'id' => $city])->select(['id', 'city_name'])->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC])->asArray()->all();
        return $upcity;
    }


    //特色主题 超过3个随机返回3个元素
    public function Subject($id, $num = 3)
    {
        $char = new \common\models\Characteristic();
        if (is_array($id) && !empty($id)) {
            $char->id = $id;
            $charMode = array_column($char->getList([], ['id', 'name']), 'name', 'id');
            if ($charMode != null) {
                $count = count($charMode);
                $arr = [];
                if ($count > $num) {
                    $rand = array_rand($charMode, $num);
                    foreach ($rand as $key => $val) {
                        $arr[$key] = $charMode[$val];
                    }
                } else {
                    $arr = $charMode;
                }
                return $arr;
            }
        }
    }
}