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

    # 我的信息
    public function info()
    {
        $data['vip'] = 2;
        $vip = Dever::db('vip', 'place')->find([]);
        if ($vip) {
            $data['vip'] = 1;
        }
        $data['agent'] = 2;
        $agent = Dever::db('agent', 'place')->find([]);
        if ($agent) {
            $data['agent'] = 1;
        }
        /*
        if (Place::$user['vip_id'] > 0) {
            $data['vip'] = Dever::db('vip', 'place')->find(Place::$user['vip_id']);
        }
        if (Place::$user['agent_id'] > 0) {
            $data['agent'] = Dever::db('agent', 'place')->find(Place::$user['agent_id']);
        }*/
        # 获取商品订单
        $data['show_order'] = 2;
        foreach (Place::$resource as $k => $v) {
            if ($v['type'] == 3) {
                $data['show_order'] = 1;
            }
        }
        if ($data['show_order'] == 1) {
            $data['order'][] = array('name' => '待付款', 'num' => Dever::db('order', 'goods')->count(array('status' => 1)), 'status' => 1);
            $data['order'][] = array('name' => '待发货', 'num' => Dever::db('order', 'goods')->count(array('status' => 2)), 'status' => 2);
            $data['order'][] = array('name' => '待收货', 'num' => Dever::db('order', 'goods')->count(array('status' => array('in', '3,4'))), 'status' => '3,4');
            $data['order'][] = array('name' => '已完成', 'num' => Dever::db('order', 'goods')->count(array('status' => array('in', '5,6'))), 'status' => '5,6');
        }
        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('member', 'place')->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('member', 'place')->update(Place::$uid, $update);
    }

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