$(function () {
    //百叶窗风格
    $('.collapse').collapse({
        toggle: false
    })
    //加载表格
    initialization();
    //加载时间控件1
    initDateInput(1);
    //加载时间控件2
    initDateInput(2);
    //加载性别
    loadSex();
    //加载科室
    loadDept();
    //加载转归情况
    loadDiagStatus();
 ;
    //权限控制按钮
    permissionControlButton();
});

function initialization(){
    $.ajax({
        type: 'get',
        url: path + '/commom/getInitialization',
        dataType: 'json',
        success: function (data) {
            console.log(data)
            if (data==1) {
                freshTable()
            }
        }
    })
}

function permissionControlButton() {
    var show = $("#showRecord").val();
    var load = $("#downloadRecord").val();
    //批量借阅申请权限判断
    if (show == 1) {
        $("#borrowingsBox").hide();
    } else {
        $("#borrowingsBox").show();
    }
    //批量下载申请权限判断
    if (load == 1) {
        $("#downBorrowingsBox").hide();//批量下载申请按钮不显示
        $("#downBorrowingBox").show();//下载按钮显示
    } else {
        $("#downBorrowingsBox").show();//批量下载申请按钮显示
        $("#downBorrowingBox").hide();//下载按钮不显示
    }
}

//加载性别
function loadSex() {
    $.ajax({
        type: 'get',
        url: path + '/commom/getSex',
        dataType: 'json',
        success: function (data) {
            if (null != data) {
                $("#sex").empty();
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    if (i > 1) {
                        html += '<div class="col-sm-3 radioDiv">'
                            + '<input type="radio" id="' + data[i].name + '" name="sex" value="' + data[i].name + '" class="sexInput"><label class="checkBoxClass" for="' + data[i].name + '">' + data[i].name + '</label></div>';
                    } else {
                        html += '<div class="col-sm-2 radioDiv">'
                            + '<input type="radio" id="' + data[i].name + '"  name="sex" value="' + data[i].name + '" class="sexInput"><label class="checkBoxClass" for="' + data[i].name + '">' + data[i].name + '</label></div>';
                    }
                }
                $("#sex").append(html);
            }
        }
    })
}

//加载病案号
function loadInpatientNo() {
    $.ajax({
        type: 'get',
        url: path + '/commom/getCommonInfo',
        dataType: 'json',
        success: function (data) {
            if (null != data) {
                $("#inpatient_no").empty();
                var html = '<option value=""></option>';
                for (var i = 0; i < data.length; i++) {
                    if (data[i].name != '' && data[i].inpatientNo != '') {
                        var name = data[i].name;
                        var inpatientNo = data[i].inpatientNo;
                        /*var py = makePy(name);
                        var pingyin = ConvertPinyin(name);*/
                        html += '<option value="' + inpatientNo + '">' + inpatientNo + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + name + '</option>';
                    }
                }
                $("#inpatient_no").append(html);
                $("#inpatient_no").selectpicker('refresh');
            }
        }
    })
}

//加载科室
function loadDept() {
    $.ajax({
        type: 'get',
        url: path + '/commom/getDept',
        dataType: 'json',
        success: function (data) {
            if (null != data) {
                $("#dis_dept").empty();
                var html = '<option value=""></option>';
                for (var i = 0; i < data.length; i++) {
                    if (data[i].name != '') {
                        var name = data[i].name;
                        var py = makePy(name);
                        var pingyin = ConvertPinyin(name);
                        html += '<option data-tokens="' + name + '|' + py[0] + '|' + pingyin + '" value="' + name + '">' + name + '</option>';
                    }
                }
                $("#dis_dept").append(html);
                $("#dis_dept").selectpicker('refresh');
            }
        }
    })
}

