define(['table', 'upload','form'], function (Table,Upload,Form) {
    var Admin = {
        //Do setup work hereAction
        index: function () {
            
            Table.api.init({
                extend: {
                    index_url: 'admin/select',
                    add_url: 'admin/insert',
                    edit_url: 'admin/update',
                    del_url: 'admin/delete',
                    multi_url: 'admin/multi',
                    dragsort_url: 'admin/weigh',
                    table: 'admin',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: false,
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true    // 是否排序
                        },
                        {
                            field: 'username',
                            title: '用户名',
                            filter: "string",
                        },
                        {
                            field: 'role_name',
                            title: '角色',
                            filter: "string",
                        }, 
                        {
                            field: 'mobile',
                            title: '手机',
                            filter: "string",
                        }, 
                        {
                            field: 'email',
                            title: '邮箱',
                            filter: "string"
                        }, 
                        {
                            field: 'login_at',
                            title: '最后登录',
                            filter: "date",
                            visible:false
                        },
                        {
                            field: 'created_at',
                            title: '注册时间',
                            filter: "date",
                            visible:false
                        }, 
                        {
                            field: 'status',
                            title: '状态',
                            formatter:Table.api.formatter.switch
                        },
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            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: "adminrole/select?format=select",
                dataType: "json",
                success: function (res) {
                    var html = "";
                    var selected=$('#roles').data('value');
                    for (let index = 0; index < res.data.length; index++) {
                        const element = res.data[index];
                        if(selected == element.value){
                            html+='<option value="'+element.value+'" selected>'+element.name+'</option>';
                        }else{
                            html+='<option value="'+element.value+'">'+element.name+'</option>';
                        }
                    }
                    $('#roles').append(html);
                }
            });
        }
    };
    return Admin
});