<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 引入自定义的CSS文件 -->
    <!--    <link rel="stylesheet" href="/css/custom.css">-->
    <link rel="stylesheet" href="/css/page_common.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>身份二次校验</title>
    <style>


    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <h2>身份二次校验</h2>
    </div>
    <div class="form">
        <div class="form-group">
            <label for="idNo">身份证</label>
            <input type="text" id="idNo" placeholder="请输入您18位身份证号码">
        </div>

        <div class="error-container">
            <span class="error-message"  id="error_msg">用户名不能为空</span>
        </div>
    </div>

    <button class="login-button"  id="submitBtn"  type="submit">二次验证</button>

    <div class="warning">

    </div>
</div>

<script src="/js/jquery-3.2.1.min.js"></script>
<script src="/js/main.js"></script>
<script>

    $(document).ready(function () {
  
        // 登录表单提交
        $('#submitBtn').on('click', function (event) {
            event.preventDefault();
            $('.error-message').hide();
            const idNo = $('#idNo').val();
            if (idNo.trim() === '') {
                $('#error_msg').text('请输入身份证号码');
                $('#error_msg').show();
                return;
            }
            const idCardRegex = /^(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            if (!idCardRegex.test(idNo)) {
                $('#error_msg').text('请输入正确的身份证号码');
                $('#error_msg').show();
                return;
            }
            //获取url中的参数
            const code = getQueryString('code');
            validateIdno(idNo, code);
        });


        /*
         * ajax 表单提交验证
         */
        function validateIdno(idNo,code) {
            $.ajax({
                url: '/normalUser/idSubmit',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ idNo: idNo, code: code }),
                success: function (response) {
                    if (response.success) {
                        const pid = response.pid;
                        toDtListPage(pid);
                    } else {
                        $('#error_msg').text('登录失败: ' + response.message);
                        $('#error_msg').show();
                        //3秒后自动退回到登录页面
                        // setTimeout(function () {
                        //     window.location.href = '/';
                        // }, 3000);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#error_msg').text('登录失败: ' + errorThrown);
                    $('#error_msg').show();
                }
            });
        }


        /**
         * 返回身份证填写页面
         */
        function toDtListPage(pid) {
            window.location.href = '/normalUser/toDtList?pid=' +pid  ;
        }


        function getQueryString(name) {
            const url = new URL(window.location.href);
            return url.searchParams.get(name);
        }

    });
</script>
</body>
</html>