//加载科室
function loadDeptDefault(defaultDeptName){
    $.ajax({
        type:'get',
        url:path+'/commom/getDept',
        dataType:'json',
        success:function (data) {
            if(null != data){
                $("#disDept").empty();
                var html = '<option value="">请选择</option>';
                for (var i = 0; i < data.length; i++) {
                    if (data[i].name != '' && data[i].name == defaultDeptName) {
                        var name = data[i].name;
                        var py = makePy(name);
                        var pingyin = ConvertPinyin(name);
                        html += '<option data-tokens="' + name + '|' + py[0] + '|' + pingyin + '" value="' + name + '" selected>' + name + '</option>';
                    } else {
                        var name = data[i].name;
                        var py = makePy(name);
                        var pingyin = ConvertPinyin(name);
                        html += '<option data-tokens="' + name + '|' + py[0] + '|' + pingyin + '" value="' + name + '">' + name + '</option>';
                    }
                }
                $("#disDept").append(html);
                $("#disDept").selectpicker('refresh');
            }
        }
    })
}

//加载转归情况
function loadDiagStatus() {
    $.ajax({
        type: 'get',
        url: path + '/commom/getDiagStatus',
        dataType: 'json',
        success: function (data) {
            if (null != data) {
                $("#dis_thing").empty();
                var html = '<option value="">全部</option>';
                for (var i = 0; i < data.length; i++) {
                    if (data[i].code != '0') {
                        html += '<option value="' + data[i].name + '">' + data[i].name + '</option>';
                    }
                }
                $("#dis_thing").append(html);
            }
        }
    })
}

//加载诊断编码和诊断名称
function loadDiag() {
    $.ajax({
        type: 'get',
        url: path + '/commom/getIcdCodeList',
        dataType: 'json',
        success: function (data) {
            if (data != null) {
                $("#diag_code").empty();
                $("#diag_name").empty();
                var codeHtml = '<option value=""></option>';
                var nameHtml = '<option value=""></option>';
                for (var i = 0; i < data.length; i++) {
                    codeHtml += '<option value=' + data[i].code + '>' + data[i].code + '</option>';
                    if (data[i].name != '') {
                        var name = data[i].name;
                        var py = makePy(name);
                        var pingyin = ConvertPinyin(name);
                        nameHtml += '<option data-tokens="' + name + '|' + py[0] + '|' + pingyin + '" value="' + name + '">' + name + '</option>';
                    }
                }
                $("#diag_code").append(codeHtml);
                $("#diag_code").selectpicker('refresh');
                $("#diag_name").append(nameHtml);
                $("#diag_name").selectpicker('refresh');
            }
        }
    })
}

//加载手术字典
function loadOperate() {
    $.ajax({
        type: 'get',
        url: path + '/commom/loadCM3',
        dataType: 'json',
        success: function (data) {
            $("#operate_code").empty();
            $("#operate_name").empty();
            if (data != null) {
                var codeHtml = '<option value=""></option>';
                var nameHtml = '<option value=""></option>';
                for (var i = 0; i < data.length; i++) {
                    codeHtml += '<option value=' + data[i].code + '>' + data[i].code + '</option>';
                    if (data[i].name != '') {
                        var name = data[i].name;
                        var py = makePy(name);
                        var pingyin = ConvertPinyin(name);
                        nameHtml += '<option data-tokens="' + name + '|' + py[0] + '|' + pingyin + '" value="' + name + '">' + name + '</option>';
                    }
                }
                $("#operate_code").append(codeHtml);
                $("#operate_code").selectpicker('refresh');
                $("#operate_name").append(nameHtml);
                $("#operate_name").selectpicker('refresh');
            }
        }
    })
}

//动态控制限手术的值
$("#isOper").change(function () {
    var checked = $(this).is(':checked');
    if (checked) {
        $(this).val(1)
    } else {
        $(this).val("");
    }
})

//时间格式属性名集合
var commomtable = 'commomtable';
var commomtable1 = 'commomtable1';

