function clearForm2(){
    $("#form2")[0].reset();
    $("#form2").find('input[type=text],input[type=number],textarea').each(function() {
         $(this).val('');
    });
}
function clearForm(formName) {
    var formObj = document.forms[formName];
    var formEl = formObj.elements;
    for (var i = 0; i < formEl.length; i++) {
        var element = formEl[i];
        if (element.type == 'submit') {
            continue;
        }
        if (element.type == 'reset') {
            continue;
        }
        if (element.type == 'button') {
            continue;
        }
        if (element.type == 'hidden') {
            continue;
        }

        if (element.type == 'text') {
            element.value = '';
        }
        if (element.type == 'textarea') {
            element.value = '';
        }
        if (element.type == 'checkbox') {
            element.checked = false;
        }
        if (element.type == 'radio') {
            element.checked = false;
        }
        if (element.type == 'select-multiple') {
            element.selectedIndex = -1;
        }
        if (element.type == 'select-one') {
            element.selectedIndex = -1;
        }
    }
}
//验证医院名称是否重复
$("#hospitalName").blur(function(){
    var hospitalName = $("#hospitalName").val();
    var hospitalId = $("#hospitalId").val();
    if(hospitalName != '' && hospitalId == ''){
        $.ajax({
            type:'get',
            url:path+"/dict/checkHospitalName?hospitalName="+hospitalName,
            dataType:"json",
            success:function(data){
                if(data.code == 200){
                    toastr.warning(data.msg);
                    $("#hospitalName").val("");
                    $("#hospitalName").focus();
                }
            }
        })
    }
})
//保存医院信息
function addHospital() {
    var hospitalName = $("#hospitalName").val();
    if(hospitalName != '') {
        //保存医院信息
        $.ajax({
            type: 'post',
            url: path + "/dict/add",
            data: $("#form1").serialize(),
            dataType: "json",
            success: function (data) {
                if(data.code == 100){
                    toastr.success("保存成功");
                    var dictId = data.extend.dictId;
                    var currentNode = parent.zTree.getSelectedNodes()[0];
                    var treeLevel = currentNode.level;
                    var id = currentNode.id;
                    parent.menu.loadMenuTree();
                    setTimeout(function(){
                        //选中新节点
                        var node = parent.zTree.getNodeByParam("selfId",dictId);
                        parent.zTree.selectNode(node);
                        parent.zTree.expandNode(node, true);
                        parent.$("#currentTreeId").val(dictId);
                        //跳转
                        parent.$("#iframe").prop("src",path+"/dict/dictManagePage?level="+treeLevel+"&selfId="+dictId+"&operFlag=edit");
                    },500)
                }else{
                    toastr.warning(data.msg);
                }
            }
        })
    }else{
        toastr.warning("医院名称不能为空!");
        $("#hospitalName").focus();
    }
}
//系统分类级联科室,非权限系统才显示选择科室
$("#sysType").change(function(){
    $("#deptId").empty();
    var sysType = $("#sysType").val();
    var dictId = $("#parentId").val();
    if(sysType == '权限系统'){
        $("#deptDiv").hide();
    }else{
        $("#deptDiv").show();
        $.ajax({
            type: 'get',
            url: path + "/dept/selectDeptByDictId",
            data:{dictId:dictId},
            dataType: "json",
            success: function (data) {
                if(data.code == 100){
                   var html = '';
                   var depts = [];
                   if(data.extend.depts != null){
                       for (var i = 0; i < data.extend.depts.length; i++) {
                           html += '<option value="'+data.extend.depts[i].deptId+'">'+data.extend.depts[i].deptName+'</option>';
                       }
                   }
                   $("#deptId").append(html);
                   $('.selectpicker').selectpicker('refresh');
                }else{
                    toastr.error(data.msg);
                }
            }
        })
    }
})

