<?php

namespace Agent\Src;

use Dever;
use Agent\Lib\Core;

class My extends Core
{
    public function getSearch()
    {
        echo Dever::maketime('2022-05-30 16:59:59');die;
        $data = Dever::db('agent/member')->getSearch(array('col' => '朱浙群'));

        return $data;
    }

    public function getCounty()
    {
        return Dever::load('area/import')->getCounty(440000, 441900, '44/4419.html');
    }
    public function test()
    {
        # 1000 邀请 1001 1001 邀请 1002
        $pid = Dever::input('pid', 1000);
        $cid = Dever::input('cid');
        if ($cid) {
            return Dever::load('invite/api')->setRelation($cid, $pid); 
        }
        

        return Dever::load('invite/api')->getParentAll(1007, false); 

        return Dever::load('invite/api')->resetRelation(1007, 2001); 
    }

    public function addtest()
    {
        $member = $this->user;
        return Dever::load('agent/lib/member')->up($member['id'], 1, 1, 1, 100, 100, 1, 'dddd', 1);
    }

    /**
     * 立即签署
     *
     * @return mixed
     */
    public function sign()
    {
        $order_id = Dever::input('order_id');
        return Dever::load('agent/lib/order')->sign($this->uid, $order_id);
    }

    /**
     * 获取基本信息
     *
     * @return mixed
     */
    public function getInfo()
    {
        $this->data['user'] = Dever::load('agent/lib/member')->getInfo($this->user);

        $this->data['config'] = Dever::db('setting/base')->one();
        if ($this->data['config']['active'] == 2) {
            $this->data['config']['active'] = 0;
        } else {
            $this->data['config']['active'] = 1;
        }

        # 获取团队数量
        $this->data['num'] = Dever::load('invite/api')->getChildNum($this->uid, 1);

        # 获取当前正在申请的订单
        $order = Dever::db('agent/order')->getNew(array('mid' => $this->uid));
        if ($order) {
            $total = Dever::db('agent/order')->getTotal(array('mid' => $this->uid));
            $this->data['user']['up'] = 2;
            if ($total > 1) {
                $this->data['user']['up'] = 1;
            }
            $this->data['user']['order_id'] = $order['id'];
            if($order['status'] == 6){
                $order['status'] = $order['history_status'];
            }
            $this->data['user']['audit'] = $order['status'];
            if ($order['status'] != 5) {
                $order['audit_desc'] = '';
            }
            $this->data['user']['audit_desc'] = $order['audit_desc'];

            if ($order['status'] == -2) {
                $agreement = Dever::db('setting/agreement')->getData();
                list($temp, $this->data['user']['agreement_1']) = Dever::load('agent/lib/agreement')->name($agreement[1], $order, $order['role'], 1, 2);
                list($temp, $this->data['user']['agreement_4']) = Dever::load('agent/lib/agreement')->name($agreement[4], $order, $order['role'], 1, 2);
            }
        }
        #获取区域分润
        $this->data['fenrun'] = Dever::db('mail/area_stat')->getMoney(array('mid'=>$this->uid));
        
        return $this->data;
    }

    # 获取相同身份证的账号
    public function getUser()
    {
        $where['id'] = $this->uid;
        $this->data['user'] = array();
        if (!$this->user['idcard']) {
            return $this->data;
        }
        $this->data['user'] = array();
        if (!preg_match(Dever::rule('idcard'), $this->user['idcard'])) {
            return $this->data;
        }
        $where['idcard'] = $this->user['idcard'];
        $where['status'] = '1,2';
        $this->data['user'] = Dever::db('agent/member')->getData($where);

        if ($this->data['user']) {
            foreach ($this->data['user'] as $k => $v) {
                $this->data['user'][$k] = Dever::load('agent/lib/member')->getInfo($v);
            }
        }
        return $this->data;
    }

    # 获取相同身份证的账号详情
    public function getUserView()
    {
        $id = Dever::input('id');
        $this->data['info'] = Dever::db('agent/member')->find($id);
        if ($this->data['info'] && $this->data['info']['idcard'] == $this->user['idcard']) {
            $this->data['info'] = Dever::load('agent/lib/member')->getInfo($this->data['info']);
        } else {
            Dever::alert('这不是您的子账户');
        }

        return $this->data;
    }

    # 获取门店
    public function getShop()
    {
        $where['type'] = 10;
        $where['area'] = Dever::input('area');
        $where['is_agent'] = 1;
        $this->data['shop'] = Dever::db('shop/info')->getAllByType($where);

        //$this->data['pshop'] = Dever::db('shop/info')->getInfo(array('type' => 10));

        $id = Dever::input('id', $this->uid);
        $this->data['info'] = Dever::db('agent/member')->find($id);
        if ($this->data['info'] && $this->data['info']['idcard'] == $this->user['idcard']) {
            if ($this->data['info']['is_shop'] == 1) {
                Dever::alert('暂时不能修改');
            }
            
        } else {
            Dever::alert('这不是您的子账户');
        }
        return $this->data;
    }

