<?php
$type = array
(
    1 => '正常商户',
    2 => '平台商户',
);
$status = array
(
    1 => '已签约',
    2 => '未签约',
);
$project = function()
{
	return Dever::db('pay/yspay_project')->state();
};

$account_id = Dever::input('search_option_account_id');
$yspay_type = 1;
if ($account_id) {
    $yspay = Dever::db('pay/yspay')->find(array('account_id' => $account_id));
    $yspay_type = $yspay['type'];
}
$set = Dever::input('set');
$list_button = array
(
    'edit' => array('编辑'),

    'add' => array('签约资料', '"yspay_sign&search_option_merchant_id={id}&search_option_account_id={account_id}&where_id={id}&oper_table=account&oper_save_table=yspay_merchant"', '{status} == 2'),

    'oper' => array('签约提审', 'pay/yspay/sign.handle?sign_id={id}', '"{pay/yspay_sign-one#step}" == -1 && {status} == 2'),
    'fast' => array('签约打款', '"yspay_sign&where_id={id}&col=cash"', '"{pay/yspay_sign-one#step}" == 2 && {status} == 2'),
    'copy' => array('签约合同', 'pay/yspay/sign.getAgreement?sign_id={id}', '"{pay/yspay_sign-one#step}" == 4 && {status} == 2'),
);

if ($yspay_type == 2) {
    $list_button['list3'] = array('资金流水', '"yspay_cash&search_option_merchant_id={id}&search_option_account_id={account_id}&oper_table=yspay_merchant&top_table=account"', '{status} == 1');
}

if ($yspay_type == 1) {
    $list_button['list2'] = array('提现管理', '"yspay_tixian&search_option_merchant_id={id}&search_option_account_id={account_id}&oper_table=yspay_merchant&top_table=account"', '{status} == 1');
}

$list_button['list1'] = array('签约日志', '"yspay_sign_log&search_option_account_id={account_id}&search_option_merchant_id={id}&oper_table=yspay_merchant&top_table=account"');

