<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增购卡记录')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-record-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="userId" class="form-control" id="userSelect" required></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">
                        <select name="cardId" class="form-control" id="cardSelect" required></select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">会员价格：</label>
                    <div class="col-sm-8">
                        <input name="memberPrice" class="form-control" type="text" disabled>
                    </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="paymentWay" 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">备注：</label>
                    <div class="col-sm-8">
                        <input name="notes" class="form-control" type="text" maxlength="255">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "record"
        $("#form-record-add").validate({
            focusCleanup: true
        });

        //用户列表
        const userSelect = document.getElementById('userSelect');
        const userrequestData = {
            status: "0",
            delFlag: "0"
        };
        $.ajax({
            url: "/system/member/list",
            type: "POST",
            dataType: "json",
            data: userrequestData,
            success: function (data) {
                // 在这里处理成功获取到的数据
                data.rows.forEach(cardType => {
                    const option = document.createElement('option');
                    option.value = cardType.id;
                    option.text = cardType.username;
                    userSelect.appendChild(option);
                });
            },
            error: function (xhr, status, error) {
                // 在这里处理请求出错的情况
                console.log("Error: " + error);
            }
        });

        //卡种列表
        const cardSelect = document.getElementById('cardSelect');
        const priceInput = document.getElementsByName("memberPrice")[0];
        let totalNumber = null;
        let cardType = null;
        let originalPrice = null;
        let memberPrice = null;
        let point = null;
        const requestData = {
            cardState: "1"
        };
        $.ajax({
            url: "/information/list",
            type: "POST",
            dataType: "json",
            data: requestData,
            success: function (data) {
                priceInput.value = data.rows[0].memberPrice
                totalNumber = data.rows[0].totalNumber
                cardType = data.rows[0].type
                originalPrice = data.rows[0].originalPrice
                memberPrice = data.rows[0].memberPrice
                point = data.rows[0].point
                // 在这里处理成功获取到的数据
                data.rows.forEach(cardType => {
                    const option = document.createElement('option');
                    option.value = cardType.id;
                    option.text = cardType.name;
                    cardSelect.appendChild(option);
                })
                cardSelect.addEventListener('change', function () {
                    const selectedOption = cardSelect.options[cardSelect.selectedIndex];
                    const selectedCardType = data.rows.find(card => card.id === parseInt(selectedOption.value));
                    if (selectedCardType && priceInput) {
                        priceInput.value = selectedCardType.memberPrice;
                        totalNumber = selectedCardType.totalNumber
                        cardType = selectedCardType.type
                        originalPrice = selectedCardType.originalPrice
                        memberPrice = selectedCardType.memberPrice
                        point = selectedCardType.point
                    }
                });
            },
            error: function (xhr, status, error) {
                // 在这里处理请求出错的情况
                console.log("Error: " + error);
            }
        });


        function submitHandler() {
            if ($.validate.form()) {
                const cardName = cardSelect.options[cardSelect.selectedIndex].text
                const request =  $('#form-record-add').serialize() + '&cardName='+cardName + '&price=' + priceInput.value
                    + '&totalNumber='+totalNumber + '&cardType=' +cardType + '&originalPrice=' +originalPrice + '&memberPrice='+memberPrice + '&point='+point
                console.log(request)
                $.operate.save(prefix + "/add", request);
            }
        }
    </script>
</body>
</html>
