/**
 * 初始化人才认定申报详情对话框
 */
var TalentInfoInfoDlg = {
    talentInfoInfoData: {},
};
/**
 * 初始化表格的列
 */
TalentInfoInfoDlg.initFileTypeColumn = function () {
    return [
        {field: 'selectItem', checkbox: false, visible: false},
        {title: '名称', field: 'name', visible: true, align: 'center', valign: 'middle', width: "30%", 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (row.must == 1) {
                    return '<i class="fa fa-paste"></i><span style="font-weight:bold;color:red;font-size:14px;font-family:宋体"> * </span> ' + value;
                }
                if (row.must == 2) {
                    return '<i class="fa fa-paste"></i>' + value;
                }
            }
        },
        {title: '模板', field: 'templateUrl', visible: true, align: 'center', valign: 'middle', width: "8%",
            formatter: function (value, row, index) {
                if (value == null || value == '' || value == 'null') {
                    return '无';
                }
                return "<button type='button' onclick=\"TalentInfoInfoDlg.downloadFile('" + row.id + "',3)\" style='margin-right: 10px' class=\"btn btn-xs btn-primary\">" +
                        "<i class=\"fa fa-download\"></i>下载" +
                        "</button>";
            }
        },
        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle', width: "52%", 'class': 'uitd_showTip'},
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: "10%",
            formatter: function (value, row, index) {
                return "";
            }
        }
    ]
};

TalentInfoInfoDlg.initFileTable = function () {
    var ajax = new $ax("/common/api/findCommonFileType", function (data) {
        if (data == null || data.length == 0) {
            return;
        }
        var datas = new Array();
        for (var i = 0; i < $(".fileTable").length; i++) {
            datas.push([]);//创建空的多维数组，等下用来存每个附件表的各自的列
        }
        for (var k in data["rows"]) {
            var rel = data["rows"][k].rel;
            if ($("#" + rel).length > 0) {
                var tableIndex = $("#" + rel).parents(".row").next(".row").find("table.fileTable").index(".fileTable");
                data["rows"][k].tableIndex = tableIndex;
                data["rows"][k].trIndex = datas[tableIndex].length;
                datas[tableIndex].push(data["rows"][k]);
            } else {
                if (data["rows"][k].isConditionFile) {
                    if ($("#talent_condition").parents(".table").length > 0) {
                        tableIndex = $("#talent_condition").parents(".table").find("table.fileTable").index(".fileTable");
                    } else {
                        tableIndex = $("#talent_condition").parents(".row").next(".row").find("table.fileTable").index(".fileTable");
                    }
                    data["rows"][k].tableIndex = tableIndex;
                    data["rows"][k].trIndex = datas[tableIndex].length;
                    datas[tableIndex].push(data["rows"][k]);//放入人才条件后面的附件表
                } else {
                    var tableIndex = $(".fileTable").length - 1;
                    data["rows"][k].tableIndex = tableIndex;
                    data["rows"][k].trIndex = datas[tableIndex].length;
                    datas[$(".fileTable").length - 1].push(data["rows"][k]);//没有归属，放入最后一个附件表
                }
            }
        }
        for (var i = 0; i < $(".fileTable").length; i++) {
            var that = $(".fileTable").eq(i);
            that.bootstrapTable({
                columns: TalentInfoInfoDlg.initFileTypeColumn(),
                data: datas[i],
                showHeader: false,
                rowStyle: function (row, index) {
                    return {classes: ""};
                },
                onPostBody: function (data) {
                    for (var k in data) {
                        var files = data[k].files;
                        var html = '<ul class="imgs"><li style="width: 80%;font-weight: bold;padding-top: 5px;">附件原名</li><li style="width: 10%;font-weight: bold;padding-top: 5px;">预览</li><li style="width: 10%;font-weight: bold;padding-top: 5px;">操作</li>';
                        for (var key in files) {
                            var sn = files[key].url.lastIndexOf(".");
                            var suffix = files[key].url.substring(sn + 1, files[key].url.length);
                            var imgStr = "";
                            if (suffix == "pdf" || suffix == "PDF") {
                                imgStr = "<button type='button'  onclick=\"Feng.showPdf('" + files[key].url + "','" + files[key].id + "','" + files[key].orignName + "')\" 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') {
                                imgStr = "<button type='button'  onclick=\"Feng.showExcel('" + files[key].url + "','" + files[key].id + "','" + files[key].orignName + "')\" class=\"btn btn-xs btn-danger\"><i class=\"fa fa-file-excel-o\" aria-hidden=\"true\"></i></button>";
                            } else {
                                imgStr = '<img class=\"imgUrl\"  src=\"' + files[key].url + '\" style=\"width:25px;height:25px;\">';
                            }

                            html += '<li data-id="' + files[key].id + '">\n\
                                    <div><input type="hidden" name="uploadFiles[]" value="' + files[key].id + '"></div>\n' +
                                    '<div style="width: 80%;">' + files[key].orignName + '</div>\n' +
                                    '<div style="width: 10%;">' + imgStr + '</div>\n' +
                                    '<div style="width: 10%;"></div>\n\
                                    </li>';
                        }
                        html = html + '</ul>';
                        that.find("tr[data-index='" + k + "']").after('<tr class="detail-view"><td colspan="5">' + html + '</td></tr>');
                    }
                    $("td.uitd_showTip").bind("mouseover", function () {
                        //var htm = $(this).html();
                        //$(this).webuiPopover({title: '详情', content: htm, trigger: 'hover'}).webuiPopover('show');
                    });
                },
            });
        }
        //$(".ibox-content").viewer({fullscreen: false});
    }, function (data) {
        Feng.error("查询失败!" + data.responseJSON.message + "!");
    });
    var queryData = {};
    queryData["mainId"] = $("#id").val();
    queryData['project'] = CONFIG.project_rcrd;
    queryData['type'] = $("#type").val();
    queryData["talent_condition"] = $("#talent_condition").val();
    queryData['checkState'] = $("#checkState").val();
    ajax.set(queryData);
    ajax.start();
}

TalentInfoInfoDlg.downloadFile = function (id, type) {
    window.location.href = Feng.ctxPath + "/api/common/downloadFile?id=" + id + "&type=" + type;
}


$(function () {
    $("#talent_type option").eq(0).prop("selected", true);
    var id = $("#id").val();
    var checkState = $("#checkState").val();
    TalentInfoInfoDlg.initFileTable();
    if (id != null && id != '') {
        //select初始化
        $("select").each(function () {
            $(this).val($(this).attr("value")).trigger("change");
        });
        Feng.getCheckLog("logTable", {"type": CONFIG.project_rcrd, "mainId": id, "typeFileId": "", "active": 1})
    }
});