return array
(
    # 表名
    'name' => 'yspay_merchant',
    # 显示给用户看的名称
    'lang' => '银联商户列表',
    # 后台菜单排序
    'order' => -100,
    //'check' => 'account_id,project_id,mid',
    'menu' => false,
    # 数据结构
    'struct' => array
    (
        'id'        => array
        (
            'type'      => 'int-11',
            'name'      => 'ID',
            'default'   => '',
            'desc'      => '',
            'match'     => 'is_numeric',
            'search'    => 'order',
            //'list'      => true,
            'order'     => 'desc',
        ),

        'account_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '账户id',
            'default'   => '',
            'desc'      => '账户id',
            'match'     => 'option',
            'update'    => 'hidden',
            'value'     => $account_id,
        ),

        'name'      => array
        (
            'type'      => 'varchar-300',
            'name'      => '商户名称',
            'default'   => '',
            'desc'      => '商户名称',
            'match'     => 'is_string',
            'update'    => $set ? false : 'text',
            'search'    => 'fulltext',
            'list'      => true,
        ),

        'type'       => array
        (
            'type'      => 'int-11',
            'name'      => '商户类型-平台商户将自动收取支付时未配置正常商户的金额，有多个平台商户时将自动随机抽取，平台商户不会分账',
            'default'   => '1',
            'desc'      => '商户类型',
            'match'     => 'option',
            'search'    => 'select',
            'update'    => $set ? false : 'radio',
            'option'    => $type,
            'list'      => true,
            'control'   => 'type',
        ),

        'project_id'       => array
        (
            'type'      => 'int-11',
            'name'      => '所属项目',
            'default'   => '',
            'desc'      => '所属项目',
            'match'     => 'option',
            'search'    => 'select',
            'update'    => $set ? false : 'radio',
            'control_url'	=> 'pay/yspay/merchant.selectRelate',
            'option'    => $project,
            //'list'      => true,
            'show'      => 'type=1',
        ),

        'relate_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '关联数据',
            'default'   => '-1',
            'desc'      => '关联数据',
            'match'     => 'option',
            'update'    => $set ? false : 'select',
            'show'      => 'type=1',
        ),

        'mid'      => array
        (
            'type'      => 'varchar-200',
            'name'      => '商户号',
            'default'   => '',
            'desc'      => '商户号',
            'match'     => 'option',
            'search'    => 'fulltext',
            'update'    => 'text',
            'list'		=> true,
        ),

        'merno'      => array
        (
            'type'      => 'varchar-200',
            'name'      => '企业用户号-清分时使用',
            'default'   => '',
            'desc'      => '企业用户号',
            'match'     => 'option',
            'update'    => 'text',
        ),

        'cash_per'		=> array
		(
			'type' 		=> 'int-11',
			'name' 		=> '平台分账百分比-如输入10，就是从总支付金额中分账10%，输入0则使用默认分账百分比，输入负数则不进行百分比分账',
			'default' 	=> '',
			'desc' 		=> '平台分账百分比',
			'match' 	=> 'option',
			//'update'	=> 'text',
		),

        'cash'      => array
        (
            'type'      => 'int-11',
            'name'      => '资金总额',
            'default'   => '0',
            'desc'      => '资金总额',
            'match'     => 'option',
            //'update'    => 'text',
            'list_name' => '分账总额 / 划付总额 / 提现总额',
            'list'      => $yspay_type == 1 ? false : 'Dever::number({fz_cash}/100, 2) . " / " . Dever::number({hf_cash}/100, 2) . " / " . Dever::number({hf_tx_cash}/100, 2)',
        ),

        'hf_cash'      => array
        (
            'type'      => 'int-11',
            'name'      => '划付总额-划付就是提现到商户自己账户的意思',
            'default'   => '0',
            'desc'      => '划付总额',
            'match'     => 'option',
            //'update'    => 'text',
            //'list'		=> 'Dever::number({hf_cash}/100, 2)',
        ),

        'hf_tx_cash'      => array
        (
            'type'      => 'int-11',
            'name'      => '划付提现总额',
            'default'   => '0',
            'desc'      => '划付提现总额',
            'match'     => 'option',
            //'update'    => 'text',
            //'list'        => 'Dever::number({hf_cash}/100, 2)',
        ),

        'fz_cash'      => array
        (
            'type'      => 'int-11',
            'name'      => '分账总额',
            'default'   => '0',
            'desc'      => '分账总额',
            'match'     => 'option',
            //'update'    => 'text',
            //'list'		=> 'Dever::number({fz_cash}/100, 2)',
        ),

        'yue'      => array
        (
            'type'      => 'int-11',
            'name'      => '账户余额',
            'default'   => '0',
            'desc'      => '账户余额',
            'match'     => 'option',
            //'update'    => 'text',
            'list_name' => '账户余额 / 提现总额',
            'list'		=> $yspay_type == 2 ? false : 'Dever::number({yue}/100, 2) . " / " . Dever::number({tx_cash}/100, 2)',
        ),

        'tx_cash'      => array
        (
            'type'      => 'int-11',
            'name'      => '提现总额',
            'default'   => '0',
            'desc'      => '划付总额',
            'match'     => 'option',
            //'update'    => 'text',
            //'list'		=> 'Dever::number({tx_cash}/100, 2)',
        ),

        'pay/yspay_sign-one#step'=> array
	    (
	        'name'      => '当前状态',
	        'default'   => '',
	        'desc'      => '当前状态',
	        'match'     => 'option',
	        # 读取另外表的关联方式
	        'sync'      => array('merchant_id' => 'id'),
	    ),

        'status'      => array
        (
            'type'      => 'tinyint-11',
            'name'      => '状态',
            'default'   => '2',
            'desc'      => '状态',
            'match'     => 'option',
            'option'    => $status,
            'update'    => 'radio',
            'search'    => 'select',
            //'list'      => true,
        ),

        'state'     => array
        (
            'type'      => 'tinyint-1',
            'name'      => '状态',
            'default'   => '1',
            'desc'      => '请选择状态',
            'match'     => 'is_numeric',
            'list_name' => '签约进度',
            'list'      => 'Dever::load("pay/yspay/sign.getLog", {id}, {status})',
        ),
        
        'cdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '录入时间',
            'match'     => array('is_numeric', time()),
            'desc'      => '',
            # 只有insert时才生效
            'insert'    => true,
            'list'      => 'date("Y-m-d H:i:s", {cdate})',
        ),
    ),
	'manage' => array
    (
    	'insert' => false,
        'edit' => false,

        # 自定义快捷新增和编辑
        'button' => array
        (
            '新增' => array('fast'),
        ),

        'list_button' => $list_button,
    ),

    'request' => array
    (
       'tx_inc' => array
		(
			'type' => 'update',
			'where' => array
			(
				'id' => 'yes',
			),
			'set' => array
			(
				'tx_cash' => array('yes', '+='),
			),
		),

		'dec' => array
		(
			'type' => 'update',
			'where' => array
			(
				'id' => 'yes',
			),
			'set' => array
			(
				'yue' => array('yes', '-='),
			),
		),

        'updateTxCash' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'hf_tx_cash' => array('yes', '+='),
            ),
        ),
    )
);