<?php


namespace mobile\modules\live\controllers;


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



class HomeController extends BaseController
{

    public $enableCsrfValidation = false;

    //楼盘直播评论---增加
    public function actionAddliveremark()
    {
        $input = Yii::$app->request->post();

    }

    public function actionGetlive()
    {
        $input = Yii::$app->request->get();
        if (!empty($input['city'])) {
            $cmodel = new CategoryCity();
            $input['city'] = array_column($cmodel->GetAllSon($input['city']), 'id');

        }
        $lmodel = new Live();
        $data = $lmodel->showList($input);
//        p($data);die;

        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 Help::JsonCode(Help::SUCCESS, '成功', $data);
        }
        return Help::JsonCode(Help::ERROR, '暂无数据');
    }

    public function actionDetail()
    {   
    
        //获取模块内容
        $module = Yii::$app->hostserver->module;

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

        $model = new Live();
//        $htmodel = new HouseType();
        $res = $model::findOne($input['id']);
        if (empty($res)) $this->goBack('/live/');

        //直播状态
        $state = Time::getLiveState($res['video_url'], $res['time']);

        $typeGroup = array_column($res->houseInfo->houseTypesGroup, 'houseTypeName');

//       去户型分组第一条数据的内容
//        $housetype = $htmodel::find()->andWhere(['hid'=>$res->hid,'type_id'=>$typeGroup[0]['id'],'del'=>1,'state'=>1])->asArray()->all();
//        if(!empty($housetype)){
//            array_walk($housetype,function(&$val){
//                $val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housetypes'] . $val['img'] . Yii::$app->params['pfgwatermark'];
//            });
//        }
        //推送直播--随机同一区域其他楼盘直播
        $pushLive = $model->getLiveBySameArea($res->city, $res->id);

//        //记录浏览记录
//        $server = new \common\api\HttpServer();
//        $record = new LiveHttprecord();
//        $record->Add($ip = Yii::$app->request->userIP, $vid = $input['id'], $url = $server->GetCurUrl());

        return $this->render($this->action->id, [
            'module' => $module['module'],
            'Data' => $res,
            'pushLive' => $pushLive,
            'info' => $res->houseInfo->housedetail->info,
            'typeGroup' => $typeGroup,
            'state' => $state
//            'housetype'=>$housetype,
        ]);
    }

    public function actionRequesthousetype()
    {
        //获取模块内容
        $module = Yii::$app->hostserver->module;
        $input = Yii::$app->request->get();
        $htmodel = new HouseType();

        //       去户型分组第一条数据的内容
        $housetype = $htmodel::find()->andWhere(['hid' => $input['id'], 'type_id' => $input['value'], 'del' => 1, 'state' => 1])->asArray()->all();
        if (!empty($housetype)) {
            array_walk($housetype, function (&$val) {
                $val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housetypesch'] . $val['img'] . Yii::$app->params['pfgwatermark'];
            });
        }
        if (!empty($housetype)) return Help::JsonCode(Help::SUCCESS, '成功', $housetype);
        return Help::JsonCode(Help::ERROR, '暂无数据');
    }

    public function actionSendcode()
    {
        $input = Yii::$app->request->post();
        $input['purpose'] = '视频播放验证';
        $smsServer = new \mobile\server\SendCode();

        return $smsServer->easySms($input);
    }

    public function actionCheckcode()
    {
        $input = Yii::$app->request->post();

        $session = Yii::$app->session;
        $skey = 'live_' . $input['mobile'];
        if (empty($session[$skey])) {
            return Help::JsonCode(Help::ERROR, '请输入正确手机号');
        }
        //验证码有效期5分钟
        if ($session[$skey]['expire_time'] < time()) {
            return Help::JsonCode(Help::ERROR, '验证码已失效');
        }
        if ($input['code'] != $session[$skey]['code']) {
            return Help::JsonCode(Help::ERROR, '验证码不正确');
        }

        Yii::$app->session->set('adopt', $input['mobile']);
        if (!empty(Yii::$app->session['adopt']) && !empty(Yii::$app->session[$skey])) {
            $record = Sendcoderecord::find()->andWhere(['mobilephone' => $input['mobile'], 'code' => $session[$skey]['code']])->one();
            $record->status = 2;
            $record->save();
        }

        return Help::JsonCode(Help::SUCCESS, '验证通过');//验证通过继续播放视频
    }

}