/**
 * 公共附件管理初始化
 */
var CommonFile = {
    id: "CommonFileTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CommonFile.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '附件原名', field: 'originalName', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "20%"},
        {title: '下载地址', field: 'id', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "40%",
            formatter: function (value, row, index) {
                return "<a href=\'javascript:void(0)\' onclick=\"CommonFile.downloadFileById('" + value + "')\">" + row.url + "</a>";
            }
        },
        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "20%"},
        {title: '上传时间', field: 'createTime', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "10%"},
        {title: '创建用户', field: 'createUser', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "10%"},
    ];
};

CommonFile.downloadFileById = function (id) {
    window.location.href = Feng.ctxPath + "/common/api/downloadFile/type/4/id/" + id;
}


/**
 * 检查是否选中
 */
CommonFile.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CommonFile.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加公共附件
 */
CommonFile.openAddCommonFile = function () {
    var index = layer.open({
        type: 1,
        title: '添加公共附件',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: "<form id=\"commonFileForm\" action=\"" + Feng.ctxPath + "/admin/common_file/add\" method=\"post\" enctype=\"multipart/form-data\" target=\"hiddenIframe\">\n" +
                "                    <div  class=\"panel panel-default\">\n" +
                "                        <div class=\"panel-heading\" style=\"font-weight: bold\">附件材料</div>\n" +
                "                        <div class=\"panel-body\">\n" +
                "                            <input type=\"text\" style=\"display: none\" id=\"index\" name=\"index\" >\n" +
                "                            <input type=\"file\" style=\"display: none\" id=\"fileUrl\" name=\"fileUrl\" onchange=\"CommonFile.fileChange(this)\">\n" +
                "                            <input class=\"form-control\"  id=\"fileInput\" onclick=\"$('#fileUrl').click()\">\n" +
                "                        </div>\n" +
                "                        <div class=\"panel-heading\" style=\"font-weight: bold\">备注</div>\n" +
                "                        <div class=\"panel-body\">\n" +
                "                            <textarea class=\"form-control\" id=\"description\" name=\"description\"></textarea>\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </form>",
        btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;提交', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
        btnAlign: 'c',
        yes: function (index, layero) {
            $("#index").val(index);
            $("#commonFileForm")[0].submit();
        }
    });
    this.layerIndex = index;
};


/**
 * 删除公共附件
 */
CommonFile.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/admin/common_file/delete", function (data) {
                if (data.code == 200) {
                    Feng.success(data.msg);
                    CommonFile.table.refresh();
                } else {
                    Feng.info(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", CommonFile.seItem.id);
            ajax.start();
        }
        Feng.confirm("确认删除吗?", operation);
    }
};

CommonFile.fileChange = function (context) {
    if (!Feng.chkFileInvalid(context.files[0], 5, 20)) {
        return;
    }
    var file = $(context).val();
    var pos = file.lastIndexOf("\\");
    $("#fileInput").val(file.substring(pos + 1));
}

CommonFile.callback = function (data) {
    if (data.code == 200) {
        Feng.success(data.msg);
        layer.close(data.obj);
        CommonFile.table.refresh();
    } else {
        Feng.info(data.msg);
    }
}


$(function () {
    var defaultColunms = CommonFile.initColumn();
    var table = new BSTable(CommonFile.id, "/admin/common_file/list", defaultColunms);
    table.setPaginationType("server");
    CommonFile.table = table.init();
});