//拼接sql
function getSql() {
    $("#whereSql").val('');
    $("#fromTableSql").val('');
    var inputValue = getInputValue();
    if (inputValue != '') {
        //from语句字符串
        var fromTableNames = '';
        //where语句字符串
        var whereNames = ' where ';
        //判断是否多表
        var tables = false;
        $('.otherTable').each(function () {
            if ($(this).val() != '') {
                tables = true;
                return false;
            }
        })
        //拼接where语句
        var name = '';
        //姓名
        var searchName = $("#name").val();
        if (searchName != '') {
            name = 'name';
            //去除前后空格
            searchName = searchName.replace(/(^\s*)|(\s*$)/g, "");
            var pinyin = /^[a-zA-Z]+$/;
            //1.逗号隔开
            if (searchName.indexOf(",") != -1 || searchName.indexOf("，") != -1) {
                if (searchName.indexOf(",") != -1) {
                    searchName = searchName.split(",");
                } else if (searchName.indexOf("，") != -1) {
                    searchName = searchName.split("，");
                }
                for (var i = 0; i < searchName.length; i++) {
                    if (searchName[i] != '') {
                        //拼接前括号
                        if (i == 0) {
                            whereNames += "(";
                        }
                        if (i != searchName.length - 1) {
                            if (searchName[i].indexOf("*") != -1) {
                                //2.带*号
                                searchName = searchName.replace(/\*/g, "_");
                                whereNames += commomtable + "." + name + " LIKE '" + searchName[i] + "' OR ";
                            } else if (pinyin.test(searchName[i])) {
                                //3.拼音缩写
                                whereNames += commomtable + ".name_cym LIKE '%" + searchName[i] + "%' OR ";
                            } else {
                                whereNames += commomtable + "." + name + " LIKE '%" + searchName[i] + "%' OR ";
                            }
                        } else {
                            if (searchName[i].indexOf("*") != -1) {
                                //2.带*号
                                searchName = searchName.replace(/\*/g, "_");
                                whereNames += commomtable + "." + name + " LIKE '" + searchName[i] + "')";
                            } else if (pinyin.test(searchName[i])) {
                                //3.拼音缩写
                                whereNames += commomtable + ".name_cym LIKE '%" + searchName[i] + "%')";
                            } else {
                                whereNames += commomtable + "." + name + " LIKE '%" + searchName[i] + "%')";
                            }
                        }
                    }
                }
                whereNames += " AND ";
            } else if (searchName.indexOf("*") != -1) {
                //2.带*号
                searchName = searchName.replace(/\*/g, "_");
                whereNames += commomtable + "." + name + " LIKE '" + searchName + "' AND ";
            } else if (pinyin.test(searchName)) {
                //3.拼音缩写
                whereNames += commomtable + ".name_cym LIKE '%" + searchName + "%' AND ";
            } else {
                whereNames += commomtable + "." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
            }
        }
        //年龄
        if ($("#age1").val() != '' && $("#age2").val() != '') {
            var ageType = $("#ageType").val();
            if (ageType == '1') {
                name = 'age';
                if ($("#age1").val() < $("#age2").val()) {
                    whereNames += "(" + commomtable + "." + name + " BETWEEN " + $("#age1").val() + " AND " + ($("#age2").val() - 1) + " or (" + commomtable + "." + name + "=" + $("#age2").val() + " and " + commomtable + ".age_month=0 and " + commomtable + ".age_day=0)) AND ";
                } else if ($("#age2").val() < $("#age1").val()) {
                    whereNames += "(" + commomtable + "." + name + " BETWEEN " + $("#age2").val() + " AND " + ($("#age1").val() - 1) + " or (" + commomtable + "." + name + "=" + $("#age1").val() + " and " + commomtable + ".age_month=0 and " + commomtable + ".age_day=0)) AND ";
                }else if($("#age1").val() == $("#age2").val()){
                    whereNames += "(" + commomtable + "." + name + " = " + $("#age1").val() + " and " + commomtable + ".age_month=0 and " + commomtable + ".age_day=0) AND ";
                }
            } else if (ageType == '2') {
                //周
                name = 'age_month';
                if ($("#age1").val() < $("#age2").val()) {
                    whereNames += "(" + 'age = 0 AND ' + commomtable + "." + name + " BETWEEN " + $("#age1").val() + " AND " + ($("#age2").val() - 1) + " OR (" + commomtable + ".age = 0 AND " + commomtable + ".age_month = " + $("#age2").val() + " AND " + commomtable + ".age_day = 0)) AND ";
                } else if ($("#age2").val() < $("#age1").val()) {
                    whereNames += "(" + 'age = 0 AND ' + commomtable + "." + name + " BETWEEN " + $("#age2").val() + " AND " + ($("#age1").val() - 1) + " OR (" + commomtable + ".age = 0 AND " + commomtable + ".age_month = " + $("#age1").val() + " AND " + commomtable + ".age_day = 0)) AND ";
                }else if($("#age1").val() == $("#age2").val()){
                    whereNames += "(" + commomtable + "." + name + " = " + $("#age1").val() + " and " + commomtable + ".age=0 and " + commomtable + ".age_day=0) AND ";
                }
            } else if (ageType == '3') {
                //天
                name = 'age_day';
                whereNames += '(age = 0 AND age_month = 0 AND ';
                if ($("#age1").val() < $("#age2").val()) {
                    whereNames += commomtable + "." + name + " BETWEEN " + $("#age1").val() + " AND " + $("#age2").val() + " AND ";
                } else if ($("#age2").val() < $("#age1").val()) {
                    whereNames += commomtable + "." + name + " BETWEEN " + $("#age2").val() + " AND " + $("#age1").val() + " AND ";
                }else if($("#age1").val() == $("#age2").val()){
                    whereNames +=  commomtable + "." + name + " = " + $("#age1").val();
                }
                whereNames += ")";
            }
        }
        //性别
        var sex = $("input[name='sex']:checked").val();
        if (sex != '') {
            name = 'sex';
            whereNames += commomtable + "." + name + " = '" + sex + "' AND ";
        }
        //病案号
        var inpatientNo = $("#inpatient_no").val();
        if (inpatientNo != '') {
            name = 'inpatient_no';
            inpatientNo = inpatientNo.replace(/(^\s*)|(\s*$)/g, "");
            var inpatientNoCheckbox = $("#inpatientNoCheckbox").is(':checked');
            if(inpatientNoCheckbox) {
                //1.逗号隔开
                if (inpatientNo.indexOf(",") != -1 || inpatientNo.indexOf("，") != -1) {
                    if (inpatientNo.indexOf(",") != -1) {
                        inpatientNo = inpatientNo.split(",");
                    } else if (inpatientNo.indexOf("，") != -1) {
                        inpatientNo = inpatientNo.split("，");
                    }
                    for (var i = 0; i < inpatientNo.length; i++) {
                        if (inpatientNo[i] != '') {
                            inpatientNo[i] = inpatientNo[i].replace(/(^\s*)|(\s*$)/g, "");
                            //拼接前括号
                            if (i == 0) {
                                whereNames += "(";
                            }
                            if (i != inpatientNo.length - 1) {
                                whereNames += commomtable + "." + name + " LIKE '%" + inpatientNo[i] + "%' OR ";
                            } else {
                                whereNames += commomtable + "." + name + " LIKE '%" + inpatientNo[i] + "%')";
                            }
                        }
                    }
                    whereNames += " AND ";
                } else {
                    whereNames += commomtable + "." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
                }
            }else{
                whereNames += commomtable + "." + name + " = '" + $("#" + name).val() + "' AND ";
            }
        }
        //ID
        if ($("#admiss_id").val() != '') {
            name = 'admiss_id';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
        }
        //出院科室
        var disDept = $("#dis_dept").val();
        if (disDept != '' && disDept != null && disDept != 'null') {
            var disDepts = "";
            for (var i = 0; i < disDept.length; i++) {
                if (disDept[i] != '') {
                    disDepts += "'" + disDept[i] + "',";
                }
            }
            disDepts = disDepts.substring(0, disDepts.length - 1);
            name = 'dis_dept';
            whereNames += commomtable + "." + name + " IN (" + disDepts + ") AND ";
        }
        //病状转归
        if ($("#dis_thing").val() != '') {
            name = 'main_dis_thing';
            whereNames += commomtable + "." + name + " = '" + $("#dis_thing").val() + "' AND ";
        }
        //入院日期
        if ($("#startTime1").val() != '' && $("#endTime1").val() != '') {
            name = 'admiss_date';
            whereNames += commomtable + "." + name + " BETWEEN '" + $("#startTime1").val() + " 00:00:00' AND '" + $("#endTime1").val() + " 23:59:59' AND ";
        }
        //出院日期
        if ($("#startTime2").val() != '' && $("#endTime2").val() != '') {
            name = 'dis_date';
            whereNames += commomtable + "." + name + " BETWEEN '" + $("#startTime2").val() + " 00:00:00' AND '" + $("#endTime2").val() + " 23:59:59' AND ";
        }
        //诊断名称
        if ($("#diag_name").val() != '') {
            name = 'main_diag_name';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#diag_name").val() + "%' AND ";
        }
        //诊断表
        //诊断码
        if ($("#diag_code").val() != '') {
            name = 'main_diag_code';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#diag_code").val() + "%' AND ";
        }
        //全部诊断
        if($("#all_diag_name").val() != ''){
            name = 'diag_name';
            whereNames += "t_diag."+name+" LIKE '%"+$("#all_diag_name").val()+"%' AND ";
        }
        //其他诊断
        if($("#other_diag_name").val() != ''){
            name = 'other_diag_name';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#other_diag_name").val() + "%' AND ";
        }
        //病理诊断
        if($("#pathology_name").val() != ''){
            name = 'pathology_name';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#pathology_name").val() + "%' AND ";
        }
        //损伤中毒
        if($("#poisoning_name").val() != ''){
            name = 'poisoning_name';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#poisoning_name").val() + "%' AND ";
        }
        //手术表
        //手术编号
        if ($("#operate_code").val() != '') {
            name = 'operate_code';
            whereNames += "t_operate." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
        }
        //手术名称
        if ($("#operate_name").val() != '') {
            name = 'operate_name';
            whereNames += "t_operate." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
        }
        //主诊编码范围
        if ($("#diag_code1").val() != '' && $("#diag_code2").val() != '') {
            name = 'main_diag_code';
            whereNames += commomtable + "." + name + " BETWEEN '" + $("#diag_cod下·下·e1").val() + "' AND '" + $("#diag_code2").val() + "' AND ";
        }
        //手术编码范围
        if ($("#operate_code1").val() != '' && $("#operate_code2").val() != '') {
            name = 'operate_code';
            whereNames += "t_operate." + name + " BETWEEN '" + $("#operate_code1").val() + "' AND '" + $("#operate_code2").val() + "' AND ";
        }
        //其他诊断编码范围
        if ($("#other_diag_name1").val() != '' && $("#other_diag_name2").val() != '') {
            name = 'other_diag_name';
            whereNames += commomtable + "." + name + " BETWEEN '" + $("#other_diag_name1").val() + "' AND '" + $("#other_diag_name2").val() + "' AND ";
        }
        //科主任
        if ($("#dept_director").val() != '') {
            name = 'dept_director';
            whereNames += commomtable1 + "." + name + " LIKE '%" + $("#dept_director").val() + "%' AND ";
        }
        //主任(副主任)
        if ($("#director").val() != '') {
            name = 'director';
            whereNames += commomtable1 + "." + name + " LIKE '%" + $("#director").val() + "%' AND ";
        }
        //住院医师
        if ($("#admiss_doctor").val() != '') {
            name = 'admiss_doctor';
            whereNames += commomtable1 + "." + name + " LIKE '%" + $("#admiss_doctor").val() + "%' AND ";
        }
        var userName = "'" + $("#userName").val() + "'";
        //拼接诊断表
        //全部诊断
        if($("#all_diag_name").val() != ''){
            fromTableNames += ' left join t_diag on '+commomtable+'.patient_id=t_diag.patient_id';
        }
        if ($("#admiss_doctor").val() != '' || $("#director").val() != '' || $("#dept_director").val() != '') {
            fromTableNames += ' left join ' + commomtable1 + ' on ' + commomtable1 + '.patient_id = ' + commomtable + '.patient_id';
        }
        //拼接手术表
        var operFlag = false;
        $(".operTable").each(function () {
            if ($(this).val() != '' && $(this).val() != 'on') {
                operFlag = true;
                return false;
            }
        })
        if (operFlag || $("#operate_code1").val() != '' || $("#operate_code2").val() != '') {
            otherTable = 't_operate';
            fromTableNames += ' left join ' + otherTable + ' on ' + commomtable + '.patient_id = ' + otherTable + '.patient_id';
        }
        //拼接主治医生
        if ($("#attending").val() != '') {
            name = 'attending';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
        }
        //拼接主治医生
        if ($("#memo").val() != '') {
            name = 'memo';
            whereNames += commomtable + "." + name + " LIKE '%" + $("#" + name).val() + "%' AND ";
        }
        //限手术
        var isOper = $("#isOper").val();
        if (isOper != '' && isOper == 1) {
            name = 'is_oper';
            whereNames += commomtable + "." + name + " = '1' AND ";
        }
        if (whereNames != ' where ') {
            whereNames = whereNames.substring(0, whereNames.length - 4);
            $("#whereSql").val(whereNames);
            $("#fromTableSql").val(fromTableNames);
        }
    }
}

