<?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\portal\controller;

use app\admin\model\SlideItemModel;
use app\common\Excel;
use app\common\Fun;
use app\love\controller\LoveBaseController;
use app\love\model\ActiveApplyModel;
use app\love\model\UserFriendModel;
use app\love\model\UserInviteModel;
use app\love\model\UserMatingModel;
use app\love\model\UserOutlookModel;
use app\love\model\UserSelectLogModel;
use app\portal\model\UserModel;

class IndexController extends LoveBaseController
{
    public function index()
    {
        $user_id = cmf_get_current_user_id();

        //判断三观
        $outlook = UserOutlookModel::where('user_id',$user_id)->find();
        if (empty($outlook)) {
            return redirect('/love/my/outlook');
        }
//        $active = ActiveApplyModel::where('user_id',$user_id)->where('active_id',2)->find();
//        if ($active) {
//            $outlook = UserOutlookModel::where('user_id',$user_id)->find();
//            if (empty($outlook)) {
//                return redirect('/love/my/outlook');
//            }
//        }

        $user    = UserModel::get($user_id);
        $matting = UserMatingModel::where('user_id', $user_id)->find();
        $where   = [
            ['user_type', '=', 2],
            ['check_status', '=', 2],
            ['is_complete', '=', 1],
            ['is_public', '=', 1],
            ['id', '<>', $user_id],
            ['sex', '=', $user['sex'] == 1 ? 2 : 1],
        ];
        //年龄
        $age_start = 0;
        $age_end   = time();
        if (!empty($matting['max_age'])) {
            $age_start = strtotime("-{$matting['max_age']} year");
        }
        if (!empty($matting['min_age'])) {
            $age_end = strtotime("-{$matting['min_age']} year");
        }
        $where[] = ['birthday', 'between', [$age_start, $age_end]];
        //身高
        $max_high = 200;
        if (!empty($matting['max_high'])) {
            $max_high = $matting['max_high'];
        }
        $where[] = ['high', 'between', [$matting['min_high'], $max_high]];
        //体重
        $max_weight = 200;
        if (!empty($matting['max_weight'])) {
            $max_weight = $matting['max_weight'];
        }
        $where[] = ['weight', 'between', [$matting['min_weight'], $max_weight]];

        //会员
        $list = UserModel::where($where)
            ->order('star desc')
            ->limit(6)
            ->select();
        foreach ($list as $v) {
            $v['age'] = Fun::getAgeByBirth($v['birthday']);
        }
        $this->assign('list', json_encode($list));

        //幻灯片
        $images = SlideItemModel::where('slide_id', 1)->select();
        foreach ($images as $image) {
            $image['image'] = cmf_get_image_url($image['image']);
        }
        $this->assign('images', json_encode($images));

        //未读消息数
        $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);

        //配对播报
        /*$select = UserSelectLogModel::with(['user1','user2'])->order('id desc')->limit(3)->select();
        if (!empty($select)) {
            foreach ($select as $v) {
                $v['name1'] = Fun::getEncodeName($v['user1']['realname']);
                $v['name2'] = Fun::getEncodeName($v['user2']['realname']);
            }
        }
        $this->assign('select',json_encode($select));
        $select_count = UserSelectLogModel::useGlobalScope(false)->count();
        $this->assign('select_count', $select_count + 4);*/

        return $this->fetch(':index');
    }

    public function demo()
    {
        return $this->fetch(':demo');
    }
}
