<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新建</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="formDemo">
    <div class="layui-form layuimini-form">
<!--        <input type="hidden" id="fysq" name="fysq" class="layui-input">-->
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 70px">人员姓名</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="xm" name="xm"  lay-reqtext="人员姓名不能为空" autocomplete="off" class="layui-input" style="display:none">
                <select name="xms" id="xms" lay-filter="xms" lay-search>
                    <option value="">请选择</option>
                    <option th:each="item:${list}" th:text="${item.xm}" th:value="${item.id}"></option>
                </select>
            </div>
            <label class="layui-form-label required" style="width: 70px">电话</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="phone" name="phone" lay-reqtext="电话不能为空" lay-verify="required" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 70px">毕业学校</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="byxx" name="byxx" lay-verify="required" lay-reqtext="毕业学校不能为空" autocomplete="off" class="layui-input" disabled>
            </div>
            <label class="layui-form-label" style="width: 70px">专业</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="zy" name="zy" lay-verify="required" lay-reqtext="专业不能为空" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label " style="width: 70px">学历</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="xl" name="xl" lay-verify="required" lay-reqtext="学历不能为空" autocomplete="off" class="layui-input" disabled>
               <!-- <select name="xl" id="xl" >
                    <option value="">请选择</option>
                    <option th:each="item:${xlList}" th:text="${item.name}" th:value="${item.name}"></option>
                </select>-->
            </div>
            <label class="layui-form-label required" style="width: 70px">身份证号</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="sfzh" name="sfzh" lay-verify="required" lay-reqtext="身份证号不能为空" autocomplete="off" class="layui-input" maxlength="18" disabled>
            </div>
        </div>

        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 60px">聘用时间</label>
            <div class="layui-form-item layui-inline" style="width: auto">
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="qsrq" id="qsrq" lay-verify="required|date" placeholder="开始日期"
                           lay-reqtext="开始日期不能为空" autocomplete="off" class="layui-input" disabled>
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="jsrq" id="jsrq" lay-verify="required|date" placeholder="结束日期"
                           lay-reqtext="结束日期不能为空" autocomplete="off" class="layui-input" disabled>
                </div>
                <br>
                <tip>日期格式：yyyy-MM-dd</tip>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 60px">项目起始时间</label>
            <div class="layui-form-item layui-inline" style="width: auto">
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="xmqssj" id="xmqssj" lay-verify="required|date" placeholder="开始日期"
                           lay-reqtext="项目开始日期不能为空" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="xmjsrq" id="xmjsrq" lay-verify="required|date" placeholder="结束日期"
                           lay-reqtext="项目结束日期不能为空" autocomplete="off" class="layui-input">
                </div>
                <br>
                <tip>日期格式：yyyy-MM-dd</tip>
            </div>
        </div>

        <!-- 右侧悬浮按钮 -->
        <div class="right-bottom-btn">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">
                <i class="layui-icon layui-icon-ok"></i>确定
            </button>
            <button class="layui-btn" lay-filter="cancleBtn">
                <i class="layui-icon layui-icon-close"></i>取消
            </button>
        </div>
    </div>
</form>

<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'layer', 'laydate'], function () {
        var form = layui.form,
            laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#xmqssj',
            value: new Date()
        });
        laydate.render({
            elem: '#xmjsrq',
            done: function(value, date, endDate){
                qsrq = $("#xmqssj").val();
                jsrq = $("#xmjsrq").val();
                if(qsrq != "" && jsrq != "") {
                    if(qsrq > jsrq) {
                        Message.warning("结束日期不能早于起始日期！", 1500);
                        $("#xmjsrq").val("");
                    }
                }
            }
        });

        //监听学历
        // form.on('select(xl)', function (data) {
        //     let text = data.elem.selectedOptions[0].text;
            // $("#xlmc").val(text);
        // });

        // 监听下拉框
        form.on('select(xms)',function (){
            console.log('进来了')
            var  va = document.getElementById('xms').value;
            console.log(va);
          // 根据姓名 id 查询 并赋值
            AjaxUtil.get({
                url: AjaxUtil.ctx + "lsUser/getId/" + va,
                contentType: "application/json",
                data: JSON.stringify(va),
                success: function (res) {
                    console.log(res.data.rows.xm);

                   $('#byxx').val(res.data.rows.byxy);
                   $('#zy').val(res.data.rows.zy);
                   $('#qsrq').val(res.data.rows.qpsj);
                   $('#jsrq').val(res.data.rows.dqsj);
                   $('#sfzh').val(res.data.rows.sfzh);
                   $('#xl').val(res.data.rows.xl);
                   $('#phone').val(res.data.rows.phone);
                   $('#xm').val(res.data.rows.xm);

                },
                error: function (error) {
                    layer.alert('服务器睡着了', {icon: 5});
                    console.log(error);
                }
            });

        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
/*            // 获取身份证号
            var sfzhm = document.getElementById('sfzh').value;

            // 身份证号的正则表达式
            var sfzhRegex = /^(\d{15}|\d{18}|\d{17}[xX])$/;

            // 检查身份证号是否合法
            if (!sfzhRegex.test(sfzhm)) {
                // 弹窗提示身份证号不合法
                layer.msg('身份证号格式不正确，请重新输入', {icon: 2});
                return false; // 阻止表单提交
            }

            // 获取起始时间和截止时间的值
            var qsrq = data.field.qsrq;
            var jsrq = data.field.jsrq;

            // 将起始时间和截止时间转换为 JavaScript 的日期对象
            var qsrqDate = new Date(qsrq);
            var jsrqDate = new Date(jsrq);

            // 计算日期差距
            var timeDiff = jsrqDate - qsrqDate;
            var dayDiff = timeDiff / (1000 * 3600 * 24); // 转换为天数

            // 计算月份数差距
            var monthsDiff = Math.floor(dayDiff / 30);

            // 判断日期范围是否超过三个月
            if (monthsDiff > 3) {
                // 弹窗提示日期范围超过三个月
                layer.msg('起始时间和截止时间的间隔不能超过三个月', {icon: 2});
                return false; // 阻止表单提交
            }*/
            qsrq = $("#xmqssj").val();
            jsrq = $("#xmjsrq").val();
            var xmqssj = new Date(qsrq);
            var xmjsrq = new Date(jsrq);
            var timeDiff = xmjsrq - xmqssj;
            var dayDiff = timeDiff / (1000 * 3600 * 24); // 转换为天数
            var monthsDiff = Math.floor(dayDiff / 30);
            if (monthsDiff > 3){
                Message.warning('起始时间和截至时间的间隔不能超过三个月',1500);
                return false;
            }
            if(qsrq != "" && jsrq != "") {
                if(qsrq > jsrq) {
                    Message.warning("结束日期不能早于起始日期！", 1500);
                    $("#xmjsrq").val("");
                    return false;
                }
            }
            if ( qsrq === jsrq){
                Message.warning("项目开始日期和项目结束日期不能小于一天",1500);
                return false;

            }

            // 如果身份证号和日期范围都合法，执行提交操作
            parent.setMember(data.field);
            parent.setUpdateFlag();
            parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭子窗口
        });

        // 监听取消按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });
</script>
</body>
</html>