//搜索功能
function search() {
    var startTime1 = $("#startTime1").val();
    var endTime1 = $("#endTime1").val();
    var startTime2 = $("#startTime2").val();
    var endTime2 = $("#endTime2").val();
    var age1 = $("#age1").val();
    var age2 = $("#age2").val();
    var diag_code1 = $("#diag_code1").val();
    var diag_code2 = $("#diag_code2").val();
    var operate_code1 = $("#operate_code1").val();
    var operate_code2 = $("#operate_code2").val();
    var other_diag_name1 = $("#other_diag_name1").val();
    var other_diag_name2 = $("#other_diag_name2").val();
    if ((age1 != '' && age2 == '') || (age1 == '' && age2 != '')) {
        toastr.warning("请输入完整年龄段!");
        return false;
    }
    if ((startTime1 != '' && endTime1 == '') || (startTime1 == '' && endTime1 != '')) {
        toastr.warning("请输入完整的入院日期时间段!");
        return false;
    }
    if ((startTime2 != '' && endTime2 == '') || (startTime2 == '' && endTime2 != '')) {
        toastr.warning("请输入完整的出院日期时间段!");
        return false;
    }
    if ((diag_code1 != '' && diag_code2 == '') || (diag_code1 == '' && diag_code2 != '')) {
        toastr.warning("请输入完整的主诊编码范围!");
        return false;
    }
    if ((operate_code1 != '' && operate_code2 == '') || (operate_code1 == '' && operate_code2 != '')) {
        toastr.warning("请输入完整的手术编码范围!");
        return false;
    }
    if ((other_diag_name1 != '' && other_diag_name2 == '') || (other_diag_name1 == '' && other_diag_name2 != '')) {
        toastr.warning("请输入完整的其他诊断编码范围!");
        return false;
    }
    getSql();
    freshTable();
}

