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

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

            var table = $("#table");

            // 初始化表格
            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: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'random_number', title: __('用户编号'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'realname', title: __('Realname'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'total_money', title: __('Money'), operate: false },
                        { field: 'shopping_money', title: __('购物金'), operate: false },
                        { field: 'province', title: __('Province'), operate: false ,visible: false},
                        { field: 'city', title: __('City'), operate: false ,visible: false},
                        { field: 'token', title: __('Token'), operate: false, visible: false },
                        { field: 'sex', title: __('Sex'), operate: false, searchList: { "0": __('Sex 0'), "1": __('Sex 1'), "2": __('Sex 2') }, formatter: Table.api.formatter.normal,visible: false},
                        { field: 'prevtime', title: __('Prevtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime , operate: false,visible: false},
                        { field: 'logintime', title: __('Logintime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime , operate: false,visible: false},
                        { field: 'loginip', title: __('Loginip'), operate: false ,visible: false},
                        { field: 'jointime', title: __('Jointime'), operate: false, addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'type', title: __('Type'), searchList: { "1": __('Type 1'), "2": __('Type 2') }, formatter: Table.api.formatter.normal },
                        { field: 'wx_openid', title: __('Wx_openid'), operate: false, visible: false },
                        { field: 'app_openid', title: __('App_openid'), operate: false, visible: false },
                        { field: 'unionid', title: __('Unionid'), operate: false, visible: false },
                        { field: 'session_key', title: __('Session_key'), operate: false, visible: false },
                        { field: 'brand', title: __('Brand'), operate: false, visible: false},
                        { field: 'equity', title: __('Equity'), operate: false,sortable:true},
                        { field: 'equity_code', title: __('Equity_code'), operate: 'LIKE' },
                        { field: 'signtime', title: __('Signtime'),operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        { field: 'user_type', title: __('User_type'), searchList: { "wechat": __('User_type wechat'), "app": __('User_type app') }, formatter: Table.api.formatter.normal },
                        { field: 'status', title: __('Status'),visible: false, searchList: { "1": __('Status 1'), "2": __('Status 2') }, formatter: Table.api.formatter.status},
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'editmoney',
                                    text: __('余额'),
                                    title: __('余额'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'users/users/editMoney',
                                },
                                {
                                    name: 'editshoppingmoney',
                                    text: __('购物金'),
                                    title: __('购物金'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'users/users/editShoppingMoney',
                                },
                                {
                                    name: 'editequity',
                                    text: __('股权'),
                                    title: __('股权'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'users/users/editEquity',
                                },
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        editmoney:function(){
            $(".btn-dialog").data("success", function(){
                parent.window.$(".btn-refresh").trigger("click");
            });
            Controller.api.bindevent();
        },
        editshoppingmoney:function(){
            $(".btn-dialog").data("success", function(){
                parent.window.$(".btn-refresh").trigger("click");
            });
            Controller.api.bindevent();
        },
        editequity: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;
});
