<?php

$type = Dever::db('setting/role_goods')->config['set']['type'];

$button = array();
$info = '';

$mid = Dever::input('search_option_mid');
$search_api_mid = Dever::input('search_api_mid');
if ($search_api_mid) {
    $member = Dever::db('agent/member')->find(array('mobile' => $search_api_mid));
    $button = array
    (
        '手动调整权益' => array('location', 'agent/lib/member_set.home?mid=' . $member['id'] . '&search_api_mid=' . $search_api_mid),
        '权益操作记录' => array('location', 'l=project/database/list&project=agent&table=member_goods_log&search_option_mid='.$member['id'].'&search_api_mid=' . $search_api_mid),
    );
    $info = Dever::load('agent/lib/member')->getInfoString($member);
    $mid = false;
} else {
    if ($mid) {
        $button = array
        (
            '返回上一页' => array('location', 'l=project/database/list&project=agent&table=member_area&page_type=1&mid=' . $mid),
            '手动调整权益' => array('location', 'agent/lib/member_set.home?mid=' . $mid),
            '权益操作记录' => array('location', 'l=project/database/list&project=agent&table=member_goods_log&search_option_mid=' . $mid),
        );
        $info = Dever::load('agent/lib/member')->getInfoString($mid);
    }
}

return array
(
    # 表名
    'name' => 'member_goods',
    # 显示给用户看的名称
    'lang' => '权益管理',
    'order' => 81,
    'info' => $info,
    # 数据结构
    'struct' => array
    (
    
        'id'        => array
        (
            'type'      => 'int-11',
            'name'      => 'ID',
            'default'   => '',
            'desc'      => '',
            'match'     => 'is_numeric',
            'search'    => 'order',
            'update'    => 'hidden',
            //'list'        => true,
        ),

        'mid'      => array
        (
            'type'      => 'int-11',
            'name'      => '手机号',
            'default'   => '-1',
            'desc'      => '代理商',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => $mid ? 'hidden' : array
            (
                'api' => 'agent/member-find',
                'col' => 'mobile',
                'result' => 'id',
            ),
            'list_name' => '代理商',
            //'list'      => $mid ? false : 'Dever::load("agent/member-find#mobile", {mid})',
            'list'      => 'Dever::load("agent/lib/member.getOne", {mid})',
            'list_order' => 2,
        ),

        'agent-member-name'=> array
        (
            'name'      => '姓名',
            'default'   => '',
            'desc'      => '姓名',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('mid', 'id'),
            //'list'      => $mid ? false : true,
            //'list_order' => 1,
        ),

        'agent-member-role'=> array
        (
            'name'      => '代理角色',
            'default'   => '',
            'desc'      => '代理角色',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('mid', 'id'),
            //'list'      => 'Dever::load("setting/role-find#name", "{agent-member-role}")',
            //'list_order' => 3,
        ),

        'type'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '类型',
            'default'   => '1',
            'desc'      => '类型',
            'match'     => 'is_numeric',
            'update'  	=> 'radio',
            'option'    => $type,
            'search'    => 'select',
            'list'      => true,
            'list_order' => 4,
        ),

        'type_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '类型id',
            'default'   => '-1',
            'desc'      => '类型id',
            'match'     => 'is_numeric',
            'list_name' => '权益名称',
            'list'      => 'Dever::load("agent/lib/manage.getGoods#name", "{type}", "{type_id}", "{sku_id}")',
            'list_order' => 5,
        ),

        'sku_id'      => array
        (
            'type'      => 'int-11',
            'name'      => 'sku_id',
            'default'   => '-1',
            'desc'      => 'sku_id',
            'match'     => 'is_numeric',
        ),

        'total_num'        => array
        (
            'type'      => 'int-11',
            'name'      => '总数量',
            'default'   => '0',
            'desc'      => '总数量',
            'match'     => 'is_numeric',
            'list_name' => '数量',
            'list'      => '{total_num} - {sell_num}',
            'list_order' => 6,
        ),

        'sell_num'      => array
        (
            'type'      => 'int-11',
            'name'      => '已兑换数量',
            'default'   => '0',
            'desc'      => '已兑换数量',
            'match'     => 'option',
            'list_name' => '单价价值（元）',
            'list'      => 'Dever::load("agent/lib/manage.getGoods#price", "{type}", "{type_id}", "{sku_id}")',
            'list_order' => 7,
        ),

        '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
    (
        'edit' => false,
        'insert' => false,
        'delete' => false,
        'button' => $button,
    ),

    'request' => array
    (
        'getData' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'order' => array('type' => 'asc', 'id' => 'desc'),
            'type' => 'all',
            'col' => '*, total_num-sell_num as num',
        ),

        'getOne' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            'col' => '*, total_num-sell_num as num',
        ),

        'findData' => array
        (
            # 匹配的正则或函数 选填项
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            'col' => '*, total_num-sell_num as num',
        ),

        # 更新售出量
        'incSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'sell_num' => array('yes', '+='),
            ),
        ),

        # 减少售出量
        'decSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'sell_num' => array('yes', '-='),
            ),
        ),

        # 更新总库存
        'updateTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '+='),
            ),
        ),

        # 增加总库存
        'incTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '+='),
            ),
        ),

        # 减少总库存
        'decTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '-='),
            ),
        ),
    ),
);
