var EpChange = {
    id: "table", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

EpChange.initColumn = function () {
    var type = $("#type").val();
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '<span style="color: red">原医院名称</span>', field: 'oldName', align: 'center', width: 200, valign: 'middle', 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (row.oldIsGeneral == 1) {
                    return value + "（总院）";
                }
                return value;
            }
        },
        {title: '原法人代表', field: 'oldLegal', align: 'center', width: 120, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '原医共体', field: 'oldMedicalCommunityName', align: 'center', width: 120, valign: 'middle', 'class': 'uitd_showTip'},        
        {title: '原医院地址', field: 'oldAddress', align: 'center', width: 200, valign: 'middle', 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                return row.oldProvinceName + row.oldCityName + row.oldCountyName + " " + value;
            }
        },
        {title: '<span style="color: red">新医院名称</span>', field: 'newName', align: 'center', width: 200, valign: 'middle', 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (row.newIsGeneral == 1) {
                    return value + "（总院）";
                }
                return value;
            }
        },
        {title: '新法人代表', field: 'newLegal', align: 'center', width: 120, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '新医共体', field: 'newMedicalCommunityName', align: 'center', width: 120, valign: 'middle', 'class': 'uitd_showTip'},        
        {title: '新医院地址', field: 'newAddress', align: 'center', width: 200, valign: 'middle', 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                return row.newProvinceName + row.newCityName + row.newCountyName + " " + value;
            }
        },
        {title: '申请时间', field: 'createTime', align: 'center', width: 150, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: 100,
            formatter: function (value, row, index) {
                return "<span class='label label-success' onclick=\"EpChange.showLog('" + value + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        },
        {title: '审核状态', field: 'checkState', align: 'center', width: 120, valign: 'middle', 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == null || value == '') {
                    return "";
                } else if (value == 1) {
                    return "保存未提交";
                } else if (value == 2) {
                    return "待审核";
                } else if (value == 3) {
                    return "审核驳回";
                } else if (value == 4) {
                    return "审核通过";
                } else if (value == 5) {
                    return "重新提交";
                } else if (value == 6) {
                    return "初审驳回";
                } else if (value == 7) {
                    return "初审通过";
                } else {
                    return "";
                }
            },
            cellStyle: function (value, row, index) {
                if (value == null || value == '') {
                    return {css: {}};
                } else if (value == 1) {
                    return {css: {}};
                } else if (value == 2) {
                    return {css: {"background-color": "LightGrey"}};
                } else if (value == 3) {
                    return {css: {"background-color": "Orange"}};
                } else if (value == 4) {
                    return {css: {'background-color': 'LightGreen'}};
                } else if (value == 5) {
                    return {css: {'background-color': 'LightGrey'}};
                } else if (value == 6) {
                    return {css: {'background-color': 'Orange'}};
                } else if (value == 7) {
                    return {css: {'background-color': 'LightBlue'}};
                } else {
                    return {css: {}};
                }
            }
        },
    ];
};

EpChange.search = function () {
    var queryData = {};
    queryData['oldName'] = $("#oldName").val();
    queryData['oldIdCard'] = $("#oldIdCard").val();
    queryData['oldLegal'] = $("#oldLegal").val();
    queryData['oldMedicalCommunityId'] = $("#oldMedicalCommunityId").val();
    queryData['oldIsGeneral'] = $("#oldIsGeneral").val();
    queryData['newName'] = $("#newName").val();
    queryData['newIdCard'] = $("#newIdCard").val();
    queryData['newLegal'] = $("#newLegal").val();
    queryData['newMedicalCommunityId'] = $("#newMedicalCommunityId").val();
    queryData['newIsGeneral'] = $("#newIsGeneral").val();
    queryData['checkState'] = $("#checkState").val();

    EpChange.table.refresh({"query": queryData});
};

EpChange.reset = function () {
    $("#oldName").val("");
    $("#oldIdCard").val("");
    $("#oldLegal").val("");
    $("#oldMedicalCommunityId").val("");
    $("#oldIsGeneral").val("");
    $("#newName").val("");
    $("#newIdCard").val("");
    $("#newLegal").val("");
    $("#newMedicalCommunityId").val("");
    $("#newIsGeneral").val("");
    $("#checkState").val("");
};

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