//查看详情
function showDetail(patientId) {
    //隐藏模板选项
    //window.location.href=path+"/commom/showRecord?patientId="+patientId;
}
/*****************************************输入框边输入内容边搜索功能方法*************************************************/
//级联病案号，且长度超过4个字节才查询
function getInpatientNoContents(id) {
    var keyword = document.getElementById(id).value;
    if (keyword.length > 4) {
        var ajax = new XMLHttpRequest();
        var url = path + "/approve/getInpatientNosByName?inpatientNo=" + keyword;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result, id);
                }
            }
        }
    } else {
        clearContent(id);
        return;
    }
}

//级联诊断码
function getDiagCodeContents(id) {
    var keyword = document.getElementById(id).value;
    if (keyword.length > 2) {
        var ajax = new XMLHttpRequest();
        var url = path + "/commom/getDiagCodeContents?code=" + keyword;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result, id);
                }
            }
        }
    } else {
        clearContent(id);
        return;
    }
}

//级联诊断名称
function getDiagNameContents(id) {
    var code = "";
    var name = document.getElementById(id).value;
    if (name.length > 2) {
        var ajax = new XMLHttpRequest();
        var url = path + "/commom/getDiagCodeContents?code=" + code + "&name=" + name;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result, id);
                }
            }
        }
    } else {
        return;
    }
    clearContent('main_diag_name')
}

