<?php

namespace Agent\Src;

use Dever;
use Agent\Lib\Core;

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

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

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

    # 获取相同身份证的账号
    public function getUser()
    {
        $where['id'] = $this->uid;
        $where['idcard'] = $this->user['idcard'];
        $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');
        $this->data['shop'] = Dever::db('shop/info')->getAll($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']) {
            
        } else {
            Dever::alert('这不是您的子账户');
        }
        return $this->data;
    }

    # 设置门店
    public function setShop()
    {
        $id = Dever::input('id', $this->uid);
        $shop_id = Dever::input('shop_id');
        $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));
        } 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'] == 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) {
                    $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')->find($where);

        # 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;
    }

    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']);

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

        return $info;
    }

    # 我的权益
    public function goodsList()
    {
        $where['type'] = Dever::input('type', 1);
        $where['mid'] = $this->uid;
        $this->data['list'] = Dever::db('agent/member_goods')->getData($where);
        $this->data['type'] = $where['type'];
        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                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 = '';
                    }
                    
                    $this->data['list'][$k] += array
                    (
                        'pic' => $goods_info['cover'],
                        'name' => $goods_info['name'],
                        'sku' => $sku,
                        'price' => $goods_info['price'],
                    );
                } elseif ($v['type'] == 2) {
                    $card = Dever::db('card/info')->find($v['type_id']);

                    $this->data['list'][$k] += array
                    (
                        'name' => $card['name'],
                        'sku' => '',
                        'price' => $card['price'],
                    );
                } elseif ($v['type'] == 3) {
                    $prize = Dever::db('setting/role_goods')->config['set']['prize'];
                    $this->data['list'][$k] += array
                    (
                        'name' => $prize[$v['type_id']],
                        'sku' => '',
                        'price' => 0,
                    );
                }
            }
        }

        return $this->data;
    }

    # 兑换权益
    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, $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);
            }
        }

        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'] = $this->data['type'][$v['type']];
                $this->data['list'][$k]['status_name'] = $this->data['status'][$v['status']];
                $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'] = $config['type'][$this->data['info']['type']];
            $this->data['info']['status_name'] = $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'];
            }
        }

        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));

        return $this->data;
    }

    # 确定提现
    public function tixian_commit()
    {
        $cash = Dever::input('cash');
        if ($cash > $this->user['cash']) {
            Dever::alert('您的可提现金额不足');
        }
        $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('银行卡号和确认银行卡号不同');
        }

        $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, $name, $bank, $bankname, $card);

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

        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);
            //return Dever::load('agent/lib/email')->renzheng($this->uid, $email, $code);
            Dever::daemon('lib/email.renzheng?mid='.$this->uid.'&email=' . $email . '&code=' . $code, 'agent');

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

    # 获取我的合同
    public function getAgreement()
    {
        $where['mid'] = $this->uid;
        $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'] = $status[$v['status']];
                $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 getSell()
    {
        $this->data['user'] = $this->user;

        $where['mid'] = $this->uid;
        $this->data['list'] = Dever::db('bill/sell_stat')->getData($where);

        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k]['month_string'] = date('Y年月', $v['month']);
            }
        }

        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);
    }
}