define(['table', 'upload','form'], function (Table,Upload,Form) {
    var User = {
        //Do setup work hereAction
        index: function () {
            window.filterData = {
            };
            window.Lang = window.Lang ? window.Lang : Config.currency_langs;
            window.groupOption=Config.groupList;
            window.roleOption=Config.roleList;
            
            Table.api.init({
                extend: {
                    index_url: 'user/select',
                    add_url: 'user/insert',
                    edit_url: 'user/update',
                    del_url: 'user/delete',
                    multi_url: 'user/multi',
                    dragsort_url: 'user/weigh',
                    table: 'user',
                }
            });
            //console.log(Config)
            var table = $("#table");
            var columns = [
                {checkbox: true},
                {
                    field: 'id',
                    title: 'ID',
                    filter: "number",
                    sortable: true    // 是否排序
                },
                {
                    title: "角色",
                    field: "role_id",
                    formatter:function(v,row){
                        return row.role ? row.role.name : '用户';
                    },
                    filter: "select",
                    filterOption:"roleOption"
                },
                {
                    title: "昵称",
                    field: "nickname"
                },
                {
                    title: "头像",
                    field: "avatar",
                    formatter: function (v,d) {
                        return '<img src="'+encodeURI(d['avatar'])+'" style="max-width:32px;max-height:32px;" alt="" />'
                    },
                    visible: false
                },
                // {
                //     title: "等级",
                //     field: "level",
                //     visible: false,
                // },
                // {
                //     title: "生日",
                //     field: "birthday",
                //     visible: false,
                // },
                // {
                //     title: "后缀",
                //     field: "decimal_part",
                //     //visible: false,
                // },
            ];
            var currencys = ['money','score','currency1','currency2','currency3','currency4','currency5','currency6','currency7','currency8','currency9'];
            for (let i = 0; i < currencys.length; i++) {
                if(Config.allow_currencys.indexOf(currencys[i])!==-1){
                    columns.push({
                        title: __(currencys[i]),
                        field: currencys[i],
                        formatter:Table.api.formatter.number,
                        //sortable: true,
                        //visible: false,
                    });
                }
            };
            columns.push({
                title: "注册时间",
                field: "createTime",
                formatter:function(v,row){
                    return Table.api.formatter.datetime(parseInt(v/1000));
                },
                filter:'datetime'
            });
            columns.push({
                title: "状态",
                field: "status",
                formatter: Table.api.formatter.switch
            });
            columns.push({
                field: 'operate', title: '操作', 
                table: table, 
                events: Table.api.events.operate, 
                formatter: Table.api.formatter.operate,
                buttons:[
                    {
                        text:"团队",
                        name:"team",
                        icon:"mdi mdi-account-group-outline",
                        classname:"btn btn-xs btn-info btn-dialog",
                        url:'user/team'
                    }
                ]
            });
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                columns: [columns]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        update:function(){
            Config['upload_url'] = '/files/avatar';
            var form = $('form');
            Form.api.bindevent(form)
            this.getRole();
        },
        insert:function(){
            Config['upload_url'] = '/files/avatar';
            var form = $('form');
            Form.api.bindevent(form)
            this.getRole();
        },
        getRole:function(){
            Fast.api.ajax({
                url: "UserRole/select?format=tree",
                dataType: "json",
                success: function (res) {
                    Layer.closeAll();
                    var html = "";
                    var selected=$('#roles').data('value');
                    for (let index = 0; index < res.data.length; index++) {
                        const element = res.data[index];
                        if(selected == element.id){
                            html+='<option value="'+element.id+'" selected>'+element.name+'</option>';
                        }else{
                            html+='<option value="'+element.id+'">'+element.name+'</option>';
                        }
                    }
                    $('#roles').append(html);
                }
            });
        },
        team:function(){
            window.filterData = {
                "user_id":{value1:Fast.api.query('ids'),symbol:'=','value2':''},
                'type' : {value1:'child',symbol:'=','value2':''}
            };

            $('.nav-tabs li').on('click',function(){
                $('.nav-tabs li').removeClass('active');
                $(this).addClass('active');
                window.filterData['type'] = {value1:$(this).data('type'),symbol:'=','value2':''};
                table.bootstrapTable('selectPage', 1);
            });
            
            Table.api.init({
                extend: {
                    index_url: 'team/select',
                    add_url: null,
                    edit_url: null,
                    del_url: null,
                    multi_url: null,
                    dragsort_url: null,
                    table: 'team',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            title: "深度",
                            field: "depth",
                        },
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true    // 是否排序
                        },
                        {
                            title: "角色",
                            field: "role_id",
                            formatter: function(v,item){
                                return 'V'+v;
                            },
                        },
                        {
                            title: "用户名",
                            field: "username",
                            filter: "string",
                        },
                        // {
                        //     title: "等级",
                        //     field: "level",
                        //     visible: false,
                        // },
                        {
                            title: "余额",
                            field: "money",
                            formatter:Table.api.formatter.number,
                            sortable: true,
                            //visible: false,
                        },
                        {
                            title: "积分",
                            field: "score",
                            sortable: true,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "调研币",
                            field: "currency1",
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "可领取",
                            field: "currency6",
                            sortable: true,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "待分配",
                            field: "currency7",
                            sortable: true,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "已分配",
                            field: "currency8",
                            sortable: true,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "未通过",
                            field: "currency9",
                            sortable: true,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "登录时间",
                            field: "last_time",
                            formatter:Table.api.formatter.datetime,
                            visible: false,
                        },
                        {
                            title: "登录ip",
                            field: "last_ip",
                            visible: false,
                        },
                        {
                            title: "注册时间",
                            field: "join_time",
                            formatter:Table.api.formatter.datetime,
                            filter:'datetime'
                        },
                        {
                            title: "注册ip",
                            field: "join_ip",
                            visible: false,
                        },
                        {
                            title: "创建时间",
                            field: "created_at",
                            visible: false,
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                            formatter:Table.api.formatter.datetime,
                            visible: false,
                        },
                        {
                            title: "状态",
                            field: "status",
                            formatter: Table.api.formatter.switch
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        }
    };
    return User
});