<?php

namespace mobile\modules\live\controllers;


use common\Helps\Time;
use common\models\CategoryCity;
use common\models\House;
use common\models\Live;
use mobile\modules\live\base\BaseController;
use Yii;


/**
 * Default controller for the `dinglongwan` module
 */
class DefaultController extends BaseController
{

    /**
     * 直播模块首页
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {

        //获取模块内容
        $module = Yii::$app->hostserver->module;

        $input = Yii::$app->request->get();

        if (!empty($input['city'])) {
            $cmodel = new CategoryCity();
            $input['city'] = array_column($cmodel->GetAllSon($input['city']), 'id');

        }

        $lmodel = new Live();

        //获取所有直播地区
        $res = $lmodel->getListArea();
        if (!empty($res)) {
            foreach ($res as &$val) {
                $val['pid'] = CategoryCity::find()->select(['pid'])->andWhere(['del' => 1, 'id' => $val['city']])->asArray()->one()['pid'];
                if (!empty($val['pid'])) $val['house']['f'] = CategoryCity::find()->select(['id', 'city_name', 'sort'])->andWhere(['del' => 1, 'state' => 1, 'id' => $val['pid']])->asArray()->one();
            }
        }

        $c = array_column($res, 'house');
        $province = array_column($c, 'f', 'province');

        $sortKey = array_column($province, 'sort'); //取出数组中sort的一列,返回一维数组
        array_multisort($sortKey, SORT_DESC, SORT_NUMERIC); //大到小排序,根据sort排序在前

        $data = $lmodel->showList($input);
        if (!empty($data)) {
            foreach ($data as &$val) {
                $val['pid'] = CategoryCity::find()->select(['pid'])->andWhere(['del' => 1, 'id' => $val['city']])->asArray()->one()['pid'];
                if (!empty($val['pid'])) $val['house']['f'] = CategoryCity::find()->select(['id', 'city_name'])->andWhere(['del' => 1, 'id' => $val['pid']])->asArray()->one();
                $val['state'] = Time::getLiveState($val['video_url'], $val['time']);
                $house = House::findOne($val['hid']);
                if (!empty($house['area'])) {
                    $val['swt'] = $house['area'];
                } else {
                    $val['swt'] = $house['city'];
                }
            }
        }

        return $this->render($this->action->id, [
            'module' => $module['module'],
            'Data' => $data,
            'province' => isset($province) ? $province : '',
        ]);
    }


}