    # 设置门店
    public function setShop()
    {
        $id = Dever::input('id', $this->uid);
        $shop_id = Dever::input('shop_id');
        $shop = Dever::db('shop/info')->find($shop_id);
        if (!$shop) {
            Dever::alert('该门店无法归店');
        }
        if ($shop && $shop['is_agent'] == 2) {
            Dever::alert('该门店无法归店');
        }
        $this->data['info'] = Dever::db('agent/member')->find($id);
        if ($this->data['info'] && $this->data['info']['idcard'] == $this->user['idcard']) {
            Dever::db('agent/member')->update(array('where_id' => $id, 'shop_id' => $shop_id, 'is_shop' => 1));
            $insert['mid'] = $id;
            $insert['new_shop_id'] = $shop_id;
            $info = Dever::db('agent/member_shop_log')->find($insert);
            if (!$info) {
                if($this->data['info']['shop_id']){
                    $insert['old_shop_id'] = $this->data['info']['shop_id'];
                }else{
                    $insert['old_shop_id'] = '';
                }
                Dever::db('agent/member_shop_log')->insert($insert);
            }
        } else {
            Dever::alert('这不是您的子账户');
        }

        return 'ok';
    }

    # 账号与代理区域
    public function area()
    {
        $this->data['user'] = Dever::load('agent/lib/member')->getInfo($this->user);

        $where['mid'] = $this->data['user']['id'];

        /*
        $this->data['area'] = Dever::db('agent/member_area')->getData($where);

        if ($this->data['area']) {
            foreach ($this->data['area'] as $k => $v) {
                $this->data['area'][$k]['area'] = Dever::load("area/api.string", $v['area']);
            }
        }*/

        $this->data['button'] = array();
        $this->data['button']['up'] = 2;
        $this->data['button']['buy'] = 2;
        if ($this->data['user']['role']) {
            if ($this->data['user']['role']['levelup'] == 1) {
                $this->data['button']['up'] = 1;
                if ($this->data['user']['role']['type'] == 2 && $this->data['user']['role']['area'] == 1) {
                    $temp = explode(',', $this->data['user']['area']);
                    $city = Dever::db('area/city')->find($temp[1]);
                    if ($city && $city['level_id'] == 1) {
                        $this->data['button']['up'] = 2;
                    }
                }
            }
            if ($this->data['user']['role']['isbuy'] == 1) {
                $this->data['button']['buy'] = 2;//不允许购买新区域
            }
        }

        return $this->data;
    }

    # 账号与代理区域
    public function getUpInfo()
    {
        $this->data['user'] = Dever::load('agent/lib/member')->getInfo($this->user);

        if ($this->data['user']['role']['type'] == 2) {
            $temp = explode(',', $this->data['user']['area']);

            # 如果当前是城市，只能升级为更高级的城市
            $city = Dever::db('area/city')->find($temp[1]);
            $level_info = array();
            if ($city['level_id'] && $city['level_id'] > 0) {
                $level_info = Dever::db('area/level')->find($city['level_id']);
            }
            
            if ($this->data['user']['role']['area'] == 1) {
                if ($city && $level_info && $level_info['level'] == 1) {
                    $this->data['role'] = array();
                } else {
                    $this->data['role'] = Dever::db('setting/role')->getInfo(array('isbuy' => 1, 'type' => 2, 'area' => 1));
                }
            }
            # 如果当前是区县，可以升级为城市和更高级的区县
            if ($this->data['user']['role']['area'] == 2) {
                $area = '1,2';
                if ($city && $level_info && $level_info['level'] == 1) {
                    $county = Dever::db('area/county')->find($temp[2]);
                    if ($county && $county['level'] == 1) {
                        $area = '1';
                    }
                }
                $this->data['role'] = Dever::db('setting/role')->getInfo(array('isbuy' => 1, 'type' => 2, 'area' => $area));
            }
            # 如果当前是街道，都可以升级
            if ($this->data['user']['role']['area'] == 3) {
                $area = '1,2,3';
                if ($city && $level_info && $level_info['level'] == 1) {
                    $area = '1,2';
                    /*
                    $county = Dever::db('area/county')->find($temp[2]);
                    $town = Dever::db('area/town')->find($temp[3]);
                    if ($county && $county['level'] == 1 && $town && $town['type'] != 1) {
                        $area = '1,2';
                    }
                    */
                }

                $this->data['role'] = Dever::db('setting/role')->getInfo(array('isbuy' => 1, 'type' => 2, 'area' => $area));
            }
        } else {
            $this->data['role'] = Dever::db('setting/role')->getInfo(array('isbuy' => 1, 'type' => 2));
        }

        $this->data['level_total'] = count($this->data['role']) + 1;

        $this->data['config'] = Dever::db('setting/base')->find();

        return $this->data;
    }