//医院级联科室
$("#parentId").change(function(){
    $("#deptId").empty();
    var dictId = $("#parentId").val();
    var sysType = $("#sysType").val();
    if(dictId != '' && sysType != '权限系统'){
        $.ajax({
            type: 'get',
            url: path + "/dept/selectDeptByDictId",
            data:{dictId:dictId},
            dataType: "json",
            success: function (data) {
                if(data.code == 100){
                   var html = '';
                   var depts = [];
                   if(data.extend.depts != null){
                       for (var i = 0; i < data.extend.depts.length; i++) {
                           html += '<option value="'+data.extend.depts[i].deptId+'">'+data.extend.depts[i].deptName+'</option>';
                       }
                   }
                   $("#deptId").append(html);
                   $('.selectpicker').selectpicker('refresh');
                }else{
                    toastr.error(data.msg);
                }
            }
        })
    }
})

//验证系统标识不能重复
$("#sysFlag").blur(function(){
    var sysFlag = $("#sysFlag").val();
    var deptId = $("#deptId").val();
    var deptIds = '';
    if(deptId != ''){
        for (var i = 0; i < deptId.length; i++) {
            if(i != deptId.length - 1){
                deptIds += deptId[i] + ",";
            }else{
                deptIds += deptId[i]
            }

        }
    }
    var dictId = $("#dictId").val();
    if(sysFlag != '' && dictId == ''){
        $.ajax({
            type: 'get',
            url: path + "/dict/checkSysFlagOrSysNameByDeptId",
            data:{sysFlag:sysFlag,deptIds:deptIds},
            dataType: "json",
            success: function (data) {
                if(data.code == 200){
                    toastr.warning(data.msg);
                    if('系统标识已存在!' == data.msg){
                        $("#sysFlag").val("");
                        $("#sysFlag").focus();
                    }
                }
            }
        })
    }
})

//验证系统名称不能重复
$("#sysName").blur(function(){
    var sysName = $("#sysName").val();
    var deptId = $("#deptId").val();
    var dictId = $("#dictId").val();
    var deptIds = '';
    if(deptId != ''){
        for (var i = 0; i < deptId.length; i++) {
            if(i != deptId.length - 1){
                deptIds += deptId[i] + ",";
            }else{
                deptIds += deptId[i]
            }

        }
    }
    if(sysName != '' && dictId == ''){
        $.ajax({
            type: 'get',
            url: path + "/dict/checkSysFlagOrSysNameByDeptId",
            data:{sysName:sysName,deptIds:deptIds},
            dataType: "json",
            success: function (data) {
                if(data.code == 200){
                    toastr.warning(data.msg);
                    if('系统名称已存在!' == data.msg){
                        $("#sysName").val("");
                        $("#sysName").focus();
                    }
                }
            }
        })
    }
})
//保存系统信息
function addSys() {
    var sysFlag = $("#sysFlag").val();
    var sysName = $("#sysName").val();
    var deptId = $("#deptId").val();
    var sysType = $("#sysType").find("option:selected").text();
    var sort = $("#sort").val();
    if(sysFlag != ''){
        //判断父节点不能为空
        if(sysName != ''){
            if(deptId != null && deptId != '' || sysType == '权限系统'){
                $.ajax({
                    type: 'post',
                    url: path + "/dict/add",
                    data: $("#form2").serialize(),
                    dataType: "json",
                    success: function (data) {
                        if(data.code == 100){
                            toastr.success("保存成功!");
                            var currentNode = parent.zTree.getSelectedNodes()[0];
                            var treeLevel = currentNode.level;
                            parent.menu.loadMenuTree();
                            var dictId = data.extend.dictId;
                            setTimeout(function(){
                                var currentTreeId = parent.$("#currentTreeId").val();
                                //选中原来节点
                                var node = parent.zTree.getNodeByParam("selfId",currentTreeId);
                                parent.zTree.selectNode(node);
                                parent.zTree.expandNode(node, true);
                                /* 回显新节点编辑*/
                                parent.$("#iframe").prop("src",path+"/dict/dictManagePage?level="+treeLevel+"&selfId="+dictId+"&operFlag=edit");
                            },500);
                        }else{
                            toastr.warning(data.msg);
                        }
                    }
                })
            }else{
                toastr.warning("所属科室不能为空!");
            }
        }else{
            toastr.warning("系统名称不能为空!");
            $("#sysName").focus();
        }
    }else{
        toastr.warning("系统标识不能为空!");
        $("#sysFlag").focus();
    }
}
