layui.use(['table', 'layer', 'laydate', 'jquery', 'form'], function () {
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    form.on('submit(formDemo)', function (data) {
        var keyWord = $("#keyWord").val();
        var id = $("#id").val();
        specialPositionsData(data,keyWord,id); //新增方法
        return false;
    });
});

//新增
function specialPositionsData(formData,keyWord,id) {
    var jobName = $("#jobName").val();
    var jobDescrip = $("#jobDescrip").val();
    var jobHazard = $("#jobHazard").val();
    var phyCycle = $("#phyCycle").val();
    debugger;
    if(isNull(jobName)){
        layer.alert("岗位名称不能为空！！！");
    }
    if(isNull(jobDescrip)){
        layer.alert("岗位描述不能为空！！！");
    }
    if(isNull(jobHazard)){
        layer.alert("接触危害及因素不能为空！！！");
    }
    if(isNull(phyCycle)){
        layer.alert("体检周期不能为空！！！！");
    }else{
        if(phyCycle == 0){
            layer.alert("体检周期不能为0 ！！！");
            return false;
        }else if(phyCycle < 0){
            layer.alert("体检周期不能小于0 ！！！");
            return false;
        }else{
            let tf = isIntNum(phyCycle);
            if(!tf){
                layer.alert("体检周期请输入整数 ！！！");
                return false;
            }
        }
    }
    var typeUrl = "";
    var msg = "";
    if(keyWord == '2'){
        if(id == '0'){
            layer.alert("数据异常！");
            return false;
        }
        typeUrl = "/specialPositions/modifySpecialPost";
        msg = "修改";
    }else{
        typeUrl = "/specialPositions/addSpecialPost";
        msg = "新增";
    }

    // 加载提示
    var addLoadingMsg = top.layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});
    // form请求地址
    $.ajax({
        type: 'post',
        async: false, // 默认异步true,false表示同步
        url: ctxPath + typeUrl, // 请求地址
        contentType: "application/json; charset=utf-8",
        dataType: 'json', // 服务器返回数据类型
        data: JSON.stringify(formData.field), //获取提交的表单字段
        success: function (data) {
            var count = data.count;//1数据已存在，2数据添加成功，3数据添加失败
            top.layer.close(addLoadingMsg);
            if(count==1){
                //layer.alert(");
                layer.msg("该特殊岗位已存在！", {icon: 2});
            }else if(count==2){
                //layer.alert("特殊岗位"+msg+"成功！");
                layer.msg("特殊岗位"+msg+"成功！", {icon: 1});
                setTimeout(function () {
                    parent.layer.closeAll();
                },1500)
            }else{
                layer.msg("特殊岗位"+msg+"失败！", {icon: 2});
                //layer.alert("特殊岗位"+msg+"失败！");
            }

        },
        error: function (XMLHttpRequest, textStatus, e) {
            layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
            top.layer.close(addLoadingMsg); //再执行关闭
        }
    });
}