    # 购买新区域 & 账号升级
    public function buyArea()
    {
        $state = Dever::load('agent/lib/order')->action($this->uid);

        if ($state) {
            return $state;
        } else {
            Dever::alert('购买失败');
        }
    }

    # 推广邀请
    public function invite()
    {
        $this->data['code'] = Dever::load('invite/api')->code($this->uid);

        return $this->data;
    }

    # 代理区域查询
    public function checkArea()
    {
        $where['role'] = Dever::input('role');
        $where['area'] = Dever::input('area');

        $this->data = Dever::load('agent/lib/order')->getPrice();
        $this->data['area_string'] = Dever::load("area/api.string", $where['area'], ' / ');
        $info = Dever::db('agent/member_area')->getOne($where);
        if (!$info) {
            $info = Dever::db('agent/member')->getOne($where);

            if ($info) {
                $role = Dever::db('setting/role')->find($where['role']);

                if ($role && $role['type'] == 2) {
                    Dever::load('area/api')->upStatus($where['area'], 2);
                    $area_data['mid'] = $this->uid;
                    $area_data['order_id'] = -1;
                    $area_data['role'] = $role['id'];
                    $area_data['area'] = $where['area'];
                    $area_data['type'] = 5;
                    $area_data['price'] = 0;
                    $area_data['status'] = 1;
                    $state = Dever::db('agent/member_area')->getOne($where);
                    if (!$state) {
                        Dever::db('agent/member_area')->insert($area_data);
                    }
                }
            }
        }

        # 1已被代理 2未被代理
        if ($info) {
            $this->data['state'] = 1;
        } else {
            $this->data['state'] = 2;
        }

        return $this->data;
    }

