<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">
        <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="zlmc" name="zlmc" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label required" style="width: 70px">单册页数</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="dcys" name="dcys" lay-verify="required" autocomplete="off" class="layui-input" maxlength="9" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');">
            </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="number" id="dycs" name="dycs" lay-verify="required" autocomplete="off" class="layui-input" maxlength="9" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');">
            </div>
            <label class="layui-form-label required " style="width: 70px">单页费用(元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input style="position:absolute;z-index:2;width:90%;" id="dcfys" name="dcfys" class="layui-input" maxlength="单页费用不能为空！">
                <select name="dcfy" id="dcfy" lay-filter="dcfy">
                    <option value="null" selected disabled hidden>请选择单价</option>
                    <option value="0.12">0.12</option>
                    <option value="0.96">0.96</option>
                    <option value="7.68">7.68</option>
                </select>
            </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 id="hj" name="hj" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>

        <!-- 右侧悬浮按钮 -->
        <div class="right-bottom-btn">
            <button type="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="@{/lib/xm-select-v1.2.2/xm-select.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', 'tableSelect'], function () {
        var form = layui.form;
        // 获取输入框元素
        const dcysInput = document.getElementById('dcys');
        const dycsInput = document.getElementById('dycs');
        const dcfySelect = document.getElementById('dcfy');
        const dcfysInput = document.getElementById('dcfys');
        const hjInput = document.getElementById('hj');

        // 添加事件监听器
        dcysInput.addEventListener('input', updateHj);
        dycsInput.addEventListener('input', updateHj);
        dcfysInput.addEventListener('input', updadcfys);
        dcfySelect.addEventListener('change', updadcfy);
        // 更新合计值的函数

        function updadcfy(){
            // 进来了
            document.getElementById('dcfys').value = dcfySelect.value;
            updateHj();
        }
        function updadcfys(){
            document.getElementById('dcfy').value = '';
            updateHj();
        }

        function updateHj() {
            console.log('进来了');
            const dcysValue = parseFloat(dcysInput.value) || 0;
            const dycsValue = parseFloat(dycsInput.value) || 0;
            /*const dcfyValue = parseFloat(dcfySelect.value) || 0;*/
            const dcfysValue = parseFloat(dcfysInput.value) || 0;
            // 正则表达式：输入必须是一个不以零开头的整数，或者是单独的零
            const regex = /^(0|[1-9][0-9]*)$/;
            // 字符串转换
            const dcysi = String(dcysInput.value).trim();
            const dycsi = String(dycsInput.value).trim();
            if (!regex.test(dcysi) && dcysInput.value !== ''){
                layer.msg('单册页数，输入无效，请确保它是一个有效的整数，且无前导零!',{icon :2});
                setTimeout(function () {
                    dcysInput.value = '';
                },1500);
                return ;
            }
            if (!regex.test(dycsi) && dycsInput.value !== ''){
                layer.msg('打印册数，输入无效，请确保它是一个有效的整数，且无前导零!',{icon :2});
                setTimeout(function () {
                    dycsInput.value = '';
                },1500);
                return ;
            }


            if (dcysValue > 0 && dycsValue > 0) {
                hjInput.value = (dcysValue * dycsValue * dcfysValue).toFixed(2);

            } else {
                hjInput.value = '0.00'; // 如果有任一输入无效则将合计设置为0
            }
        }
/*        $("#dcfys").blur(function () {
            // 输入框失去焦点
            if ($("#bzje").val() != '') {
                var bzje = parseFloat($("#bzje").val());
                var sl = $("#slval").val();
                var slInt = parseInt(sl, 10);
                var bhsje = (bzje * 100) / (100 + slInt);
                $("#bhsje").val(bhsje.toFixed(2));
                $("#sf").val((bzje - bhsje).toFixed(2));
            }
        });*/
        form.on('select(dcfy)', function (data) {
            var dcysValue = document.getElementById('dcys').value;
            var dycsValue = document.getElementById('dycs').value;
            if (!dcysValue || !dycsValue) {
                alert("请先填写单册页数和打印册数！");
                this.value = ''; // 重置下拉框的选中项
                form.render('select'); // 重新渲染select
            } else {
                updadcfy(); // 更新合计值
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            parent.setPrintln(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>