//级联手术编码
function getOperateCodeContents(id) {
    var keyword = document.getElementById(id).value;
    if (keyword.length > 2) {
        var ajax = new XMLHttpRequest();
        var url = path + "/commom/getOperateCodeContents?code=" + keyword;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result, id);
                }
            }
        }
    } else {
        clearContent(id);
        return;
    }
}

//级联手术名称
function getOperateNameContents(id) {
    var name = document.getElementById(id).value;
    var code = "";
    if (name.length > 2) {
        var ajax = new XMLHttpRequest();
        var url = path + "/commom/getOperateCodeContents?code=" + code + "&name=" + name;
        ajax.open("GET", url, true);
        ajax.send(null);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    var result = ajax.responseText;
                    insertContent(result, id);
                }
            }
        }
    } else {
        clearContent(id);
        return;
    }
}


//添加内容，tr td
function insertContent(content, id) {
    clearContent(id);
    setLocation(id);
    var json = JSON.parse(content);
    for (key in json) {
        var tr = document.createElement("tr");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td4 = document.createElement("td");
        tr.setAttribute("bgcolor", "#FFFAFA");
        tr.setAttribute("border", "0");
        if (id == 'main_diag_name'){
            td1.innerHTML = json[key];
            tr.appendChild(td1);
        } else {
            if (id == 'inpatient_no' || id == 'diag_code' || id == 'operate_code') {
                td2.innerHTML = key;
            } else {
                td2.innerHTML = json[key];
                td4.innerHTML = key;
            }
            tr.appendChild(td2);
            tr.appendChild(td4);
        }
        document.getElementById(id + "Content").appendChild(tr);
        tr.onmouseover = function () {
            this.className = 'mouseOver';
            document.getElementById(id).value = $(this).children("td").eq(0).html();
        };
        tr.onmouseout = function () {
            this.className = 'mouseOut';
        };
        tr.onclick = function () {
            clearContent(id);
        }
    }
}