    # 我的代理订单
    public function order()
    {
        $where = array();
        $where['mid'] = $this->uid;
        $type = Dever::input('type');
        if ($type == 1) {
            # 审核中
            $where['status'] = '1,2,3';
        } elseif ($type == 2) {
            # 审核中
            $where['status'] = '4';
        } elseif ($type == 3) {
            # 审核中
            $where['status'] = '5';
        }
        
        $this->data['list'] = Dever::db('agent/order')->getData($where);

        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k] = $this->getOrderInfo($v);
            }
        }
        return $this->data;
    }

    public function orderInfo()
    {
        $where = array();
        $where['mid'] = $this->uid;
        $where['id'] = Dever::input('id');
        $this->data['info'] = Dever::db('agent/order')->find($where);

        if ($this->data['info']) {
            $this->data['info'] = $this->getOrderInfo($this->data['info']);
        }

        return $this->data;
    }

    # 修改信息
    public function upOrderInfo()
    {
        Dever::config('base')->hook = true;
        $where = array();
        $where['mid'] = $this->uid;
        $where['id'] = Dever::input('id');
        if (!$where['id']) {
            $where['id'] = Dever::input('order_id');
        }
        
        $info = Dever::db('agent/order')->find($where);

        //if ($info['status'] == 5) {
        if ($info['status']) {
            $update = array();
            $update['name'] = Dever::input('name');
            if (!$update['name']) {
                Dever::alert('姓名不能为空');
            }
            $update['idcard'] = Dever::input('idcard');
            if (!$update['idcard']) {
                Dever::alert('身份证号不能为空');
            }
            $update['idcard_front'] = Dever::input('idcard_front');
            if (!$update['idcard_front']) {
                Dever::alert('请上传身份证正面');
            }

            $update['idcard_back'] = Dever::input('idcard_back');
            if (!$update['idcard_back']) {
                Dever::alert('请上传身份证反面');
            }

            $update['idcard_path'] = Dever::input('idcard_path');
            if($update['idcard_path']){
                $idcard_path = (array)json_decode($update['idcard_path']);
                if($idcard_path['face']){
                    $rest = (array)$idcard_path['face'];
                    if (isset($rest['name'])) {
                        if($this->user['name'] != $rest['name'] || $this->user['idcard'] != $rest['idNumber']){
                            Dever::alert('姓名或身份证号不匹配');
                        }
                    }
                    
                }
                if($idcard_path['back']){
                    $rest = (array)$idcard_path['back'];
                    if (isset($rest['validPeriod'])) {
                        $date = explode('-',$rest['validPeriod']);
                        if($date[1] && $date[1]!='长期'){
                            $cdate = strtotime(str_replace('.','-',$date[1]));
                            if(time()>=$cdate){
                                Dever::alert('身份证背面已过期');
                            }
                        }
                    }
                }
            }

            if ($info['type'] == 2) {
                $update['company_name'] = Dever::input('company_name');
                $update['company_pic'] = Dever::input('company_pic');
                $update['company_number'] = Dever::input('company_number');
                if (!$update['company_name']) {
                    Dever::alert('公司名称不能为空');
                }
                if (!$update['company_pic']) {
                    Dever::alert('营业执照照片不能为空');
                }
                if (!$update['company_number']) {
                    Dever::alert('营业执照号码不能为空');
                }
            }

            $sign = Dever::input('sign');
            if ($sign) {
                $update['sign'] = $sign;
                //Dever::alert('请填写签名');
            }

            $area = Dever::input('area');
            if ($area) {
                $update['area'] = $area;
            }

            //$update += Dever::load('agent/lib/order')->getPrice($this->user);

            if ($info['pay_type'] == 2) {
                $update['pay_pic'] = Dever::input('pay_pic');
                if (!$update['pay_pic']) {
                    Dever::alert('请上传汇款截图');
                }
            }

            $code = Dever::input('code');
            if ($code) {
                $uid = Dever::load('invite/api')->getUid($code);
                if ($uid) {
                    $user = Dever::db('agent/member')->find($uid);
                    if ($user && $user['status'] <= 2) {
                        $update['parent_mid'] = $user['id'];
                    }
                    $update['parent_code'] = $code;
                }
            }

            $update['where_id'] = $this->uid;
            Dever::db('agent/member')->update($update);

            $update['where_id'] = $info['id'];
            if ($info['history_status']) {
                $update['status'] = $info['history_status'];
            } else {
                $update['status'] = 1;
            }
            
            $update['udate'] = time();
            Dever::db('agent/order')->update($update);

            $this->data['info'] = $info;

            $agreement = Dever::db('setting/agreement')->getData();
            list($temp, $this->data['agreement_1']) = Dever::load('agent/lib/agreement')->name($agreement[1], $info, $info['role'], 1, 2);
            list($temp, $this->data['agreement_4']) = Dever::load('agent/lib/agreement')->name($agreement[4], $info, $info['role'], 1, 2);

            return $this->data;
        }

        Dever::alert('无申请信息');
    }

    private function getOrderInfo($info)
    {
        $info['role'] = Dever::db('setting/role')->find($info['role']);
        if ($info['status'] <= 3) {
            $info['status'] = 1;
            $info['status_name'] = '审核中';
        } elseif ($info['status'] == 4) {
            $info['status'] = 2;
            $info['status_name'] = '已确认';
        } elseif ($info['status'] == 5) {
            $info['status'] = 3;
            $info['status_name'] = '已作废';
        }
        $info['cdate'] = date('Y-m-d', $info['cdate']);

        $info['area_string'] = Dever::load("area/api.string", $info['area']);

        $info['member'] = Dever::db('agent/member')->find($info['mid']);

        if ($info['parent_mid'] && $info['parent_mid'] > 0) {
            $info['parent_member'] = Dever::db('agent/member')->find($info['parent_mid']);
            $info['parent_member']['code'] = Dever::load("invite/api.code", $info['parent_mid']);
        }

        if ($info['source_id'] && $info['source_id'] > 0) {
            $info['source'] = Dever::db('setting/source')->find($info['source_id']);
        }
        

        return $info;
    }

    # 我的权益
    public function goodsList()
    {
        $where['type'] = Dever::input('type', 1);
        $where['mid'] = $this->uid;
        $this->data['list'] = array();
        $list = Dever::db('agent/member_goods')->getData($where);
        $this->data['type'] = $where['type'];
        if ($list) {
            foreach ($list as $k => $v) {
                if ($v['num'] <= 0) {
                    continue;
                }
                $this->getGoodsInfo($v);
                $this->data['list'][] = $v;
            }
        }

        return $this->data;
    }

    public function goodsListView()
    {
        $where['mid'] = $this->uid;
        $where['id'] = Dever::input('id');
        if (!$where['id']) {
            Dever::alert('错误');
        }
        $this->data['info'] = Dever::db('agent/member_goods')->find($where);
        if ($this->data['info']) {
            $this->getGoodsInfo($this->data['info']);
        }

        return $this->data;
    }

    private function getGoodsInfo(&$v)
    {
        if (!isset($v['num'])) {
            $v['num'] = $v['total_num'] - $v['sell_num'];
        }
        
        if ($v['type'] == 1) {
            $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['type_id'], $v['sku_id']);
            if (isset($goods_info['sku'])) {
                $sku = $goods_info['sku']['string'];
            } else {
                $sku = '';
            }
            
            $v += array
            (
                'pic' => $goods_info['cover'],
                'name' => $goods_info['name'],
                'sku' => $sku,
                'price' => $goods_info['price'],
            );
        } elseif ($v['type'] == 2) {
            $code = Dever::db('card/code')->find($v['sku_id']);
            $card = Dever::db('card/info')->find($code['card_id']);
            $card_type = Dever::db('card/type')->find($card['type_id']);
            $shop_type = Dever::db('shop/info')->config['config_type'];
            $card_type['shop_type'] = explode(',', $card_type['shop_type']);
            foreach ($card_type['shop_type'] as $k1 => $v1) {
                $card_type['shop_type'][$k1] = $shop_type[$v1];
            }
            $card_type['shop_type'] = implode(',', $card_type['shop_type']);

            if ($card['end_dh_day'] && $card['end_dh_day'] > 0) {
                $code['eddate'] = $code['bdate'] + ($card['end_dh_day'] * 86400);
            }

            $end_date = '';
            $show = 1;
            if (isset($code['eddate']) && $code['eddate'] && $code['eddate'] > 0) {
                $end_date = '截止' . date('Y-m-d H:i', $code['eddate']) . '前可以使用';
                $time = time();
                if ($code['eddate'] <= $time) {
                    $show = 2;
                }
            }

            $v += array
            (
                'name' => $card['name'],
                'sku' => '',
                'price' => $code['total_cash'],
                'card' => $code['card'],
                'card_status' => $code['status'],
                'shop_type' => $card_type['shop_type'],
                'card_desc' => $card['content'],
                'end_date' => $end_date,
                'show' => $show,
            );
        } elseif ($v['type'] == 3) {
            $prize = Dever::db('setting/prize')->find($v['type_id']);
            $v += array
            (
                'name' => $prize['name'],
                'sku' => '',
                'price' => $prize['price'],
                'desc' => $prize['desc'],

            );
        }
    }

    # 兑换权益
    public function dhGoods()
    {
        $type = Dever::input('type', 1);
        $this->data = Dever::load('agent/lib/dhorder')->goods($this->uid);

        return $this->data;
    }

    # 兑换权益 确认订单
    public function dhGoodsAct_commit()
    {
        $type = Dever::input('type', 1);
        $data = Dever::load('agent/lib/dhorder')->goods($this->uid);
        $address_id = Dever::input('address_id');
        if (!$address_id) {
            Dever::alert('请选择配送地址');
        }
        $dh_type = 1;
        if ($type == 3) {
            $dh_type = 2;
        }
        $info = Dever::input('info');

        return Dever::load('agent/lib/dhorder')->action($this->user, $data['name'], $data['num'], $data['price'], $data['list'], $address_id, $dh_type, $type, -1, $info);
    }

    # 我的权益订单
    public function goodsOrder()
    {
        return Dever::load('agent/lib/dhorder')->getList($this->uid);
    }

    # 我的权益订单详情
    public function goodsOrderView()
    {
        $id = Dever::input('order_id');
        return Dever::load('agent/lib/dhorder')->getView($this->uid, $id);
    }

    # 获取我的团队
    public function getGroup()
    {
        # 获取团队数量
        $this->data['user'] = Dever::load('invite/api')->getChild($this->uid, 1);

        if ($this->data['user']) {
            foreach ($this->data['user'] as $k => $v) {
                $v = Dever::db('agent/member')->find($v['to_uid']);
                $this->data['user'][$k] = Dever::load('agent/lib/member')->getInfo($v);
                $this->data['user'][$k]['cdate'] = date('Y-m-d H:i', $v['cdate']);
            }
        }

        return $this->data;
    }

    # 获取我的资金
    public function getCash()
    {
        $this->data['user'] = $this->user;
        $this->data['type'] = Dever::db('bill/cash')->config['set']['type'];
        $this->data['status'] = Dever::db('bill/cash')->config['set']['status'];

        $where['mid'] = $this->uid;
        $type = Dever::input('type');
        if ($type) {
            $where['type'] = $type;
        }
        $this->data['list'] = Dever::db('bill/cash')->getData($where);
        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k]['type_name'] = Dever::status($this->data['type'], $v['type']);
                $this->data['list'][$k]['status_name'] = Dever::status($this->data['status'], $v['status']);
                if ($v['type'] == 11) {
                    $tixian = Dever::db('bill/tixian')->find($v['type_id']);
                    if ($tixian && $tixian['status'] == 2) {
                        $this->data['list'][$k]['status_name'] .= '（已发放）';
                    } else {
                        $this->data['list'][$k]['status_name'] .= '（待发放）';
                    }
                }
                $this->data['list'][$k]['cdate'] = date('Y-m-d H:i', $v['cdate']);
                if ($v['cash'] > 0) {
                    $this->data['list'][$k]['cash'] = '+' . $v['cash'];
                }
            }
        }

        return $this->data;
    }

    public function getCashView()
    {
        $where['mid'] = $this->uid;
        $where['id'] = Dever::input('id');
        $this->data['info'] = Dever::db('bill/cash')->find($where);

        $config = Dever::db('bill/cash')->config['set'];

        if ($this->data['info']) {
            $this->data['info']['type_name'] = Dever::status($config['type'], $this->data['info']['type']);
            $this->data['info']['status_name'] = Dever::status($config['status'], $this->data['info']['status']);

            $this->data['info']['cdate'] = date('Y-m-d H:i', $this->data['info']['cdate']);
            if ($this->data['info']['cash'] > 0) {
                $this->data['info']['cash'] = '+' . $this->data['info']['cash'];
            }
            if ($this->data['info']['type'] == 11) {
                $tixian = Dever::db('bill/tixian')->find($this->data['info']['type_id']);
                if ($tixian && $tixian['status'] == 2) {
                    $this->data['info']['status_name'] .= '（已发放）';
                } else {
                    $this->data['info']['status_name'] .= '（待发放）';
                }
            }
        }

        return $this->data;
    }

    # 提现详情
    public function tixianInfo()
    {
        $this->data['user'] = $this->user;
        $this->data['config'] = Dever::db('setting/base')->one();
        $this->data['config']['tax'] = $this->data['config']['tax']/100;
        $this->data['bank'] = Dever::db('setting/bank')->select();

        # 获取上次提现记录
        $this->data['mybank'] = Dever::db('bill/tixian')->getNew(array('mid' => $this->uid));
        if($this->data['user']){#234初中高可提现
            $this->data['user']['ti_type'] = 2;
            if ($this->data['user']['level_id'] > 0) {
                $this->data['user']['ti_type'] = 1;
                $level=Dever::db('setting/level')->one(array('id'=>$this->data['user']['level_id'],'state'=>1));
                $this->data['config']['market_desc']=$level['market_desc'];
            }
            /*
            if($this->data['user']['level_id']==2 || $this->data['user']['level_id']==3 || $this->data['user']['level_id']==4){
                $this->data['user']['ti_type']=1;
            }else{
                $this->data['user']['ti_type']=2;
            }
            */
            
        }
        return $this->data;
    }

    # 确定提现
    public function tixian_commit()
    {
        if (!$this->user['idcard']) {
            Dever::alert('未做身份证号认证，请先做身份认证');
        }
        $cash = Dever::input('cash');
        if ($cash > $this->user['cash']) {
            Dever::alert('您的可提现金额不足');
        }
        if ($cash < 50) {
            Dever::alert('未满50元不可提现');
        }
        /*
        $name = Dever::input('name');
        if (!$name) {
            Dever::alert('姓名不能为空');
        }
        $bank = Dever::input('bank');
        if (!$bank) {
            Dever::alert('银行不能为空');
        }
        $bankname = Dever::input('bankname');
        if (!$bankname) {
            Dever::alert('开户行不能为空');
        }
        $card = Dever::input('card');
        if (!$card) {
            Dever::alert('银行卡号不能为空');
        }
        $ycard = Dever::input('ycard');
        if (!$ycard) {
            Dever::alert('确认银行卡号不能为空');
        }
        if ($card != $ycard) {
            Dever::alert('银行卡号和确认银行卡号不同');
        }
        */
        $sign = Dever::input('sign');
        if(!$sign){
            //Dever::alert('手写签名不能为空');
        }

        $config = Dever::db('setting/base')->one();
        $tax = $config['tax']/100;
        $ycash = $cash;
        $cash = $cash - $cash*$tax;

        $id = Dever::load('bill/lib/tixian')->up($this->uid, $ycash, $cash, $sign);

        if ($id) {
            //$cash_id = Dever::load('bill/lib/cash')->up($this->uid, 11, $ycash, $this->user['role'], $id, '提现到'.substr($card, -4).'银行卡', 1);
            $cash_id = Dever::load('bill/lib/cash')->up($this->uid, 11, $ycash, $this->user['role'], $id, '提现', 1);

            Dever::db('bill/tixian')->update(array('where_id' => $id, 'cash_id' => $cash_id));

            $where['where_id'] = $this->uid;
            $where['cash'] = -1*$ycash;
            Dever::db('agent/member')->upCash($where);
        }

        return 'ok';
    }

    # 修改密码
    public function setPass()
    {
        $pass = Dever::input('pass');
        if (!$pass) {
            Dever::alert('请输入密码');
        }

        $where['where_id'] = $this->uid;
        $where['password'] = $pass;

        Dever::db('agent/member')->update($where);

        return Dever::db('agent/member')->find(array('id' => $this->uid, 'clear' => true));
    }

    # 修改资料
    public function setInfo()
    {
        $where = array();
        $name = Dever::input('name');
        if ($name) {
            $where['name'] = $name;
        }
        $avatar = Dever::input('avatar');
        if ($avatar) {
            $where['avatar'] = $avatar;
        }
        $username = Dever::input('username');
        if ($username) {
            $where['username'] = $username;
        }
        $birthday = Dever::input('birthday');
        if ($birthday) {
            $where['birthday'] = Dever::maketime($birthday);
        }

        if ($where) {
            $where['where_id'] = $this->uid;
            Dever::db('agent/member')->update($where);
        }

        if ($name && $name != $this->user['name']) {
            # 如果修改名字，就要把合同都变成待签署状态
            $agreement = Dever::db('agent/member_agreement')->select(array('mid' => $this->uid, 'status' => 2));

            if ($agreement) {
                $sign = array();
                foreach ($agreement as $k => $v) {
                    if ($v['name'] == $this->user['name']) {
                        Dever::db('agent/member_agreement')->update(array('where_id' => $v['id'], 'status' => 3));
                        $sign[] = $v;
                    }
                }
                if ($sign) {
                    foreach ($sign as $k => $v) {
                        $info = Dever::db('agent/member_agreement')->find(array('mid' => $this->uid, 'name' => $name));
                        if ($info) {
                            Dever::db('agent/member_agreement')->update(array('where_id' => $info['id'], 'status' => 1));
                        } else {
                            unset($v['id']);
                            unset($v['cdate']);
                            unset($v['sdate']);
                            unset($v['edate']);
                            unset($v['qdate']);
                            unset($v['sign']);
                            $member = Dever::db('agent/member')->find($v['mid']);
                            $v['type'] = $member['type'];
                            $v['company_number'] = $member['company_number'];
                            $v['idcard'] = $member['idcard'];
                            $v['mobile'] = $member['mobile'];
                            $v['sign'] = '';
                            $v['status'] = 1;
                            $v['name'] = $name;
                            Dever::db('agent/member_agreement')->insert($v);
                        }
                    }
                }
            }
        }

        return Dever::db('agent/member')->find(array('id' => $this->uid, 'clear' => true));
    }

    # 发送邮件
    public function sendEmail()
    {
        $email = Dever::input('email');
        if ($email) {
            if (!preg_match(Dever::rule('email'), $email)) {
                Dever::alert('请输入正确的邮箱');
            }

            $check = Dever::db('agent/member')->find(array('email' => $email));
            if ($check) {
                Dever::alert('该邮箱已被认证，请更换');
            }

            $code = Dever::load('passport/reg')->code(false, false);

            $email = base64_encode($email);
            Dever::daemon('lib/email.renzheng?mid='.$this->uid.'&email=' . $email . '&code=' . $code . '&username=' . $this->user['name'], 'agent');

            return '邮件已经发送成功！请到您的邮箱里查看。';
        } else {
            Dever::alert('请输入邮箱');
        }
    }

    # 获取我的合同
    public function getAgreement()
    {
        $where['mid'] = $this->uid;
        $where['status'] = '1,2';
        $this->data['list'] = Dever::db('agent/member_agreement')->getAll($where);

        if ($this->data['list']) {
            $status = Dever::db('agent/member_agreement')->config['status'];
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k]['status_name'] = Dever::status($status, $v['status']);
                $this->data['list'][$k]['qdate_string'] = '';
                if ($v['qdate']) {
                    $this->data['list'][$k]['qdate_string'] = date('Y.m.d', $v['qdate']);
                }
                
                $role = Dever::db('setting/role')->find($v['role']);
                $name = Dever::load('agent/lib/agreement')->name($v['agreement_id'], $v, $role, 2, 2);
                $this->data['list'][$k]['link'] = $name[1];
            }
        }

        return $this->data;
    }

    # 获取我的合同
    public function getAgreementView()
    {
        $where['mid'] = $this->uid;
        $where['id'] = Dever::input('id');
        $this->data['info'] = array();
        if ($where['id']) {
            $status = Dever::db('agent/member_agreement')->config['status'];
            $this->data['info'] = Dever::db('agent/member_agreement')->find($where);
            if ($this->data['info']) {
                $this->data['info']['status_name'] = Dever::status($status, $this->data['info']['status']);
                $this->data['info']['qdate_string'] = '';
                if ($this->data['info']['qdate']) {
                    $this->data['info']['qdate_string'] = date('Y.m.d', $this->data['info']['qdate']);
                }
                
                $role = Dever::db('setting/role')->find($this->data['info']['role']);
                $name = Dever::load('agent/lib/agreement')->name($this->data['info']['agreement_id'], $this->data['info'], $role, 2, 2);
                $this->data['info']['link'] = $name[1];
            }
        }

        return $this->data;
    }

    # 对待签署的合同进行重新签署
    public function upAgreement()
    {
        $sign = Dever::input('sign');
        if (!$sign) {
            Dever::alert('请上传签名');
        }
        $where['mid'] = $this->uid;
        $where['id'] = Dever::input('id');
        if ($where['id']) {
            $info = Dever::db('agent/member_agreement')->find($where);
            if ($info && $info['status'] == 1) {
                $state = Dever::db('agent/member_agreement')->update(array('sign' => $sign, 'where_id' => $info['id'], 'status' => 2, 'qdate' => time()));
                if ($state) {
                    $role = Dever::db('setting/role')->find($info['role']);
                    $info['sign'] = $sign;
                    $name = Dever::load('agent/lib/agreement')->name($info['agreement_id'], $info, $role, 2, 2);

                    $soft_cash = Dever::db('agent/soft_cash')->find(array('agreement_id'=>$where['id'],'mid'=>$where['mid']));
                    if($soft_cash){
                        $update['where_id'] = $soft_cash['id'];
                        $update['set_fdate'] = time();
                        $update['set_type'] = 2;
                        Dever::db('agent/soft_cash')->update($update);
                    }
                    return array('link' => $name[1]);
                }
            }
        }
        Dever::alert('合同不存在');
    }
    
    # 获取我的业绩统计
    public function getSell()
    {
        $this->data['user'] = $this->user;

        $where['mid'] = $this->uid;
        $this->data['list'] = Dever::db('bill/sell_stat')->getData($where);
        #如果没有就插入
        $start_time = date('Y-m',time()) .'-01 00:00:00';
        $start = Dever::maketime($start_time);
        $sell=Dever::db('bill/sell_stat')->find(array('mid'=>$this->data['user']['id'],'month'=>$start));
        if(!$sell){
            $where['num']=0;
            $where['new_num']=0;
            $where['month']=$start;
            $where['mid']=$this->data['user']['id'];
            $where['cdate']=time();
            Dever::db('bill/sell_stat')->insert($where);
        }
        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k]['month_string'] = date('Y年m月', $v['month']);
                #月份等于当前月份
                if( $this->data['list'][$k]['month_string']==date('Y年m月',time())){
                    $this->data['list'][$k]['num']=$this->data['user']['group_sell'];
                }
            }
        }
        return $this->data;
    }

    # 获取默认地址信息
    public function getDefaultAddress()
    {
        $id = Dever::input('id');
        return Dever::load('agent/address')->getDefaultAddress($this->uid);
    }

    # 我的地址列表
    public function address()
    {
        return Dever::load('agent/address')->getAddress($this->uid);
    }

    # 获取单个地址信息
    public function getAddress()
    {
        $id = Dever::input('id');
        return Dever::load('agent/address')->getOne($this->uid, $id);
    }

    # 删除单个地址信息
    public function delAddress()
    {
        $id = Dever::input('id');
        return Dever::load('agent/address')->del($this->uid, $id, 2);
    }
    #获取体验店和零售店区域分润
    public function getAreastat(){
        $data = Dever::db('mail/area_stat')->getTy(array('mid'=>$this->uid));
        return $data;
    }
    #获取体验店和零售店区域分润列表
    public function getAreadesc(){
        $type = Dever::input('type');#1=体验店，2=零售店
        $data = array();
        if (!$type) {
            $type = 1;
        }
        $res = Dever::db('mail/area_stat')->getTy(array('mid'=>$this->uid));
        $data['list'] = Dever::db('mail/area_stat')->getTiyan(array('mid'=>$this->uid));
        if ($type == 1) {
            $price = 't_fprice';
        } else {
            $price = 'l_fprice';
        }
        $data['total']= $res[$price];
        if ($data['list']) {
            foreach ($data['list'] as $k => $v) {
                $data['list'][$k]['month'] = date('Y年m月',$v['month']);
                $data['list'][$k]['price'] = $v[$price];
            }
        }
        return $data;
    }
}
