define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    balance_url: 'user/user/balance',
                    cardslip_url: 'user/user/cardslip',
                    collection_url: 'user/user/collection',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},

                        {field: 'id', title: __('Group_id')},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'parent_id', title: __('Parent_id'), operate: 'BETWEEN', sortable: true},
                        {field: 'balance', title: __('Balance'), operate: 'BETWEEN', sortable: true},
                        {field: 'freeze', title: __('Freeze'), operate: 'BETWEEN', sortable: true},
                        {field: 'task_num', title: __('Task'), formatter: Table.api.formatter.search},

                        {field: 'id', title: __('Recharge'), operate: 'BETWEEN', sortable: true},
                        {field: 'id', title: __('Withdrawal'), operate: 'BETWEEN', sortable: true},
                        {field: 'id', title: __('Income'), operate: 'BETWEEN', sortable: true},

                        {field: 'invitation_code', title: __('Invitation_code'), formatter: Table.api.formatter.search},
                        {field: 'is_agent', title: __('Is_agent'), searchList: {1: __('Yes'), 0: __('No')}, formatter: Table.api.formatter.status},
                        
                        {field: 'user_type', title: __('User_type'), formatter: Table.api.formatter.toggle, 
                            searchList: {0: __('Dummy'), 1: __('Real person')}
                        },
                        {field: 'is_lock', title: __('Status'), formatter: Table.api.formatter.toggle, 
                            searchList: {0: __('Normal'), 1: __('Locking')}
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: "account",
                                    text:  __('Account change'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-dialog',
                                    url: 'trade/moneylog/index',
                                    refresh: true
                                },
                                {
                                name: "clear",
                                text:  __('Clear'),
                                classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                url: 'user/user/clear',
                                visible: function (row) {
                                    if(row.task_num > 0)  return true;
                                    return false;
                                },
                                refresh: true
                            },{
                                name: "team",
                                text:  __('Team'),
                                classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                confirm: __('Confirm review'),
                                url: 'user/group/Team',
                
                                refresh: true
                            },{
                                name: "edit",
                                text:  __('Edit'),
                                classname: 'btn btn-xs btn-warning btn-magic btn-dialog',
                                url: 'user/user/edit',
                                refresh: true
                            },{
                                name: "del",
                                text:  __('Del'),
                                classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                confirm: __('Confirm review'),
                                url: 'user/user/del',
                                refresh: true
                            }],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //余额           
            $(document).on('click','.btn-balance',function () {
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.balance_url + "?ids=" + ids, __('Balance'))
            });
            //卡单  
            $(document).on('click','.btn-cardslip',function () {
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.cardslip_url + "?ids=" + ids, __('Card slip'))
            });
            //收款  
            $(document).on('click','.btn-collection',function () {
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.collection_url + "?ids=" + ids, __('Collection'))
            });

        },
        balance: function () {
            //余额
            Controller.api.bindevent();
        },
        cardslip: function () {
            //卡单
            Controller.api.bindevent();
        },
        collection: function () {
            //收款
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});