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

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            sortable: true    // 是否排序
                        },
                        {
                            title: "名字",
                            field: "title",
                            filter: "string",
                        },
                        {
                            title: "文件",
                            field: "path",
                            formatter: function (v,d) {
                                if (["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"].indexOf(d.extension.toLowerCase()) !== -1) {
                                    return '<img src="' + Fast.api.cdnurl(v) + '" style="max-width:32px;max-height:32px;" />';
                                }
                                return '<a href="' + encodeURI(v) + '" target="_blank">' + escape(v) + '</a>';
                            }
                        },
                        {
                            title: "管理员id",
                            field: "admin_id",
                            visible: false,
                        },
                        {
                            title: "用户id",
                            field: "user_id",
                            visible: false,
                        },
                        {
                            title: "文件大小",
                            field: "size",
                            formatter: function (v,d) {
                                return User.formatSize(d.size);
                            }
                        },
                        {
                            title: "类型",
                            field: "mime_type",
                            visible: false,
                        },
                        {
                            title: "图片宽度",
                            field: "width",
                        },
                        {
                            title: "图片高度",
                            field: "height",
                        },{
                            title: "扩展名",
                            field: "extension",
                        },
                        {
                            title: "上传时间",
                            field: "created_at",
                            visible: false,
                        },
                        {
                            title: "引用次数",
                            field: "use_count"
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                            hide: true,
                        },
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
            //上传
            Config['upload_url'] = 'files/insert';
            Upload.api.upload($(".plupload,.faupload"),function(){
                table.bootstrapTable('refresh');
                Fast.api.success('上传成功');
            },function(){
                console.log(this,arguments);
                
            },function(){
                console.log(this,arguments);
                
            });
        },
        // 格式化文件大小
        formatSize : function(value) {
            if(null == value || "" === value){
                return "0 Bytes";
            }
            let unitArr = ["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"];
            let index = 0;
            let srcSize = parseFloat(value);
            index = Math.floor(Math.log(srcSize) / Math.log(1024));
            let size =srcSize / Math.pow(1024, index);
            size = size.toFixed(2);
            return size + unitArr[index];
        },
        update:function(){
            this.bindevent();
        },
        insert:function(){
            this.bindevent();
        },
        bindevent:function(){
            Config['upload_url'] = 'files/file';
            var form = $('form');
            Form.api.bindevent(form);
        },
        list:function(){
            Table.api.init({
                extend: {
                    index_url: 'files/select',
                    add_url: 'files/insert',
                    edit_url: 'files/update',
                    del_url: 'files/delete',
                    multi_url: 'files/multi',
                    dragsort_url: 'files/weigh',
                    table: 'upload',
                }
            });

            // 选择行
            let select = function (obj) {
                let index = parent.layer.getFrameIndex(window.name);
                let callback = parent.layui.$("#layui-layer" + index).data("callback");
                callback(obj.data);
                parent.layer.close(index);
            };
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                commonSearch: false,
                search: false,
                showExport: false,
                totalField: "count",
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true    // 是否排序
                        },
                        {
                            title: "名字",
                            field: "title",
                            filter: "string",
                        },
                        {
                            title: "文件",
                            field: "path",
                            filter: "string",
                            formatter: function (v,d) {
                                if (["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"].indexOf(d.extension.toLowerCase()) !== -1) {
                                    return '<img src="' + Fast.api.cdnurl(v) + '" style="max-width:32px;max-height:32px;" />';
                                }
                                return '<a href="' + encodeURI(v) + '" target="_blank">' + escape(v) + '</a>';
                            }
                        },
                        {
                            title: "管理员id",
                            field: "admin_id",
                            visible: false,
                        },
                        {
                            title: "用户id",
                            field: "user_id",
                            visible: false,
                        },
                        {
                            title: "文件大小",
                            field: "size",
                            formatter: function (v,d) {
                                return User.formatSize(d.size);
                            }
                        },
                        {
                            title: "mime类型",
                            field: "mime_type",
                            hide: true,
                        },
                        {
                            title: "图片宽度",
                            field: "width",
                            visible: false,
                        },
                        {
                            title: "图片高度",
                            field: "height",
                            visible: false,
                        },{
                            title: "扩展名",
                            field: "extension",
                        },
                        {
                            title: "上传时间",
                            field: "created_at",
                            visible: false,
                        },
                        {
                            title: "引用次数",
                            field: "use_count"
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                            hide: true,
                        },
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        }
    };
    return User
});