<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增代金券')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-voucher-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">代金券类型：</label>
                    <div class="col-sm-8">
                        <select name="voucherType" class="form-control" th:with="type=${@dict.getType('voucher_type')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">代金券名称：</label>
                    <div class="col-sm-8">
                        <input name="voucherName" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">门槛金额：</label>
                    <div class="col-sm-8">
                        <input name="instantDiscount" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">代金券金额：</label>
                    <div class="col-sm-8">
                        <input name="voucherPrice" class="form-control" type="text" required>
                    </div>
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "voucher"
        $("#form-voucher-add").validate({
            focusCleanup: true,
            rules:{
                voucherName:{
                    required:true
                },
                voucherPrice:{
                    required: true, // 必填
                    digits: true, // 必须为整数
                    min: 1 // 最小值为1，确保为正整数
                },
                instantDiscount:{
                    required: true, // 必填
                    digits: true, // 必须为整数
                    min: 1 // 最小值为1，确保为正整数
                },
                voucherType:{
                    required:true
                },
            },
            messages: {
                voucherPrice: {
                    required: "代金券金额不能为空",
                    digits: "代金券金额必须为整数",
                    min: "代金券金额必须大于0"
                },
                instantDiscount: {
                    required: "门槛金额不能为空",
                    digits: "门槛金额必须为整数",
                    min: "门槛金额必须大于0"
                }
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                // 提交之前增加校验: 代金券金额不能大于门槛金额
                var voucherPrice = parseInt($("input[name='voucherPrice']").val());
                var instantDiscount = parseInt($("input[name='instantDiscount']").val());

                if (voucherPrice > instantDiscount) {
                    $.modal.alertWarning("代金券金额不能大于门槛金额");
                    return false;
                }

                if (voucherPrice == instantDiscount) {
                    $.modal.alertWarning("代金券金额与门槛金额不能相同");
                    return false;
                }

                $.operate.save(prefix + "/add", $('#form-voucher-add').serialize());
            }
        }
    </script>
</body>
</html>