EpChange.gotoEnterpriseChangeDetailPage = function () {
    if (!EpChange.check()) {
        return;
    }
    var index = layer.open({
        type: 2,
        title: '查看详情',
        area: ['830px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/admin/enterpriseChangeRecord/gotoEnterpriseChangeDetailPage?id=' + EpChange.seItem.id,
        btn: ['<i class="fa fa-check"></i>&nbsp;&nbsp;审核', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
        btnAlign: 'c',
        yes: function (index, layero) {
            var obj = layero.find("iframe")[0].contentWindow;
            obj.ecre.doExamine();
        }
    });
    layer.full(index);
    EpChange.layerIndex = index;
};


/**
 * 修改驳回的字段及附件
 */
EpChange.updateFieldsAndFiles = function () {
    if (this.check()) {
        var id = EpChange.seItem.id;
        var ajax = new $ax("/admin/enterpriseChangeRecord/findFieldsAndFiles?id=" + id, function (data) {
            if (data.code == 200) {
                layer.open({
                    type: 1,
                    id: "fieldCheckModalForm",
                    title: '修改',
                    area: ['800px', '450px'], //宽高
                    fix: false, //不固定
                    shade: 0,
                    maxmin: true,
                    content: EpChange.creatFieldCheckModal(),
                    btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;提交', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
                    btnAlign: 'c',
                    zIndex: layer.zIndex,
                    success: function (layero, index) {
                        var fileList = data.files;
                        var fieldList = data.fields;
                        var html_field = '';
                        var html_file = '';
                        for (var key in fieldList) {
                            html_field = html_field + '<li><input type="checkbox" ' + (fieldList[key].checked ? "checked" : "") + ' value="' + fieldList[key].field + '"><span>' + fieldList[key].name + '</span></li>';
                        }
                        for (var key in fileList) {
                            html_file = html_file + '<li style="width: 100%"><input type="checkbox" ' + (fileList[key].checked ? "checked" : "") + ' value="' + fileList[key].field + '"><span>' + fileList[key].name + '</span></li>';
                        }
                        $("#field_info").empty().append("<ul>" + html_field + "</ul>");
                        $("#field_file").empty().append("<ul>" + html_file + "</ul>");
                    },
                    yes: function (index, layero) {
                        EpChange.submitFieldsAndFiles(index, id);
                    }
                });
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};

/**
 * 修改提交
 * @param index
 * @param id
 */
EpChange.submitFieldsAndFiles = function (index, id) {
    var fields = '';
    var files = '';
    $("#field_info li input").each(function (index) {
        if ($(this).is(":checked")) {
            fields = fields + $(this).val() + ",";
        }
    });
    $("#field_file li input").each(function (index) {
        if ($(this).is(":checked")) {
            files = files + $(this).val() + ",";
        }
    });
    if (fields == '' && files == '') {
        Feng.info("请选择可修改的字段或附件！");
        return;
    }
    var ajax = new $ax("/admin/enterpriseChangeRecord/updateFieldsAndFiles", function (data) {
        if (data.code == 200) {
            layer.close(index);
            Feng.success(data.msg);
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.setData({"id": id, "fields": fields, "files": files})
    ajax.start();
};
EpChange.creatFieldCheckModal = function () {
    return '<form id="fieldCheckModalForm">\n' +
            '                    <div class="form-group" style="margin: 10px;">\n' +
            '                        <div >\n' +
            '                            <label for="checkMsg" class="control-label">可修改字段</label>\n' +
            '                            <div id="field_info">\n' +
            '                            </div>\n' +
            '                            <label for="checkMsg" class="control-label">可修改附件</label>\n' +
            '                            <div id="field_file">\n' +
            '                            </div>\n' +
            '                            <div class="form-group" style="text-align: center">\n' +
            '                                <button type="button" class="btn btn-primary" onclick="EpChange.checkAll()">全选</button>\n' +
            '                                <button type="button" class="btn btn-success" onclick="EpChange.unCheckAll()">反选</button>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </form>';
};
EpChange.checkAll = function () {
    $("#fieldCheckModalForm input[type=checkbox]").prop("checked", true);
};
EpChange.unCheckAll = function () {
    $("#fieldCheckModalForm input[type=checkbox]").removeAttr("checked");
};

EpChange.export = function () {
    var queryData = {};
    queryData['oldName'] = $("#oldName").val();
    queryData['oldIdCard'] = $("#oldIdCard").val();
    queryData['oldLegal'] = $("#oldLegal").val();
    queryData['oldMedicalCommunityId'] = $("#oldMedicalCommunityId").val();
    queryData['oldIsGeneral'] = $("#oldIsGeneral").val();
    queryData['oldIndustryField'] = $("#oldIndustryField").val();
    queryData['newName'] = $("#newName").val();
    queryData['newIdCard'] = $("#newIdCard").val();
    queryData['newLegal'] = $("#newLegal").val();
    queryData['newMedicalCommunityId'] = $("#newMedicalCommunityId").val();
    queryData['newIsGeneral'] = $("#newIsGeneral").val();
    queryData['checkState'] = $("#checkState").val();
    window.location.href = Feng.setUrlParam(Feng.ctxPath + "/admin/enterpriseChangeRecord/export", queryData);
}

EpChange.showLog = function (id) {
    layer.open({
        type: 1,
        title: "日志",
        fixed: false,
        content: '<table id="' + id + '"></table>',
        area: ['80%', '80%'],
        maxmin: true,
        success: function (layero, index) {
            Feng.getCheckLog(id, {"type": 10, "mainId": id, "typeFileId": "", "active": 1})
        }
    });
}



$(function () {
    var defaultColunms = EpChange.initColumn();
    var table = new BSTable(EpChange.id, "/admin/enterpriseChangeRecord/findEnterpriseChangeRecordByPage", defaultColunms);
    table.setPaginationType("server");
    EpChange.table = table.init();
});