<?php namespace Place_interface\Api;
use Dever;
use Place;
use Place_interface\Lib\Core;
class User extends Core
{
    protected $login = true;
    protected $entry = true;

    # 我的信息
    public function info()
    {
        $data = array();
        # 获取角色
        $data['role'] = Dever::db('role', 'place_user')->select(array('status' => 1), array('col' => 'id,name,icon'));

        if ($data['role']) {
            foreach ($data['role'] as &$role) {
                $user_role = Dever::db('user_role', 'place_user')->find(array('role_id' => $role['id']));
                if ($user_role) {
                    $role['level'] = Dever::db('role_level', 'place_user')->find($user_role['level_id'], array('col' => 'id,name,icon'));
                }
            }
        }
        # 获取积分
        $score = Dever::db('user', 'place_score')->select(array('uid' => Place::$uid), array('col' => 'info_id,total,used,balance'));
        if ($score) {
            foreach ($score as $k => $v) {
                $data['score'][$k] = Dever::db('info', 'place_score')->find($v['info_id'], array('col' => 'id,name,symbol,symbol_location'));
                $data['score'][$k] += $v;
            }
        }
        return Place::result($data);
    }

    # 更新我的信息
    public function update()
    {
        $update = array();
        $name = Dever::input('name');
        if ($name) {
            Place::$user['name'] = $update['name'] = $name;
        }
        $avatar = Dever::input('avatar');
        if ($avatar) {
            Place::$user['avatar'] = $update['avatar'] = $avatar;
        }
        if ($update) {
            Dever::db('info', 'place_user')->update(Place::$uid, $update);
        }
        return Place::$user;
    }

    # 修改密码
    public function pwd()
    {
        # 大于8位字符，至少包含：小写字母、大写字母、数字、特殊字符
        $pwd = Dever::input('pwd', '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/', '密码');
        $code = Dever::input('code', 'is_string', '验证码');
        Dever::load('template', 'msg')->check('user_code', Place::$user['mobile'], $code);
        $update = Dever::load('manage/common')->createPwd($pwd);
        Dever::db('info', 'place_user')->update(Place::$uid, $update);
    }

    # 发送短信验证码
    public function sms()
    {
        $data = Dever::load('template', 'msg')->send('user_code', array('mobile' => Place::$user['mobile']));
        return $data;
    }

    # 我的分享
    public function invite()
    {
        # 代理邀请码
        $data['code'] = Dever::load('code', 'invite')->get(Place::$uid);

        $data['parent'] = 2;
        # 获取我的上级
        $relation = Dever::load('relation', 'invite')->getParent(Place::$uid);
        if ($relation) {
            $data['parent'] = 1;
            $data['parent_info'] = Dever::db('info', 'place_user')->find($relation['uid'], array('col' => 'id,name,avatar'));
        }
        return $data;
    }

    # 我的下级列表
    public function getGroup()
    {
        $relation = Dever::load('relation', 'invite')->getChild(Place::$uid);
        $result = array();
        if ($relation) {
            foreach ($relation as $k => $v) {
                $result[$k] = Dever::db('info', 'place_user')->find($v['to_uid'], array('col' => 'id,name,avatar'));
                $result[$k]['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
            }
        }
        return $result;
    }

    # 填写邀请码，添加上级
    public function addParent()
    {
        $relation = Dever::load('relation', 'invite')->getParent(Place::$uid);
        if ($relation) {
            Dever::error('您已经有上级了');
        }
        $invite = Dever::input('invite', 'is_numeric', '请填写邀请码');
        $parent_uid = Dever::load('code', 'invite')->getUid($invite);
        if (!$parent_uid) {
            Dever::error('邀请码不存在');
        }
        $parent = Dever::db('info', 'place_user')->find($parent_uid);
        if (!$parent) {
            Dever::error('邀请码不存在');
        }
        Dever::load('relation', 'invite')->set($parent_uid, Place::$uid);
        Dever::db('info', 'place_user')->update(Place::$uid, array('parent_uid' => $parent_uid));
        return 'ok';
    }
}