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

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

                    auth_url: 'user/withdrawal/userwithdrawal_log/auths',
                    paid_url: 'user/withdrawal/userwithdrawal_log/paid',

                    table: 'user_withdrawal_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                asyndownload: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'fee_price', title: __('Fee_price'), operate:'BETWEEN'},
                        {field: 'real_price', title: __('Real_price'), operate:'BETWEEN'},
                        {field: 'real_have_price', title: __('Real_have_price'), operate:'BETWEEN'},
                        {field: 'real_fee_price', title: __('Real_fee_price'), operate:'BETWEEN'},



                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},

                        {field: 'withdrawal_status', title: __('Withdrawal_status'), searchList: {"1":__('Withdrawal_status 1'),"2":__('Withdrawal_status 2'),"3":__('Withdrawal_status 3')}, formatter: Table.api.formatter.status},
                        {field: 'remark', title: __('打款备注'), operate: 'LIKE', table: table, class: 'autocontent'},

                        {field: 'reason', title: __('Reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'type', title: __('Type'), searchList: {"bank":__('Type bank'),"wechat":__('Type wechat'),"alipay":__('Type alipay')}, formatter: Table.api.formatter.normal},
                        {field: 'name',visible:false, title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bank_name',visible:false, title: __('Bank_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bank_user_name',visible:false, title: __('Bank_user_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'examinetime', title: __('Examinetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user.nickname',visible:false, title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.realname',visible:false, title: __('User.realname'), operate: 'LIKE'},
                        {field: 'user.mobile',visible:false, title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'user.avatar', visible:false,title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},

                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},




                        {field: 'operate', title: __('Operate'), table: table , buttons: [
                                {
                                    name: 'auths',
                                    text: __('提现审核'),
                                    title: __('审核提现是否通过'),
                                    classname: 'btn btn-xs btn-danger  btn-magic btn-dialog',
                                    icon: 'fa fa-sign-in',
                                    // dropdown : '更多',
                                    url: $.fn.bootstrapTable.defaults.extend.auth_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.status == '1';
                                    }
                                },
                                {
                                    name: 'paid',
                                    text: __('确认打款'),
                                    title: __('确认打款并填写备注'),
                                    classname: 'btn btn-xs btn-danger  btn-magic btn-dialog',
                                    icon: 'fa fa-sign-in',
                                    // dropdown : '更多',
                                    url: $.fn.bootstrapTable.defaults.extend.paid_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.status == '2' && row.withdrawal_status == '2';
                                    }
                                },


                                {
                                    name: 'order_detail',
                                    text: __('提现对应的结算记录'),
                                    title: __('提现对应的结算记录'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-calendar',
                                    dropdown : '更多',
                                    url: order_detail_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.paytime;
                                    // }
                                },


                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate},


                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

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



            //自定义Tab筛选条件
            $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value!=="") {
                        //这里可以自定义多个筛选条件
                        filter.withdrawal_status = value;
                        op.withdrawal_status = '=';
                    }else{
                        //console.log("111111111111111111");
                        //选全部时要移除相应的条件
                        delete filter.withdrawal_status;
                        delete op.withdrawal_status;
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });



        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        auths: function () {
            Controller.api.bindevent();
        },
        paid: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };


    var order_detail_url = function (row,dom) {
        return 'school/activity/order/settle_log/index?withdrawal_log_id='+row.id;
    }

    return Controller;
});
