<?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\love\model\ActiveApplyModel;
use app\love\model\ActiveModel;
use app\love\model\UserFriendModel;
use app\love\model\UserInviteModel;

class ActiveController extends LoveBaseController
{
    private $_age = [[0, 100], [18, 25], [25, 30], [30, 40], [40, 50], [50, 100]];
    private $_high = [[0, 250], [150, 160], [161, 170], [171, 180], [180, 250]];
    private $_weight = [[0, 150], [40, 50], [51, 60], [61, 70], [71, 80], [81, 150]];

    /**
     * 活动列表
     */
    public function index()
    {
        $list = ActiveModel::where('status', 1)->order('create_time desc')->select();
        foreach ($list as $v) {
            $v['main_image'] = cmf_get_image_preview_url($v['main_image']);
            $v['start_time'] = date('m-d', $v['start_time']);
            $v['end_time']   = date('m-d', $v['end_time']);
        }
        $this->assign('list', $list);

        //未读消息数
        $unread_num = UserFriendModel::where('user_id', cmf_get_current_user_id())->sum('unread_num');
        $invite_num = UserInviteModel::where('to_id', cmf_get_current_user_id())
            ->where('status', 1)
            ->count();
        $this->assign('unread_num', $unread_num + $invite_num);

        return $this->fetch();
    }

    /**
     * 详情
     */
    public function detail()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('活动不存在或已结束');
        }
        $active = ActiveModel::get($id);
        if (empty($active)) {
            $this->error('活动不存在或已结束');
        }
        $this->assign('id', $id);

        $active                = ActiveModel::get($id);
        $active['time_status'] = 0;
        if ($active['start_time'] > time()) {
            $active['time_status'] = 1;
        }
        if ($active['end_time'] < time()) {
            $active['time_status'] = 2;
        }
        $active['start_time'] = date('m-d', $active['start_time']);
        $active['end_time']   = date('m-d', $active['end_time']);
        $active['main_image'] = cmf_get_image_preview_url($active['main_image']);
        $this->assign('info', $active);

        //状态
        $apply = ActiveApplyModel::where([
            ['user_id', '=', $this->user['id']],
            ['active_id', '=', $id],
        ])->find();
        $this->assign('apply', $apply ?: 'false');

        if ($apply && $apply['check_status'] == 2) {
            $this->assign('share_image_url', $active['main_image']);
            $this->assign('share_title', $active['title']);
            $this->assign('share_link', url('love/login/index', '', true, true) . '?active_id=' . $id);
            $this->assign('share_desc', $active['describe']);
        }

        return $this->fetch();
    }

    /**
     * 参加活动
     */
    public function apply()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('活动不存在或已结束');
        }
        $active = ActiveModel::get($id);
        if (empty($active)) {
            $this->error('活动不存在或已结束');
        }

        $check = ActiveApplyModel::get(['active_id'=>$id,'user_id'=>$this->user['id']]);
        if (!empty($check)) {
            $this->error('您已报名！');
        }

        //参加
        ActiveApplyModel::create([
            'active_id'   => $id,
            'user_id'     => $this->user['id'],
            'create_time' => time(),
        ]);
        $this->success('报名成功');
    }

    /**
     * 取消参加活动
     */
    public function cancel()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('活动不存在或已结束');
        }
        $active = ActiveModel::get($id);
        if (empty($active)) {
            $this->error('活动不存在或已结束');
        }

        $check = ActiveApplyModel::get(['active_id'=>$id,'user_id'=>$this->user['id']]);
        if (empty($check)) {
            $this->error('您未报名，无需取消！');
        }

        $check->delete();
        $this->success('取消成功');
    }

    /**
     * 嘉宾
     */
    public function people()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('活动不存在或已结束');
        }
        $active = ActiveModel::get($id);
        if (empty($active)) {
            $this->error('活动不存在或已结束');
        }
        $this->assign('id', $id);

        $where = [
            ['a.active_id', '=', $id],
            ['a.check_status', '=', 2],
            ['u.check_status', '=', 2],
            ['u.is_complete', '=', 1],
            ['u.id', '<>', cmf_get_current_user_id()],
        ];
        $list  = ActiveApplyModel::alias('a')
            ->field('u.*,a.site_status')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where($where)
            ->order('a.site_status asc')
            ->limit(6)
            ->select();

        foreach ($list as $v) {
            $v['age'] = Fun::getAgeByBirth($v['birthday']);
        }
        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 列表
     */
    public function peopleList()
    {
        $param = $this->request->post();

        $where = [
            ['a.active_id', '=', $param['active_id']],
            ['a.check_status', '=', 2],
            ['u.check_status', '=', 2],
            ['u.is_complete', '=', 1],
            ['u.id', '<>', cmf_get_current_user_id()],
        ];
        if (!empty($param['age'])) {
            $age        = $this->_age[$param['age']];
            $start_time = strtotime("-{$age[1]} year");
            $end_time   = strtotime("-{$age[0]} year");
            $where[]    = ['u.birthday', 'between', [$start_time, $end_time]];
        }
        if (!empty($param['site_status'])) {
            $where[] = ['a.site_status', '=', $param['site_status']];
        }
        if (!empty($param['sex'])) {
            $where[] = ['u.sex', '=', $param['sex']];
        }
        if (!empty($param['high'])) {
            $high    = $this->_high[$param['high']];
            $where[] = ['u.high', 'between', [$high[0], $high[1]]];
        }
        if (!empty($param['weight'])) {
            $weight  = $this->_weight[$param['weight']];
            $where[] = ['u.weight', 'between', [$weight[0], $weight[1]]];
        }
        if (!empty($param['education'])) {
            $where[] = ['u.education', '=', $param['education']];
        }
        if (!empty($param['id'])) {
            $where[] = ['a.user_no', '=', $param['id']];
        }

        $list = ActiveApplyModel::alias('a')
            ->field('u.*,a.site_status')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where($where)
            ->order('a.site_status asc')
            ->limit(6)
            ->page($param['page'])
            ->select();
        foreach ($list as $v) {
            $v['age'] = Fun::getAgeByBirth($v['birthday']);
        }
        $this->result($list, 1);
    }

    /**
     * 排行榜
     */
    public function rank()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('活动不存在或已结束');
        }
        $active = ActiveModel::get($id);
        if (empty($active)) {
            $this->error('活动不存在或已结束');
        }
        $this->assign('id', $id);

        //列表
        $where = [
            ['a.active_id', '=', $id],
            ['a.check_status', '=', 2],
            ['u.check_status', '=', 2],
            ['u.is_complete', '=', 1],
        ];
        $list1 = ActiveApplyModel::alias('a')
            ->field('u.*,a.site_status')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where($where)
            ->where('sex', 2)
            ->order('u.invite_num desc')
            ->limit(10)
            ->select();
        foreach ($list1 as $v) {
            $v['age'] = Fun::getAgeByBirth($v['birthday']);
        }
        $this->assign('list1', $list1);

        $list2 = ActiveApplyModel::alias('a')
            ->field('u.*,a.site_status')
            ->leftJoin('cmf_user u', 'a.user_id = u.id')
            ->where($where)
            ->where('sex', 1)
            ->order('u.invite_num desc')
            ->limit(10)
            ->select();
        foreach ($list2 as $v) {
            $v['age'] = Fun::getAgeByBirth($v['birthday']);
        }
        $this->assign('list2', $list2);

        return $this->fetch();
    }
}
