<!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-edit" th:object="${cardPurchaseRecord}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">用户：</label>
                    <div class="col-sm-8">
                        <select th:field="*{userId}" name="userId" class="form-control" id="userSelect">
                        </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">
                        <select  name="cardId" class="form-control" id="cardSelect">
                        </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="price" th:field="*{price}" class="form-control" type="text">
                    </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">
                        <div class="input-group date">
                            <input name="paymentTime" th:value="${#dates.format(cardPurchaseRecord.paymentTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </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">
                        <div class="input-group date">
                            <input name="refundTime" th:value="${#dates.format(cardPurchaseRecord.refundTime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </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="paymentWay" th:field="*{paymentWay}" class="form-control" type="text">
                    </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="state" th:field="*{state}" class="form-control" type="text">
                    </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" th:field="*{notes}" class="form-control" type="text">
                    </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="version" th:field="*{version}" class="form-control" type="text">
                    </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-edit").validate({
            focusCleanup: true
        });

        //用户列表
        const userSelect = document.getElementById("userSelect");
        const selectedUserId = [[${cardPurchaseRecord.userId}]]
        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.realName;
                    userSelect.appendChild(option);
                });
                if (selectedUserId) {
                    $('#userSelect').find('option[value="' + selectedUserId + '"]').prop('selected', true);
                }
            },
            error: function () {
                console.log('获取用户数据失败');
            }
        });

        //卡种列表
        const cardSelect = document.getElementById('cardSelect');
        const priceInput = document.getElementsByName("price")[0];
        const requestData = {
            state: "0"
        };
        const selectedCardId = [[${cardPurchaseRecord.cardId}]]
        $.ajax({
            url: "/information/list",
            type: "POST",
            dataType: "json",
            data: requestData,
            success: function (data) {
                var a = $('#cardId').val()
                // 在这里处理成功获取到的数据
                data.rows.forEach(cardType => {
                    const option = document.createElement('option');
                    option.value = cardType.id;
                    option.text = cardType.name;
                    cardSelect.appendChild(option);
                })
                if (selectedCardId) {
                    $('#cardSelect').find('option[value="' + selectedCardId + '"]').prop('selected', true);
                }
                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.price;
                    }
                });
            },
            error: function (xhr, status, error) {
                // 在这里处理请求出错的情况
                console.log("Error: " + error);
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-record-edit').serialize());
            }
        }

        $("input[name='paymentTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='refundTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>
