<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('授权配置')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
    <style>
    </style>
</head>

<body>
<div class="main-content">
    <div class="h4 form-header" th:text="#{license.conf.header}"></div>
    <div id="baseConfigs" class="row">
        <div class="form-group col-sm-12">
            <label class="col-sm-2 control-label" th:text="#{license.fingerprint.label}"></label>
            <div class="col-sm-10">
                <textarea id="fingerprintValue" name="fingerprintValue" rows="5" class="form-control" readonly></textarea>
            </div>
        </div>
        <div class="form-group col-sm-12">
            <label class="col-sm-2 control-label" th:text="#{license.info.label}"></label>
            <div class="col-sm-10">
                <div class="form-group row">
                    <div class="col-sm-5">
                        <label class="col-sm-3 control-label" th:text="#{license.info.modules.label}"></label>
                        <div class="col-sm-9">
                            <input id="modules" class="form-control" readonly>
                        </div>
                    </div>
                    <div class="col-sm-5">
                        <label class="col-sm-3 control-label" th:text="#{license.info.concurrency.label}"></label>
                        <div class="col-sm-9">
                            <input id="concurrency" class="form-control" readonly>
                        </div>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-5">
                        <label class="col-sm-3 control-label" th:text="#{license.info.updateTime.label}"></label>
                        <div class="col-sm-9">
                            <input id="updateTime" class="form-control" readonly>
                        </div>
                    </div>
                    <div class="col-sm-5">
                        <label class="col-sm-3 control-label" th:text="#{license.info.expireDate.label}"></label>
                        <div class="col-sm-9">
                            <input id="expireDate" class="form-control" readonly>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group col-sm-12">
            <label class="col-sm-2 control-label" th:text="#{license.conf.label}"></label>
            <div class="col-sm-10">
                <textarea id="licenseValue" name="licenseValue" rows="8" class="form-control"></textarea>
            </div>
        </div>
    </div>

    <!-- 保存按钮 -->
    <div class="row" ></div>
    <div class="row" >
        <div class="col-sm-offset-5 col-sm-10">
            <button id="saveConfig" type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i><span th:text="#{switchconf.btn.save}"></span></button>&nbsp;&nbsp;
            &nbsp;&nbsp;
            <button id="restartFs" type="button" class="btn btn-sm btn-primary" ><i class="fa fa-check"></i><span th:text="#{switchconf.btn.restart}"></span></button>&nbsp;
        </div>
    </div>
</div>



<th:block th:include="include :: footer" />
<th:block th:include="include :: layout-latest-js" />
<script>
    // 页面加载完成后，发送AJAX请求获取配置数据
    var prefix = ctx + "cc/fsconf";
    $(function() {
        $.ajax({
            url: prefix + '/getLicenseConf',
            type: 'GET',
            success: function(_data) {
                // 假设后端返回的数据格式为 {"certValue":"xxx"}
                var data = _data.data;
                $('#fingerprintValue').val(data.fingerprintValue);
                $('#licenseValue').val(data.licenseValue);
                // 解析 licenseInfo
                var info = data.licenseInfo || {};
                $('#modules').val(info.modules || '');
                $('#concurrency').val(info.concurrency || '');
                // 时间戳转 yyyy-MM-dd HH:mm:ss
                function fmt(ts) {
                    if (!ts) return '';
                    var d = new Date(ts);
                    return d.getFullYear() + '-' +
                        String(d.getMonth() + 1).padStart(2, '0') + '-' +
                        String(d.getDate()).padStart(2, '0');
                }
                $('#updateTime').val(fmt(info.updateTime));
                $('#expireDate').val(fmt(info.expireDate));
            },
            error: function(error) {
                console.error('Error fetching configuration:', error);
            }
        });
    });

    // 保存配置
    $('#saveConfig').click(function() {
        var config = {
            licenseValue: $("#licenseValue").val().trim()
        };
        $.ajax({
            url: prefix + '/setLicenseConf', // 假设后端保存配置的接口是 /setConfig
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(config),
            success: function(response) {
                $.modal.msgSuccess(i18n('switchconf.msg.save.success'));
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('配置保存失败！');
            }
        });
    });


    // 重启freeswitch
    $('#restartFs').click(function() {
        $.ajax({
            url: prefix + '/restartFs',
            type: 'GET',
            beforeSend: function () {
                $.modal.loading(i18n("common.tip.loading"));
                $.modal.disable();
            },
            success: function(response) {
                if (response.code === 0) {
                    $.modal.msgSuccess(i18n('switchconf.msg.restart.success'));
                } else {
                    $.modal.msgError(response.msg || '重启失败！');
                }
                $.modal.closeLoading();
                $.modal.enable();
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('重启失败！');
                $.modal.closeLoading();
                $.modal.enable();
            }
        });
    });

</script>

</body>
</html>