define(['table', 'upload','form'], function (Table,Upload,Form) {
    var User = {
        //Do setup work hereAction
        index: function () {
            window.filterData = {
            };
            window.groupOption=Config.groupList;
            window.roleOption=Config.roleList;
            
            Table.api.init({
                extend: {
                    index_url: 'studio/select',
                    add_url: 'studio/insert',
                    edit_url: 'studio/update',
                    del_url: 'studio/delete',
                    multi_url: 'studio/multi',
                    dragsort_url: 'studio/weigh',
                    table: 'studio',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true    // 是否排序
                        },
                        {
                            title: "推荐人",
                            field: "parent_id",
                            formatter:function(v,row){
                                return row.referrer ? row.referrer.username : '';
                            },
                            visible: false,
                        },
                        {
                            title: "分组",
                            field: "group",
                            formatter:function(v,row){
                                for (let i = 0; i < Config.groupList.length; i++) {
                                    if(Config.groupList[i].value == v){
                                        return Config.groupList[i].label;
                                    }
                                }
                                return '';
                            },
                            filter: "select",
                            filterOption:"groupOption",
                            visible: false,
                        },
                        {
                            title: "角色",
                            field: "role_id",
                            formatter:function(v,row){
                                return row.role.name;
                            },
                            filter: "select",
                            filterOption:"roleOption",
                            visible: false,
                        },
                        {
                            title: "用户名",
                            field: "username",
                            filter: "string",
                        },
                        {
                            title: "昵称",
                            field: "nickname",
                            visible: false
                        },
                        // {
                        //     title: "密码",
                        //     field: "password",
                        //     visible: false,
                        // },
                        {
                            title: "性别",
                            field: "sex",
                            formatter: function (v,d) {
                                let field = "sex";
                                if (typeof d[field] == "undefined") return "";
                                let items = [];
                                $.each((d[field] + "").split(","), function (k , v) {
                                    items.push( v);//apiResults[field][v] ||
                                });
                                return util.escape(items.join(","));
                            },
                            visible: false,
                        },
                        {
                            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: "email",
                            visible: false
                        },
                        {
                            title: "手机",
                            field: "mobile",
                            visible: false
                        },
                        // {
                        //     title: "等级",
                        //     field: "level",
                        //     visible: false,
                        // },
                        // {
                        //     title: "生日",
                        //     field: "birthday",
                        //     visible: false,
                        // },
                        // {
                        //     title: "后缀",
                        //     field: "decimal_part",
                        //     //visible: false,
                        // },
                        {
                            title: "余额",
                            field: "money",
                            formatter:Table.api.formatter.number,
                            sortable: true,
                            //visible: false,
                        },
                        {
                            title: "积分",
                            field: "score",
                            sortable: true,
                            visible: false,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "currency1",
                            field: "currency1",
                            visible: false,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "冻结",
                            field: "currency2",
                            sortable: true,
                            visible: false,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "currency3",
                            field: "currency3",
                            visible: false,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "currency4",
                            field: "currency4",
                            visible: false,
                            formatter:Table.api.formatter.number
                        },
                        {
                            title: "currency5",
                            field: "currency5",
                            visible: false,
                            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
                        },
                        {
                            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:'studio/team'
                                }
                            ]
                        }
                    ]
                ]
            };
            // 初始化表格
            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",
                            visible: false,
                        },
                        {
                            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: "currency2",
                            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
});