/**
 * commonFileType管理初始化
 */
var UnCommonFiletype = {
    id: "UnCommonFiletypeTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
UnCommonFiletype.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        // {title: '优秀人才类别', field: 'type', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: 100,
        //     formatter: function (value, row, index) {
        //         if (value == null || value == '') {
        //             return "";
        //         } else if (value == 1) {
        //             return "晋江市现代产业体系人才";
        //         } else if (value == 2) {
        //             return "集成电路优秀人才";
        //         } else {
        //             return "";
        //         }
        //     }
        // },
        {title: '申报项目', field: 'projectName', visible: true, align: 'center', valign: 'middle', width: 100},
        {title: '附件名称', field: 'name', visible: true, align: 'center', valign: 'middle', width: 200, 'class': 'uitd_showTip'},
        {title: '附件类型api', field: 'api', visible: true, align: 'center', valign: 'middle', width: 100, 'class': 'uitd_showTip'},
        {title: '必传', field: 'must', visible: true, align: 'center', valign: 'middle', width: 80,
            formatter: function (value, row, index) {
                if (value == null || value == '') {
                    return "";
                } else if (value == 1) {
                    return "是";
                } else if (value == 2) {
                    return "否";
                } else {
                    return "";
                }
            }
        },
        {title: '生效', field: 'active', visible: true, align: 'center', valign: 'middle', width: 80,
            formatter: function (value, row, index) {
                if (value == null || value == '') {
                    return "";
                } else if (value == 1) {
                    return "有效";
                } else if (value == 2) {
                    return "无效";
                } else {
                    return "";
                }
            }
        },
        {
            title: '模板', field: 'templateUrl', visible: true, align: 'center', valign: 'middle', width: 80,
            formatter: function (value, row, index) {
                if (value == null || value == '') {
                    return "无";
                }
                var sn = value.lastIndexOf(".");
                var suffix = value.substring(sn + 1, value.length);
                if (suffix == "pdf" || suffix == "PDF") {
                    return "<button type='button'  onclick=\"Feng.showPdf('" + value + "')\" class=\"btn btn-xs btn-danger\"><i class=\"fa fa-file-pdf-o\" aria-hidden=\"true\"></i></button>";
                } else if (suffix == "xlsx" || suffix == "XLSX" || suffix == 'xls' || suffix == 'XLS') {
                    return "<button type='button'  onclick=\"Feng.showExcel('" + value + "')\" class=\"btn btn-xs btn-danger\"><i class=\"fa fa-file-excel-o\" aria-hidden=\"true\"></i></button>";
                } else {
                    return '<img class=\"imgUrl\"  src=\"' + value + '\" style=\"width:25px;height:25px;\">';
                }
            }
        },
        {title: '排序', field: 'sn', visible: true, align: 'center', valign: 'middle', width: 80, },
        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle', width: 300, 'class': 'uitd_showTip'}
    ];
};

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

/**
 * 点击添加commonFileType
 */
UnCommonFiletype.openAddUnCommonFiletype = function () {
    var index = layer.open({
        type: 2,
        title: '添加',
        area: ['1200px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: '/admin/file_type/add'
    });
    UnCommonFiletype.layerIndex = index;
};

/**
 * 打开查看commonFileType详情
 */
UnCommonFiletype.openUnCommonFiletypeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改',
            area: ['1200px', '500px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/admin/file_type/edit/id/' + UnCommonFiletype.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除commonFileType
 */
UnCommonFiletype.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax("/admin/file_type/delete", function (data) {
                if (data.code == 200) {
                    Feng.success(data.msg);
                    UnCommonFiletype.table.refresh();
                } else {
                    Feng.error(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", UnCommonFiletype.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否刪除该附件类型?", operation);
    }
};

/**
 * 查询commonFileType列表
 */
UnCommonFiletype.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['type'] = $("#type").val();
    queryData['project'] = $("#project").val();
    queryData['must'] = $("#must").val();
    queryData['active'] = $("#active").val();
    queryData['checkCompany'] = $("#checkCompany").val();
    UnCommonFiletype.table.refresh({query: queryData});
};

UnCommonFiletype.reset = function () {
    $("#type").val("");
    $("#project").val("");
    $("#must").val("");
    $("#active").val("");
}

$(function () {
    var defaultColunms = UnCommonFiletype.initColumn();
    var table = new BSTable(UnCommonFiletype.id, "/admin/file_type/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnLoadSuccess(function () {
        $(".imgUrl").viewer({
            // toolbar:false,
            fullscreen: false
        });
    });
    UnCommonFiletype.table = table.init();
});

function showsmsselect() {
    var ajax = new $ax(Feng.ctxPath + "/company/selectAll", function (data) {
        var h = "";
        $.each(data, function (key, value) {

            h += "<option value='" + value.id + "'>" + value.shortName //下拉框序言的循环数据
                    + "</option>";
        });
        $("#checkCompany").append(h);//append 添加进去并展示
        $("#checkCompany").on(
                "change",
                function (a, b, c) {
                    $("#contentID").val(
                            $("#checkCompany option:selected").val());

                    $("#contentName").val(
                            $("#checkCompany option:selected").text());

                })
    }, function (data) {
        Feng.error("获取company数据失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.unCommonFiletypeInfoData);
    ajax.start();

}
