<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\love\controller;

use app\common\Fun;
use app\common\Wechat;
use app\love\model\ActiveApplyModel;
use app\love\model\ActiveModel;
use app\love\model\UserMatingModel;
use app\love\model\UserModel;
use app\love\model\UserSelectLogModel;
use cmf\controller\HomeBaseController;
use think\facade\Request;
use think\facade\Session;

class SceneController extends HomeBaseController
{
    public function initialize()
    {
        parent::initialize();

        //分享页面
        $wx_api_sign = Wechat::instance()->getJsSign(Request::url(true));
        $this->assign('domain', $this->request->domain());
        $this->assign('wx_api_sign', json_encode($wx_api_sign));
    }

    /**
     * 签到
     */
    public function wechat()
    {
        $active_id = $this->request->param('active_id', 0);
        if (empty($active_id)) {
            $this->error('您的二维码有误，请联系工作人员', url('/'));
        }
        $active = ActiveModel::get($active_id);
        if (empty($active)) {
            $this->error('您的二维码有误，请联系工作人员', url('/'));
        }
        Session::set('sign_in_id', $active_id);

        $this->redirect('https://www.jucai.gov.cn/api/auth/wechat_auth?url=' . urlencode(url('love/Scene/signIn', '', true, true)));
    }

    /**
     * 签到回调
     */
    public function signIn()
    {
        $param     = $this->request->param();
        $open_id   = $param['openid'];
        $active_id = Session::pull('sign_in_id');

        //获取用户
        $user = UserModel::where('open_id', $open_id)->find();
        if (empty($user)) {
            $this->assign('content', '请先注册');
            $this->assign('btn', '点击注册');
            $this->assign('url', url('love/login/index') . '?id=' . $active_id);
            return $this->fetch();
        }

        //用户状态
        if ($user['is_complete'] == 2) {
            $this->assign('content', '请先完善资料');
            $this->assign('btn', '完善资料');
            if (empty($user['sex'])) {
                $this->assign('url', url('love/register/sex'));
            } else {
                $this->assign('url', url('love/register/intro'));
            }
            return $this->fetch();
        }
        if ($user['user_status'] != 1) {
            $this->redirect(url('love/register/ban'));
        }
        if ($user['check_status'] != 2) {
            $this->assign('content', '请等待后台审核');
            $this->assign('btn', '前往首页');
            $this->assign('url', url('/'));
            return $this->fetch();
        }

        //活动
        $apply = ActiveApplyModel::where([
            ['active_id', '=', $active_id],
            ['user_id', '=', $user['id']],
        ])->find();
        if (empty($apply)) {
            $this->assign('content', '请先报名活动');
            $this->assign('btn', '进入活动');
            $this->assign('url', url('love/active/detail') . '?id=' . $active_id);
            return $this->fetch();
        }
        if ($apply['check_status'] != 2) {
            $this->assign('content', '活动还未审核通过');
            $this->assign('btn', '进入活动');
            $this->assign('url', url('love/active/detail') . '?id=' . $active_id);
            return $this->fetch();
        }

        //签到
        if ($apply->site_status == 2) {
            $apply->site_status = 1;
            $apply->site_time   = time();
            $apply->save();
        }

        //嘉宾编号
        if (empty($apply['user_no'])) {
            $user_no        = ActiveApplyModel::where('active_id', $active_id)->max('user_no');
            $apply->user_no = ++$user_no;
            $apply->save();
        }

        $this->assign('content', '签到成功<br/>您的嘉宾编号是' . $apply['user_no']);
        $this->assign('btn', '进入活动');
        $this->assign('url', url('love/active/detail') . '?id=' . $active_id);

        return $this->fetch();
    }

    /**
     * 现场轮播图
     */
    public function sceneList()
    {
        $id = $this->request->param('id', 0);
        if (empty($id)) {
            return '0';
        }
        $this->assign('id', $id);

        $active = ActiveModel::get($id);
        if (empty($active)) {
            return '0';
        }
        $this->assign('active', $active);

        //获取列表
        $where = [
            ['a.active_id', '=', $id],
            ['a.site_status', '=', 1],
        ];
        $list  = $this->_getList($where);
        $this->assign('list', $list);

        //最后到场时间
        $this->assign('last_time', $list->isEmpty() ? 0 : $list[0]['site_time']);

        return $this->fetch();
    }

    /**
     * 现场轮播图信息
     */
    public function sceneListPost()
    {
        $param = $this->request->param();

        //获取列表
        $where = [
            ['a.active_id', '=', $param['id']],
            ['a.site_status', '=', 1],
            ['a.site_time', '>', $param['time']],
        ];
        $list  = $this->_getList($where);

        $this->success('', '', $list);
    }

    /**
     * 获取列表
     */
    private function _getList($where)
    {
        $list = ActiveApplyModel::alias('a')
            ->field('u.*,a.user_no,a.site_time')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where($where)->order('site_time desc')->select();
        if ($list->isEmpty()) {
            return $list;
        }

        //获取择偶要求
        $age_list = $list->column('id');
        $mating   = UserMatingModel::where('user_id', 'in', $age_list)->column('*', 'user_id');
        foreach ($list as $v) {
            $v['age'] = Fun::getAgeByBirth($v['birthday']);
            $v['mating'] = $mating[$v['id']];
            $v['url']    = urlencode(url('love/userwall/detail', ['id' => $v['id']], true, true));
        }

        return $list;
    }

    /**
     * 选择特效开始
     */
    public function selectStart()
    {
        return $this->fetch();
    }

    /**
     * 选择特效
     */
    public function selectView()
    {
        $info = UserSelectLogModel::with('user1,user2')->where('is_top', 1)->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    /**
     * 帮助中心
     */
    public function helpCenter()
    {
        $this->assign('share_title', '速看 | 泉州人才联谊平台操作手册！');
        $this->assign('share_desc', '点击查看>>>>');
        $this->assign('share_link', $this->request->url(true));
        return $this->fetch();
    }

    /**
     * 随机抽人
     */
    public function randomPeople()
    {
        $id  = $this->request->param('id', 0);
        $man = ActiveApplyModel::alias('a')
            ->field('u.realname,u.main_image,u.username,u.star,a.user_no')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where([
                ['a.active_id', '=', $id],
                ['a.user_no', '>', 0],
                ['u.sex', '=', 1],
            ])->select();
        $this->assign('man', json_encode($man));
        $this->assign('man_count', $man->count() - 1);

        $woman = ActiveApplyModel::alias('a')
            ->field('u.realname,u.main_image,u.username,u.star,a.user_no')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where([
                ['a.active_id', '=', $id],
                ['a.user_no', '>', 0],
                ['u.sex', '=', 2],
            ])->select();
        $this->assign('woman', json_encode($woman));
        $this->assign('woman_count', $woman->count() - 1);

        return $this->fetch();
    }
}
