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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'user_id', title: __('User_id'),addClass: "selectpage", extend: "data-source='users/users/index' data-search-field='id,nickname,realname' data-field='realname'  data-format-item='{id} - {nickname} - {realname}' data-order-by='id desc'",
                            formatter: function (value, row, index) {
                                if (row.users) {
                                    var html = "id：" + row.user_id + "<br/> 昵称：" + row.users.nickname;
                                    html = row.users.realname ? html + "<br/> 姓名：" + row.users.realname : html;
                                    html = row.users.mobile ? html + "<br/> 电话：" + row.users.mobile : html;
                                    return html;
                                } else {
                                    return "id：" + row.user_id;
                                }

                            }
                        },
                        {field: 'money', title: __('Money'), operate:false},
                        {field: 'before', title: __('Before'), operate:false},
                        {field: 'after', title: __('After'), operate:false},
                        {field: 'memo', title: __('Memo'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'heatos', title: __('Heatos'), searchList: {"1":__('Heatos 1'),"-1":__('Heatos -1')}, formatter: Table.api.formatter.normal},
                        {field: 'logs_id', title: __('Logs_id'), visible: false, operate:false},
                        {field: 'operate', title: __('Operate'), visible: false, table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var filter = {};
                    if (value !== '') {
                        filter[field] = value;
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
