<?php

namespace Agent\Lib;

use Dever;

class Order
{
    /**
     * 下单
     *
     * @return mixed
     */
    public function action($mid)
    {
        $member = array();
    	if ($mid > 0) {
    		$member = Dever::db('agent/member')->find($mid);
    		if (!$member) {
    			Dever::alert('代理商不存在');
    		}
    		$mobile = $member['mobile'];
    		$parent_mid = $member['parent_mid'];
            $data['mid'] = $member['id'];
    		$data['type'] = $member['type'];
	        $data['name'] = $member['name'];
	        $data['idcard'] = $member['idcard'];
	        $data['idcard_front'] = $member['idcard_front'];
	        $data['idcard_back'] = $member['idcard_back'];
            $data['company_name'] = $member['company_name'];
            $data['company_pic'] = $member['company_pic'];
            $data['company_number'] = $member['company_number'];
            $data['source_id'] = $member['source_id'];
    	} else {
    		$mobile = Dever::input('mobile');
	        $where['mobile'] = Dever::load('agent/lib/member')->checkMobile($mobile);
	        $checkMember = Dever::db('agent/member')->find($where);

	        if ($checkMember) {
	            Dever::alert('注册失败，手机号已存在');
	        }

            $parent_mid = -1;

            $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) {
                        $parent_mid = $user['id'];
                    }
                }
            }

	        $data['type'] = Dever::input('type', 1);
	        $data['name'] = Dever::input('name');
	        $data['idcard'] = Dever::input('idcard');
	        $data['idcard_front'] = Dever::input('idcard_front');
	        $data['idcard_back'] = Dever::input('idcard_back');
            $data['company_name'] = Dever::input('company_name');
            $data['company_pic'] = Dever::input('company_pic');
            $data['company_number'] = Dever::input('company_number');
            $data['source_id'] = Dever::input('source_id');
    	}
        
    	Dever::load('agent/lib/member')->checkMcode($mobile);

        $data['mobile'] = $mobile;
        $data['parent_mid'] = $parent_mid;

        if (!$data['idcard_front']) {
            Dever::alert('身份证正面不能为空');
        }
        if (!$data['idcard_back']) {
            Dever::alert('身份证反面不能为空');
        }

        if ($data['type'] == 2) {
        	if (!$data['company_name']) {
                Dever::alert('公司名称不能为空');
            }
            if (!$data['company_pic']) {
                Dever::alert('营业执照照片不能为空');
            }
            if (!$data['company_number']) {
                Dever::alert('营业执照号码不能为空');
            }
        }
        if (!$data['name']) {
            Dever::alert('姓名不能为空');
        }
        if (!$data['idcard']) {
            Dever::alert('身份证号不能为空');
        }

        $data += $this->getPrice($member);

        $data['sign'] = Dever::input('sign');
        if (!$data['sign']) {
            Dever::alert('手写签名不能为空');
        }

        $data['order_num'] = $this->getOrderId();

        # 付款信息
        $data['pay_type'] = Dever::input('pay_type', 1);

        if ($data['pay_type'] == 1 && $data['role']['type'] == 1) {
            Dever::alert('暂时不支持在线支付');
            $data['openid'] = Dever::input('openid');
            if (!$data['openid']) {
                Dever::alert('请先授权');
            }
        } elseif ($data['pay_type'] == 3) {
        	$data['bank_id'] = Dever::input('bank_id');
        	if (!$data['bank_id']) {
        		Dever::alert('请选择银行');
        	}
        	$data['bank_card'] = Dever::input('bank_card');
        	if (!$data['bank_card']) {
        		Dever::alert('请输入银行卡号');
        	}
        } else {
        	# 汇款
        	$data['pay_pic'] = Dever::input('pay_pic');
        	if (!$data['pay_pic']) {
        		Dever::alert('请上传打款凭证');
        	}
            $data['pay_price'] = Dever::input('pay_price');
        }

        $where['mobile'] = $data['mobile'];
        $where['role'] = $data['role']['id'];
        $where['area'] = $data['area'];

        $info = Dever::db('agent/order')->find($where);
        if ($info && $info['status'] != 5) {
        	Dever::alert('请勿重复提交');
        }

        $data['role'] = $where['role'];

        $data['id'] = Dever::db('agent/order')->insert($data);

        if ($data['id']) {
        	$result = array();
        	$result['msg'] = '您的申请已经提交完成，请耐心等待，我们将在工作日8小时内为您通过审核，非常感谢。';
        	if ($data['pay_type'] == 3) {
        		$result['bank_msg'] = '您需要使用银行卡：所有人（'.$data['name'].'），账号（'.$data['bank_card'].'），转账至如下账户（'.$data['price_str'].'）:';
        		$config = Dever::db('setting/base')->find();
        		$result['bank_user'] = $config['bank_user'];
        		$result['bank_name'] = $config['bank_name'];
        		$result['bank_card'] = $config['bank_card'];
        	} elseif ($data['pay_type'] == 1) {
        		# 在线支付 生成支付信息
        		$result['pay'] = $this->pay($data);
        	}

        	return $result;
        } else {
            Dever::alert('登录失败');
        }
    }

    /**
     * 发起在线支付
     *
     * @return mixed
     */
    public function pay($data)
    {
        $param = array
        (
            'project_id' => 2,
            'channel_id' => 1,
            'system_source' => 5,
            'account_id' => 7,
            'uid' => $data['id'],
            'name' => $data['name'],
            'openid' => $data['openid'],
            'cash' => $data['price'],
            'product_id' => $data['id'],
            'order_id' => $data['order_num'],
            'refer' => Dever::input('refer'),
        );

        $receipt = Dever::input('receipt');
        if ($receipt) {
            $param['receipt'] = $receipt;
        }

        $result = Dever::load('pay/api.pay', $param);
        $result['order_num'] = $data['order_num'];
        $result['order_id'] = $data['id'];

        return $result;
    }

    /**
     * 生成订单号
     *
     * @return mixed
     */
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('D');
        $state = Dever::db('agent/order')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }

    # 支付成功回调 安全加密 设置token
    public function success_secure_api_token()
    {
        $project_id = Dever::input('pay_project_id');
        $info = Dever::db('pay/project')->one($project_id);
        if ($info) {
            return $info['key'];
        }
        return 'agent_buy_dever_2020';
    }

    # 支付成功回调 安全加密
    public function success_secure_api($param = array())
    {
        $this->success($param);
    }

    # 支付成功回调
    public function success($param = array())
    {
        $send = $param ? $param : Dever::preInput('pay_');
        $product_id = $send['pay_product_id'];
        $order_id = $send['pay_order_id'];
        $status = $send['pay_status'];
        $cash = $send['pay_cash'];
        $msg = $send['pay_msg'];

        $order = Dever::db('shop/buy_order')->one(array('id' => $product_id, 'time' => time()));
        if ($order && $order['status'] == 1 && $status == 2) {
            $update['status'] = 2;
            $update['where_id'] = $order['id'];
            Dever::db('shop/buy_order')->update($update);

            # 生成结算单
            Dever::load('cash/lib/order')->up($order, 1, 1);
        }

        return 'ok';
    }

    /**
     * 获取支付金额
     *
     * @return mixed
     */
    public function getPrice($member = false)
    {
        if ($member && !is_array($member)) {
            $member = Dever::db('agent/member')->find($member);
        }
        
    	$data['role'] = Dever::input('role');
        if (!$data['role']) {
            Dever::alert('代理类型不能为空');
        }
        $data['role'] = Dever::db('setting/role')->find($data['role']);
        if (!$data['role']) {
        	Dever::alert('代理类型不正确');
        }
        if ($data['role']['isbuy'] != 1) {
            Dever::alert('代理类型不正确');
        }
        $data['area'] = Dever::input('area');
        if ($data['role']['type'] == 2 && !$data['area']) {
            Dever::alert('代理区域不能为空');
        }

        # 计算支付金额
        if ($data['role']['type'] == 1) {
        	$data['price'] = $data['role']['price'];
        } elseif ($data['role']['type'] == 2) {
        	$data['price'] = Dever::load('area/api')->getPrice($data['role']['area'], $data['area']);
        	if ($data['price'] <= 0) {
        		Dever::alert('代理区域的代理价格为空，请联系客服');
        	}
        } else {
        	Dever::alert('代理类型不正确');
        }

        if ($data['price'] >= 10000) {
            $data['price_str'] = ($data['price']/10000) . '万元';
        } else {
            $data['price_str'] = $data['price'] . '元';
        }

        if (is_int($data['price'])) {
            $data['price_str'] .= '整';
        }
        $data['order_type'] = 1;
        if ($member) {
            $area = Dever::db('agent/member_area')->getOne(array('mid' => $member['id'], 'type' => 1));
            if ($area && $area['price'] > 0 && $data['price'] > $area['price']) {
                $data['price'] -= $area['price'];
                $data['order_type'] = 2;
            } else {
                Dever::alert('代理区域无法升级');
            }
        }

        $data['f_price'] = $data['price'];

        return $data;
    }


    # 展示订单详情
    public function show()
    {
        $id = Dever::input('order_id');

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

        $info = Dever::db('agent/order')->one($id);

        $process = Dever::db('agent/order_process')->getData(array('order_id' => $id));
        $process_status = Dever::db('agent/order_process')->config['status'];

        //print_r($process);die;
        $role = Dever::db('setting/role')->one($info['role']);
        $area = Dever::load("area/api.string", $info['area']);

        $status = $config['status'][$info['status']];

        $member = array();
        if ($info['mid'] > 0) {
            $member = Dever::db('agent/member')->find($info['mid']);
        }
        
        $parent_member = array();
        if ($info['parent_mid'] > 0) {
            $parent_member = Dever::db('agent/member')->find($info['parent_mid']);
        }

        $cdate = date('Y-m-d H:i', $info['cdate']);

        if ($info['fdate']) {
            $fdate = date('Y-m-d H:i', $info['fdate']);
        } else {
            $fdate = '';
        }

        $result = array();

        $status_value = array();

        if ($process) {
            foreach ($process as $k => $v) {
                $v['cdate'] = date('Y-m-d H:i', $v['cdate']);
                $status_value[] = array($process_status[$v['status']], $v['cdate']);
            }
        }

        $result[$info['order_num']] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('订单状态', $status),
                    array('申请日期', $cdate),
                ),
                $status_value,
            )
        );

        $idcard_pic = '<a href="'.$info['idcard_front'].'" target="_blank"><img src="'.$info['idcard_front'].'" width="150" /></a>';
        if ($info['idcard_back']) {
            $idcard_pic .= '&nbsp;&nbsp;<a href="'.$info['idcard_back'].'" target="_blank"><img src="'.$info['idcard_back'].'" width="150" /></a>';
        }
        $sign_pic = '<a href="'.$info['sign'].'" target="_blank"><img src="'.$info['sign'].'" width="150" /></a>';

        $pay_pic = '';
        if ($info['pay_pic']) {
            $pay_pic = '&nbsp;&nbsp;<a href="'.$info['pay_pic'].'" target="_blank"><img src="'.$info['pay_pic'].'" width="150" /></a>';
        }

        $result['账号信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('类型', $config['type'][$info['type']]),
                    array('姓名', $info['name']),
                    array('手机号', $info['mobile']),
                ),

                array
                (
                    array('代理角色', $role['name']),
                    array('代理区域', $area),
                    array('注意', '赠送区域无权益'),
                ),

                array
                (
                    array('身份证号码', $info['idcard']),
                    array('身份证照片', $idcard_pic),
                ),
            ),
        );

        if ($info['type'] == 2) {
            $company_pic = '&nbsp;&nbsp;<a href="'.$info['company_pic'].'" target="_blank"><img src="'.$info['company_pic'].'" width="150" /></a>';
            $result['账号信息']['content'][] = array
            (
                array('企业名称', $info['company_name']),
                array('营业执照号码', $info['company_number']),
                array('营业执照照片', $company_pic),
            );
        }

        $result['账号信息']['content'][] = array
        (
            array('代理商签名', $sign_pic),
        );

        $cstatus = '';
        if ($role['soft'] && $role['soft'] && $info['price'] > 0 && $info['status'] > 1) {
            $cstatus = ',cstatus';
            //$soft = '【<a href="" style="color:blue">拆分代理费</a>】';
        }

        $result['支付信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('支付方式', $config['pay_type'][$info['pay_type']]),
                    array('支付状态', $config['pay_status'][$info['pay_status']]),
                    array('付款备注', $info['desc']),
                ),

                array
                (
                    array('应付金额', '¥' . $info['f_price'] . '元'),
                    array('实付金额', '¥' . $info['price'] . '元'),
                ),

                array
                (
                    array('代理费', '¥' . $info['agent_cash'] . '元'),
                    array('软件服务费', '¥' . $info['soft_cash'] . '元'),
                ),

                array
                (
                    array('支付凭证', $pay_pic),
                ),
            ),
        );

        $button = array();

        if ($info['status'] == 1) {
            $purl = Dever::url('project/database/update?project=agent&table=order_process&order_id='.$info['id'].'&status=1&col=order_id,status,audit,desc', 'manage');
            if (isset($process[1]) && $process[1]) {
                $purl .= '&where_id=' . $process[1]['id'];
            }
            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '业务初审',
            );
        } elseif ($info['status'] == 2) {
            # 根据价值获取名额
            $prize = Dever::db('setting/prize')->find(array('type' => 1, 'price' => $info['price']));
            if ($prize) {
                $member_goods = Dever::db('agent/member_goods')->getOne(array('mid' => $info['mid'], 'type' => 3, 'type_id' => $prize['id']));
                if ($member_goods && $member_goods['num'] > 0) {
                    # 有名额
                    $cstatus = ',prize_type';
                }
            }
            $purl = Dever::url('project/database/update?project=agent&table=order_process&order_id='.$info['id'].'&status=2&col=order_id,status,audit,price,desc'.$cstatus.'&price='.$info['price'], 'manage');
            if (isset($process[2]) && $process[2]) {
                $purl .= '&where_id=' . $process[2]['id'];
            }
            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '财务终审',
            );
            
        } elseif ($info['status'] == 3) {
            $purl = Dever::url('project/database/update?project=agent&table=order_process&order_id='.$info['id'].'&status=3&col=order_id,status,type', 'manage');
            if (isset($process[3]) && $process[3]) {
                $purl .= '&where_id=' . $process[3]['id'];
            }
            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '确认完成',
            );
        }

        if (($info['status'] == 3 || $info['status'] == 4) && $info['agent_cash'] > 0) {

            if (isset($process[3]) && $process[3]) {
                $process[3]['type'] = explode(',', $process[3]['type']);
            }

            if ($parent_member) {
                # 这里要获取上级信息
                $table['head'] = array('名称', '角色', '级别', '分润比例', '分润（元）');
                $table['body'] = array();

                $parents[0] = array('uid' => $parent_member['id'], 'level' => 0);
                $parents = array_merge($parents, Dever::load('invite/api')->getParentAll($parent_member['id'], 20));

                $parent_name = '邀请人：'.$parent_member['name'].'，'.$parent_member['mobile'].'&nbsp;&nbsp;邀请人角色：';

                $parent_per = 0;
                foreach ($parents as $k => $v) {
                    $parent_info = Dever::db('agent/member')->find($v['uid']);
                    if ($parent_info && $parent_info['status'] == 2) {
                        $child_num = 0;
                        $parent_role = Dever::db('setting/role')->find($parent_info['role']);
                        if ($parent_info['level_id'] > 0) {
                            $parent_level = Dever::db('setting/level')->find($parent_info['level_id']);
                        } else {
                            $parent_level = array('id' => -1, 'name' => '普通', 'level' => 0, 'num' => 1);
                        }
                        $cash = 0;

                        $order_cash = Dever::db('agent/order_cash')->getData(array('order_id' => $info['id'], 'parent_mid' => $v['uid'], 'type' => 2));

                        if ($order_cash) {
                            $cash = $order_cash['cash'];
                            $parent_level['value'] = $order_cash['per'];
                        } else {
                            # 根据团队业绩获取等级
                            # 获取当前用户的团队业绩
                            if (!$parent_info['group_sell']) {
                                $parent_info['group_sell'] = 0;
                            }
                            $sell = $parent_info['group_sell'] + $info['agent_cash'];

                            $level = Dever::db('setting/level')->getOne(array('target' => ($sell/10000)));
                            if ($level && $level['level'] > $parent_level['level']) {
                                # 升级
                                $old = $parent_level;
                                $parent_info['level_id'] = $level['id'];
                                $parent_level = $level;
                                $parent_level['name'] = $old['name'] . '->' . $level['name'];
                            }

                            # 获取直推人数
                            $child_num = Dever::load('invite/api')->getChildNum($v['uid'], 1) + 1;

                            if ($parent_level['id'] > 0 && $child_num >= $parent_level['num']) {
                                # 达到要求，开始团队分润
                                if ($parent_per > 0) {
                                    $parent_level['value'] = $parent_level['value'] - $parent_per;
                                }
                                if ($parent_level['value'] > 0) {
                                    $cash += $info['agent_cash'] * ($parent_level['value']/100);
                                    $parent_per = $parent_level['value'];
                                }
                            }
                        }

                        if ($k == 0) {
                            $order_cash = Dever::db('agent/order_cash')->getData(array('order_id' => $info['id'], 'parent_mid' => $v['uid'], 'type' => 1));
                            $parent_name .= $parent_role['name'] . '（'.$parent_level['name'].'）';
                            # 根据角色获取佣金 直推
                            if (!$order_cash) {
                                $reward = Dever::db('setting/role_reward')->find(array('role_id' => $parent_info['role'], 't_role_id' => $info['role']));
                                if ($reward) {
                                    $zcash = 0;
                                    $zper = 0;
                                    if ($reward['num'] == 1) {
                                        # 每推荐1个，获取直推佣金
                                        if ($reward['value_type'] == 1) {
                                            $zcash = $info['agent_cash'] * ($reward['value']/100);
                                            $zper = $reward['value'];
                                        } else {
                                            $zcash = $reward['value'];
                                            $zper = 0;
                                        }
                                    } elseif ($reward['num'] > 1) {
                                        # 每推荐多少个，获取直推佣金
                                        if (!$child_num) {
                                            $child_num = Dever::load('invite/api')->getChildNum($v['uid'], 1) + 1;
                                        }
                                        if ($child_num > $reward['num'] && $child_num % $reward['num'] == 0) {
                                                $zcash = $reward['value'];
                                                $zper = 0;
                                        }
                                    }
                                    if ($reward['is_give'] == 1) {
                                        $give = Dever::array_decode($reward['give']);
                                        if (!$child_num) {
                                            $child_num = Dever::load('invite/api')->getChildNum($v['uid'], 1) + 1;
                                        }
                                        foreach ($give as $gv) {
                                            if (isset($gv['num']) && $child_num > $gv['num'] && $child_num % $gv['num'] == 0) {
                                                $prize_log = Dever::db('agent/member_prize')->find(array('mid' => $v['uid'], 'prize_id' => $gv['type'], 'child_num' => $child_num));
                                                if (!$prize_log) {
                                                    $prize = Dever::db('setting/prize')->find($gv['type']);
                                                    $zper .= '-' . $prize['id'] . '-' . $child_num;
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if ($zcash > 0) {
                                        if (isset($prize)) {
                                            $parent_name .= '&nbsp;&nbsp;赠送名额：'.$prize['name'];
                                        }
                                        if ($zper > 0) {
                                            $parent_name .= '&nbsp;&nbsp;直推佣金比例：'.$zper.'%';
                                        }
                                        $parent_name .= '&nbsp;&nbsp;直推佣金：<a style="color:blue" class="set_cash" data-param="1-'.$info['id'].'-'.$parent_info['id'].'-'.$zcash.'-'.$parent_level['id'].'-'.$zper.'">' . $zcash . '</a>元';
                                    }
                                }
                            } else {
                                $zcash = $order_cash['cash'];
                                if ($order_cash['prize_id'] && $order_cash['prize_id'] > 0) {
                                    $prize = Dever::db('setting/prize')->find($order_cash['prize_id']);
                                    if ($prize) {
                                        $zper .= '-' . $prize['id'];
                                        $parent_name .= '&nbsp;&nbsp;赠送名额：'.$prize['name'];
                                    }
                                }
                                if ($order_cash['per'] > 0) {
                                    $parent_name .= '&nbsp;&nbsp;直推佣金比例：'.$order_cash['per'].'%';
                                }
                                $parent_name .= '&nbsp;&nbsp;直推佣金：<a style="color:blue">' . $zcash . '</a>元';
                            }
                        }
                        $cash = '<a style="color:blue" class="set_cash" data-param="2-'.$info['id'].'-'.$parent_info['id'].'-'.$cash.'-'.$parent_level['id'].'-'.$parent_level['value'].'">' . $cash . '</a>';

                        $table['body'][] = array($parent_info['name'], $parent_role['name'], $parent_level['name'], $parent_level['value'] . '%', $cash);
                    }
                }

                if (isset($process[3]['type']) && is_array($process[3]['type']) && in_array(3, $process[3]['type'])) {
                    $parent_title = '（已发放分润）';
                    $parent_button = array();
                } else {
                    $parent_title = '（未发放分润）';
                    /*
                    $parent_button[] = array
                    (
                        'type' => 'link',
                        'link' => Dever::url('lib/order_set.home?order_id='.$info['id'].'&role_id=' . $info['role'], 'agent'),
                        'name' => '修改分润金额'
                    );*/

                    $furl = Dever::url('lib/manage.set_commit?json=1&type=3&order_id='.$info['id'], 'agent');
                    $parent_button[] = array
                    (
                        'type' => 'click',
                        'content' => 'setCash()',
                        'name' => '确认发放分润<script>function setCash(){layer.confirm(\'确定进行此项操作吗？\', function(){var cash = [];$(\'.set_cash\').each(function(){cash.push($(this).attr(\'data-param\'));});$.post(\''.$furl.'\',{param:cash.join(",")}, function(t){t=JSON.parse(t);showYes(t)})})}</script>'
                    );
                }
                
                $result['分润信息' . $parent_title] = array
                (
                    'type' => 'table',
                    'content' => $table,
                    'head' => array('name' => '<span>' . $parent_name . '</span>', 'btn' => $parent_button),
                );
            }

            $goods = Dever::db('agent/order_goods')->getData(array('order_id' => $info['id']));
            if (!$goods) {
                $goods = Dever::db('setting/role_goods')->getData(array('role_id' => $info['role'], 'give' => $info['order_type']));
            }

            if (isset($process[3]['type']) && is_array($process[3]['type']) && in_array(2, $process[3]['type'])) {
                $process_status = 1;
                $process_title = '（已发放权益）';
                $process_button = array();
            } else {
                $process_status = 2;
                $process_title = '（未发放权益）';
                
                $process_button[] = array(
                    'type' => 'link',
                    'link' => Dever::url('lib/order_set.home?order_id='.$info['id'].'&role_id=' . $info['role'], 'agent'),
                    'name' => '修改权益'
                );
                $process_button[] = array(
                    'type' => 'action',
                    'link' => Dever::url('lib/manage.set_commit?json=1&type=2&order_id='.$info['id'], 'agent'),
                    'name' => '确认发放权益'
                );
            }
            
            if ($goods) {
                foreach ($goods 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 = '无';
                        }
                        $del = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13', 'factory');

                        $status = '<a href="javascript:;" onclick="audit('.$v['id'].', 2, \''.$del.'\')" class="layui-btn">删除</a>';

                        $d = array
                        (
                            'pic' => $goods_info['cover'],
                            'name' => $goods_info['name'],
                            'sku' => $sku,
                            'price' => $goods_info['price'],
                            'num' => $v['num'],
                            //'status' => $status,
                        );
                    } elseif ($v['type'] == 2) {
                        $card = Dever::db('card/info')->find($v['type_id']);

                        $d = array
                        (
                            //'pic' => $goods_info['cover'],
                            'name' => $card['name'],
                            'sku' => '',
                            'price' => $card['price'],
                            'num' => $v['num'],
                            //'status' => $status,
                        );
                    } elseif ($v['type'] == 3) {
                        $prize = Dever::db('setting/role_goods')->config['set']['prize'];
                        $d = array
                        (
                            //'pic' => $goods_info['cover'],
                            'name' => $prize[$v['type_id']],
                            'sku' => '',
                            'price' => 0,
                            'num' => $v['num'],
                            //'status' => $status,
                        );
                    }
                    
                    $body[] = $d;
                }

                $result['权益信息' . $process_title] = array
                (
                    'type' => 'list',
                    'head' => array('name' => '权益列表：', 'btn' => $process_button),
                    'content' => $body,
                );
            }

            $body = array();
            $agreement_type = Dever::db('setting/agreement')->config['config_type'];
            if (isset($process[3]['type']) && is_array($process[3]['type']) && in_array(5, $process[3]['type'])) {
                $agreement_status = 1;
                $agreement_title = '（已生成合同）';
                $agreement_button = array();

                $agreement = Dever::db('agent/member_agreement')->getData(array('order_id' => $info['id']));
                foreach ($agreement as $k => $v) {
                    $agreement_info = Dever::db('setting/agreement')->find($v['agreement_id']);

                    $body[] = array($agreement_type[$agreement_info['type']], Dever::load('agent/lib/agreement')->name($agreement_info, $v, $role, 2));
                }

            } else {
                $agreement_status = 2;
                $agreement_title = '（未生成合同）';
                
                $agreement_button[] = array(
                    'type' => 'action',
                    'link' => Dever::url('lib/manage.set_commit?json=1&type=5&order_id='.$info['id'], 'agent'),
                    'name' => '生成代理合同'
                );

                $agreement = Dever::db('setting/agreement')->getData();
                $body[] = array($agreement_type[$agreement[1]['type']], Dever::load('agent/lib/agreement')->name($agreement[1], $info, $role, 1));
                if ($info['order_type'] == 2) {
                    # 升级的话，增加解除合同
                    $body[] = array($agreement_type[$agreement[2]['type']], Dever::load('agent/lib/agreement')->name($agreement[2], $info, $role, 1));
                } else {
                    $body[] = array($agreement_type[$agreement[4]['type']], Dever::load('agent/lib/agreement')->name($agreement[4], $info, $role, 1));
                }
            }

            $result['代理合同' . $agreement_title] = array
            (
                'type' => 'info',
                'head' => array('name' => '&nbsp;', 'btn' => $agreement_button),
                'content' => array
                (
                    $body,
                )
            );
        }
        
        
        $head = array
        (
            'name' => '基本信息',
            'btn' => $button,
        );
        $html = Dever::show($head, $result);

        return $html;
    }
}