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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'users/withdraw/index' + location.search,
                    add_url: 'users/withdraw/add',
                    edit_url: 'users/withdraw/edit',
                    del_url: 'users/withdraw/del',
                    multi_url: 'users/withdraw/multi',
                    import_url: 'users/withdraw/import',
                    table: 'withdraw',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                $("#total_withraw").text(data.total_withraw);
                $("#total_money").text(data.total_money);

            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'tel', title: __('Tel')},
                        {field: 'bank_name', title: __('Bank_name'), operate: 'LIKE'},
                        {field: 'bank_num', title: __('Bank_num')},
                        {field: 'money', title: __('Money')},
                        {field: 'serve_money', title: __('Serve_money')},
                        {field: 'remark', title: __('拒绝理由')},
                        {field: 'status', title: __('Status'), searchList: {"-1":__('Status -1'),"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'withdraw_way', title: __('Withdraw_way'), searchList: {"1":__('Withdraw_way 1'),"2":__('Withdraw_way 2')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'auditpass',
                                    text: __('通过'),
                                    title: __('通过'),
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    url:function (row) {
                                        return 'users/Withdraw/audit?id='+row.id+'&status=1';
                                    },
                                    confirm:function (row){
                                        return '您确定要通过吗?';
                                    },
                                    refresh:true,
                                    hidden:function (row) {
                                        return row.status==0?false:true;
                                    },
                                },{
                                    name: 'refuce',
                                    text: __('拒绝'),
                                    title: __('请输入退款理由，并确认'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    //extend: 'data-area="["100%","100%"]"',
                                    url:function (row) {
                                        return 'users/Withdraw/refuce?id='+row.id;
                                    },
                                    hidden:function (row) {
                                        return row.status==0?false:true;
                                    },
                                },
                            ],}
                    ]
                ]
            });


            $('#batchaudit').click(function () {
                var data= table.bootstrapTable('getSelections');
                var id_str = '';
                for(var i = 0; i < data.length; i++){
                    id_str += data[i].id + ',';
                }
                id_str  =  id_str.substr(0,id_str.length-1);
                console.log(id_str);
                $.get('users/withdraw/batchaudit',{ids:id_str},function (res) {
                    if(res.code == 1){
                        console.log('批量审核成功');
                        layer.alert('批量审核成功');
                    }else {
                        console.log(res.msg);
                        layer.alert(res.msg);
                    }
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        batchaudit: function () {
            Controller.api.bindevent();
        },
        refuce:function(){
            $(".btn-dialog").data("success", function(){
                parent.window.$(".btn-refresh").trigger("click");
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