//清楚内容方法
function clearContent(id) {
    var popNode = document.getElementById(id + "Div");
    popNode.style.border = "none";
    var contentNode = document.getElementById(id + "Content");
    var len = contentNode.childNodes.length;
    for (var i = len - 1; i >= 0; i--) {
        contentNode.removeChild(contentNode.childNodes[i]);
    }
}

//设置样式
function setLocation(id) {
    var inputNode = document.getElementById(id);
    var width = inputNode.offsetWidth;
    var left = inputNode["offsetLeft"];
    var top = inputNode.offsetHeight + inputNode["offsetTop"];
    var popNode = document.getElementById(id + "Div");
    popNode.style.border = "gray 0.5px solid";
    popNode.style.width = width + "px";
    popNode.style.top = top + "px";
    popNode.style.left = left + "px";
    document.getElementById(id + "Content_table").style.width = width + "px";
}



$(function () { $("[data-toggle='popover']").popover(); });

$("[rel=drevil]").popover({
    trigger:'manual',
    html: 'true',
    animation: false
}).on("mouseenter", function () {
    var _this = this;
    $(this).popover("show");
    $(this).siblings(".popover").on("mouseleave", function () {
        $(_this).popover('hide');
    });
}).on("mouseleave", function () {
    var _this = this;
    setTimeout(function () {
        if (!$(".popover:hover").length) {
            $(_this).popover("hide")
        }
    }, );
});　



