<!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="#{switchconf.label.baseConfigs}"></div>
    <div id="baseConfigs"></div>

    <div id="showAdvancedConfig" style="margin-top:210px" class="h6 form-header" th:text="#{switchconf.label.more}"></div>
    <div id="advancedConfigs" style="display: none"></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;
            <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 + '/getSwitchConf',
            type: 'GET',
            success: function(_data) {
                // 假设后端返回的数据格式为 { baseConfigs: [], advancedConfigs: [] }
                var baseConfigsHtml = '';
                var advancedConfigsHtml = '';
                var data = _data.data;

                // 渲染基础配置
                $.each(data.baseConfigs, function(index, config) {
                    baseConfigsHtml += '<div className="row">';
                    baseConfigsHtml += '<div class="col-sm-12">';
                    baseConfigsHtml += '<label class="col-sm-2 control-label">' + config.aliasName + '</label><div class="col-sm-10"><input class="config-value form-control" type="text" id="' + config.name + '" value="' + config.value + '" /></div>';
                    baseConfigsHtml += '</div>';
                    baseConfigsHtml += '</div>';
                });
                $('#baseConfigs').html(baseConfigsHtml);

                // 渲染高级配置
                $.each(data.advancedConfigs, function(index, config) {
                    advancedConfigsHtml += '<div className="row">';
                    advancedConfigsHtml += '<div class="col-sm-12">';
                    advancedConfigsHtml += '<label class="col-sm-2 control-label">' + config.aliasName + '</label><div class="col-sm-10"><input class="config-value form-control" type="text" id="' + config.name + '" value="' + config.value + '" /></div>';
                    advancedConfigsHtml += '</div>';
                    advancedConfigsHtml += '</div>';
                });
                $('#advancedConfigs').html(advancedConfigsHtml);
                $("#advancedConfigs").hide();
            },
            error: function(error) {
                console.error('Error fetching configuration:', error);
            }
        });
    });

    // JavaScript函数用于展开和折叠高级配置
    function toggleAdvancedConfig() {
        if ($("#advancedConfigs").css('display') === 'none') {
            $("#advancedConfigs").show();
            $('#showAdvancedConfig').text(i18n('switchconf.label.fold'));
        } else {
            $("#advancedConfigs").hide();
            $('#showAdvancedConfig').text(i18n('switchconf.label.more'));
        }
    }
    // 为每个高级配置的标签添加点击事件
    $('#showAdvancedConfig').click(function() {
        toggleAdvancedConfig(this);
    });

    // 保存配置
    $('#saveConfig').click(function() {
        var configs = [];
        $('input.config-value').each(function() {
            var config = {
                name: $(this).attr('id'),
                value: $(this).val().trim()
            };
            configs.push(config);
        });
        $.ajax({
            url: prefix + '/setSwitchConf', // 假设后端保存配置的接口是 /setConfig
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(configs),
            beforeSend: function () {
                $.modal.loading(i18n("common.tip.loading"));
                $.modal.disable();
            },
            success: function(response) {
                $.modal.msgSuccess(i18n('switchconf.msg.save.success'));
                $.modal.closeLoading();
                $.modal.enable();
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('配置保存失败！');
                $.modal.closeLoading();
                $.modal.enable();
            }
        });
    });


    // 重启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>