<?php

$role = function()
{
	$array = array();
	$data = Dever::load('setting/role-state');
	if($data)
	{
		$array += $data;
	}
	return $array;
};

$status = array
(
    1 => '未认证',
    2 => '正常',
    3 => '驳回',
    4 => '已删除',
    5 => '已禁用',
);

$type = array
(
    1 => '个人',
    2 => '公司',
);

$source_id = function()
{
    $array = array
    (
        -1 => array
        (
            'id' => '-1',
            'name' => '无来源',
        ),
    );
    $data = Dever::load('setting/source-state');
    if($data)
    {
        $array += $data;
    }
    return $array;
};

$level = function()
{
	$array = array
	(
		-1 => array
		(
			'id' => -1,
			'name' => '普通',
		),
	);
	$data = Dever::load('setting/level-state');
	if($data)
	{
		$array += $data;
	}
	return $array;
};

$info = '';
$button = array();
$getInfo = Dever::input('getInfo');
$pmid = Dever::input('search_option_pmid');
$parent_mid = Dever::input('search_option_parent_mid');
$search_api_parent_mid = Dever::input('search_api_parent_mid');
if ($search_api_parent_mid && $getInfo != 1) {
    Dever::setInput('search_api_parent_mid', 0);
    $parent = Dever::db('agent/member')->find(array('mobile' => $search_api_parent_mid));
    if ($parent) {
        $info = '上级' . Dever::load('agent/lib/member')->getInfoString($parent) . ', 以下为直推列表：';
    }
} elseif ($parent_mid && $parent_mid > 0 && $getInfo != 1) {
    $info = '上级' . Dever::load('agent/lib/member')->getInfoString($parent_mid) . ', 以下为直推列表：';
    $button = array
    (
        '返回上一页' => array('location', 'l=project/database/list&project=agent&table=member_area&page_type=1&mid=' . $parent_mid),
    );
}
return array
(
    # 表名
    'name' => 'member',
    # 显示给用户看的名称
    'lang' => '代理商管理',
    'order' => 100,
    'auto' => 1000000,
    'config_type' => $type,
    'config_status' => $status,
    'info' => $info,
    # 数据结构
    'struct' => array
    (
    
        'id'        => array
        (
            'type'      => 'int-11',
            'name'      => 'ID',
            'default'   => '',
            'desc'      => '',
            'match'     => 'is_numeric',
            'search'    => 'order',
            //'list'        => true,
        ),

        'name'      => array
        (
            'type'      => 'varchar-100',
            'name'      => '姓名',
            'default'   => '',
            'desc'      => '姓名',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => ($parent_mid || $pmid) ? 'hidden' : 'fulltext',
            'list'      => 'Dever::load("agent/lib/member.getOne", {id})',
        ),

        'rdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '认证时间',
            'match'     => 'option',
            'default'   => '0',
            'desc'      => '',
            'update'    => 'date',
            'callback'  => 'maketime',
            'list'        => '{rdate} > 0 ? date("Y-m-d H:i", {rdate}) : "无"',
        ),

        'avatar'		=> array
		(
			'type' 		=> 'varchar-150',
			'name' 		=> '头像',
			'default' 	=> '',
			'desc' 		=> '请选择头像',
			'match' 	=> 'option',
			'update'	=> 'image',
			'key' 		=> '1',
			'place'		=> '150',
		),

        'mobile'      => array
        (
            'type'      => 'bigint-11',
            'name'      => '手机号',
            'default'   => '',
            'desc'      => '请输入手机号',
            'match'     => Dever::rule('mobile'),
            'update'    => 'text',
            'search'    => ($parent_mid || $pmid) ? 'hidden' : 'fulltext',
            //'list'      => true,
        ),

        'email'     => array
        (
            'type'      => 'varchar-150',
            'name'      => '邮箱',
            'default'   => '',
            'desc'      => '请输入邮箱',
            'match'     => 'option',
            'update'    => 'text',
        ),

        'username'      => array
        (
            'type'      => 'varchar-100',
            'name'      => '昵称',
            'default'   => '',
            'desc'      => '昵称',
            'match'     => 'is_string',
            'update'    => 'text',
        ),

        'birthday'      => array
        (
            'type'      => 'int-11',
            'name'      => '生日',
            'default'   => '',
            'match'     => 'option',
            'desc'      => '生日',
            'update'    => 'time',
            'callback'  => 'maketime',
        ),

        'password'		=> array
		(
			'type' 		=> 'varchar-50',
			'name' 		=> '密码',
			'default' 	=> '',
			'desc' 		=> '请输入密码',
			'match' 	=> 'option',
			'update'	=> 'password',
			'callback'	=> 'sha1',
		),

        'parent_mid'      => array
        (
            'type'      => 'int-11',
            'name'      => '邀请人手机号',
            'default'   => '-1',
            'desc'      => '邀请人',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => $parent_mid ? 'hidden' : array
            (
                'api' => 'agent/member-select',
                'col' => 'mobile',
                'result' => 'id',
            ),
            'list_name' => '邀请人',
            'list'      => ($parent_mid || $pmid) ? false : 'Dever::load("agent/lib/member.getOne", {parent_mid})',
        ),

        'source_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '注册来源',
            'default'   => '-1',
            'desc'      => '注册来源',
            'match'     => 'is_numeric',
            'search'    => 'select',
            'update'    => 'checkbox',
            'option'    => $source_id,
            'list'      => true,
        ),

        'pmid'      => array
        (
            'type'      => 'int-11',
            'name'      => '邀请人id-废弃无用处',
            'default'   => '-1',
            'desc'      => '邀请人',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => 'hidden',
        ),

        'code'        => array
        (
            'type'      => 'varchar-50',
            'name'      => '邀请码',
            'default'   => '',
            'desc'      => '邀请码',
            'match'     => 'option',
            //'update'    => 'text',
            'list'      => ($parent_mid || $pmid) ? false : 'Dever::load("invite/api.code", {id})',
        ),

        'role'      => array
        (
            'type'      => 'int-11',
            'name'      => '代理角色',
            'default'   => '',
            'desc'      => '代理角色',
            'match'     => 'is_numeric',
            'search'	=> 'select',
            'update'    => 'select',
            'option'    => $role,
            //'list'      => true,
        ),

        'level_id'		=> array
		(
			'type' 		=> 'int-11',
			'name' 		=> '代理商等级',
			'default' 	=> '-1',
			'desc' 		=> '代理商等级',
			'match' 	=> 'is_numeric',
			'search'	=> 'select',
			'update'	=> 'select',
			'option'	=> $level,
			//'list'		=> true,
		),

		'area'       => array
        (
            'type'      => 'varchar-500',
            'name'      => '区域',
            'default'   => '',
            'desc'      => '区域',
            'match'     => 'option',
            'search'    => 'linkage',
            'update'    => 'linkage',
            'option'    => Dever::url('api.get?level_total=4', 'area'),
            'list'      => 'Dever::load("area/api.string", "{area}")',
        ),

        'shop_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '所属门店',
            'default'   => '',
            'desc'      => '所属门店',
            'match'     => 'is_numeric',
            'update'    => 'select',
            'update_search' => 'shop/lib/manage.search',
            'list'      => ($parent_mid || $pmid) ? false : '{shop_id} > 0 ? Dever::load("shop/info-one#name", {shop_id}) : "无"',
        ),

        'type'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '类型',
            'default'   => '1',
            'desc'      => '类型',
            'match'     => 'is_numeric',
            'option'    => $type,
            'update'    => 'radio',
            'control'   => 'type',
        ),

        'sign'     => array
        (
            'type'      => 'varchar-150',
            'name'      => '手写签名',
            'default'   => '',
            'desc'      => '手写签名',
            'match'     => 'option',
            'update'    => 'image',
            'key'       => '7',
        ),

        'idcard'      => array
        (
            'type'      => 'varchar-32',
            'name'      => '身份证号码',
            'default'   => '',
            'desc'      => '身份证号码',
            'match'     => 'is_string',
            'update'    => 'text',
        ),

        'idcard_front'     => array
        (
            'type'      => 'varchar-150',
            'name'      => '身份证正面',
            'default'   => '',
            'desc'      => '身份证正面',
            'match'     => 'option',
            'update'    => 'image',
            'key'       => '8',
            'place'     => '660*660',
        ),

        'idcard_back'     => array
        (
            'type'      => 'varchar-150',
            'name'      => '身份证背面',
            'default'   => '',
            'desc'      => '身份证背面',
            'match'     => 'option',
            'update'    => 'image',
            'key'       => '8',
            'place'     => '660*660',
        ),

        'company_name'      => array
        (
            'type'      => 'varchar-100',
            'name'      => '公司名称',
            'default'   => '',
            'desc'      => '公司名称',
            'match'     => 'is_string',
            'update'    => 'text',
            //'search'    => 'fulltext',
            //'list'      => true,
            'show'      => 'type=2',
        ),

        'company_pic'      => array
        (
            'type'      => 'varchar-150',
            'name'      => '营业执照',
            'default'   => '',
            'desc'      => '营业执照',
            'match'     => 'option',
            'update'    => 'image',
            'key'       => '8',
            'place'     => '660*660',
            'show'      => 'type=2',
        ),

        'company_number'      => array
        (
            'type'      => 'varchar-80',
            'name'      => '营业执照号码',
            'default'   => '',
            'desc'      => '营业执照号码',
            'match'     => 'is_string',
            'update'    => 'text',
            'show'      => 'type=2',
        ),

        'address'      => array
        (
            'type'      => 'varchar-800',
            'name'      => '地址',
            'default'   => '',
            'desc'      => '地址',
            'match'     => 'is_string',
            'update'    => 'text',
        ),

        'cash'      => array
        (
            'type'      => 'float-11,2',
            'name'      => '余额',
            'default'   => '0',
            'desc'      => '余额',
            'match'     => 'is_numeric',
            'update'    => 'text',
            'list'      => true,
        ),

        'sell'      => array
        (
            'type'      => 'float-11,2',
            'name'      => '直推业绩',
            'default'   => '0',
            'desc'      => '销售业绩',
            'match'     => 'is_numeric',
            'update'    => 'text',
            'list'      => ($parent_mid || $pmid) ? true : false,
        ),

        'group_sell'      => array
        (
            'type'      => 'float-11,2',
            'name'      => '团队业绩',
            'default'   => '0',
            'desc'      => '团队销售业绩',
            'match'     => 'is_numeric',
            'update'    => 'text',
            'list'      => ($parent_mid || $pmid) ? true : false,
        ),

        'status'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '状态',
            'default'   => '1',
            'desc'      => '状态',
            'match'     => 'is_numeric',
            'option'    => $status,
            'search'    => 'select',
            'list'      => true,
        ),

        'state'     => array
        (
            'type'      => 'tinyint-1',
            'name'      => '状态',
            'default'   => '1',
            'desc'      => '请选择状态',
            'match'     => 'is_numeric',
        ),
        
        'cdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '加入日期',
            'match'     => array('is_numeric', time()),
            'desc'      => '',
            # 只有insert时才生效
            //'insert'    => true,
            //'search'    => 'date',
            //'list'      => 'date("Y-m-d H:i:s", {cdate})',
        ),
    ),

    'manage' => array
    (
        'insert' => false,
        'delete' => false,
        'edit' => false,
        'button' => $button,

        'list_button' => array
        (
            'list' => array('查看详情', '"member_area&mid={id}&page_type=1"'),

            //'edit' => array('退款', 'status,tk_time,tk_pic,tk_desc,tk_admin,tk_status', '{status} == 6'),
            //'delete' => array('删除', '', '{status} == 1'),
        ),
    ),

    'request' => array
    (
        'getInfo' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'status' => 2,
                'state' => 1,
            ),
            'order' => array('id' => 'desc'),
            'type' => 'one',
            'col' => '*',
        ),

        'getData' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'idcard' => 'yes',
                'id' => array('yes', '!='),
                'status' => 2,
                'state' => 1,
            ),
            'order' => array('id' => 'desc'),
            'type' => 'all',
            'page' => array(20, 'list'),
            'col' => '*',
        ),

        'upCash' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'cash' => array('yes', '+='),
            ),
        ),

        'upSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'cash' => array('yes', '+='),
                'upcol' => array('yes-sell', '+='),
                'level_id' => 'yes',
            ),
        ),

        'upGroupSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'cash' => array('yes', '+='),
                'upcol' => array('yes-group_sell', '+='),
                'level_id' => 'yes',
            ),
        ),

        'upGroupSellOne' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'group_sell' => array('yes', '+='),
            ),
        ),
    ),
);
