<!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('线路配置列表')" />
    <style>
        /* 模态框样式 */
        .modal-content {
            width: 80%;
            margin: 15% auto;
        }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label th:text="#{gateways.query.purpose}"></label> <!-- 网关用途的标签 -->
                                <select name="purpose"> <!-- 下拉选择框 -->
                                    <option value="" th:text="#{options.all}"></option> <!-- 默认选项 -->
                                    <option value="0" th:text="#{gateways.query.purpose.abandoned}"></option> <!-- 0废弃 -->
                                    <option value="1" th:text="#{gateways.query.purpose.toolbar}"></option> <!-- 工具条 -->
                                    <option value="2" th:text="#{gateways.query.purpose.outgoing}"></option> <!-- 外呼 -->
                                    <option value="3" th:text="#{gateways.query.purpose.unlimited}"></option> <!-- 不限制 -->
                                </select>
                            </li>
                            <li>
                                <label th:text="#{gateways.query.gwName}"></label>
                                <input type="text" name="gwName"/>
                            </li>
                            <li>
                                <label th:text="#{gateways.query.profileName}"></label>
                                <select name="profileName">
                                    <option value="" th:text="#{options.all}"></option>
                                    <option th:each="profileData:${profileList}" th:value="${profileData.profileName}" th:text="${profileData.profileName}" ></option>
                                </select>
                            </li>
                            <li>
                                <label th:text="#{gateways.query.caller}"></label>
                                <input type="text" name="caller"/>
                            </li>
                            <li>
                                <label th:text="#{gateways.query.calleePrefix}"></label>
                                <input type="text" name="calleePrefix"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;<span th:text="#{btn.search}"></span></a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;<span th:text="#{btn.reset}"></span></a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="cc:gateways:add">
                    <i class="fa fa-plus"></i> <span th:text="#{btn.add}"></span>
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="cc:gateways:edit">
                    <i class="fa fa-edit"></i> <span th:text="#{btn.edit}"></span>
                </a>
                <a class="btn btn-info" onclick="createJson()" >
                    <i class="fa fa-search"></i> <span th:text="#{gateways.btn.createJson}"></span>
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>

            <!-- 模态框 -->
            <div id="myModal" class="modal fade" tabindex="-1" role="dialog">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" th:text="#{gateways.title.jsonData}"></h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <pre id="jsonOutput"></pre>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('cc:gateways:edit')}]];
        var prefix = ctx + "cc/gateways";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                removeUrl: prefix + "/remove",
                updateUrl: prefix + "/edit/{id}",
                modalName: i18n('gateways.modalName'),
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '${comment}',
                    visible: false
                },
                {
                    field: 'gwName',
                    title: i18n('gateways.table.gwName'),
                },
                {
                    field: 'purpose',
                    title: i18n('gateways.table.purpose'),
                    formatter: function(value, row, index) {
                        if (value == '0') {
                            return i18n('gateways.table.purpose.abandoned');
                        } else if (value == '1') {
                            return i18n('gateways.table.purpose.toolbar');
                        } else if (value == '2') {
                            return i18n('gateways.table.purpose.outgoing');
                        } else if (value == '3') {
                            return i18n('gateways.table.purpose.unlimited');
                        } else {
                            return value;
                        }
                    }
                },
                {
                    field: 'profileName',
                    title: i18n('gateways.table.profileName'),
                },
                {
                    field: 'caller',
                    title: i18n('gateways.table.caller'),
                },
                // {
                //     field: 'calleePrefix',
                //     title: i18n('gateways.table.calleePrefix'),
                // },
                {
                    field: 'gwAddr',
                    title: i18n('gateways.table.gwAddr'),
                },
                // {
                //     field: 'codec',
                //     title: i18n('gateways.table.codec'),
                // },
                {
                    field: 'gwDesc',
                    title: i18n('gateways.table.gwDesc'),
                },
                {
                    field: 'maxConcurrency',
                    title: i18n('gateways.table.maxConcurrency'),
                },
                {
                    field: 'authUsername',
                    title: i18n('gateways.table.authUsername'),
                },
                // {
                //     field: 'authPassword',
                //     title: i18n('gateways.table.authPassword'),
                // },
                // {
                //     field: 'priority',
                //     title: i18n('gateways.table.priority'),
                // },
                {
                    field: 'register',
                    title: i18n('gateways.table.register'),
                    formatter: function(value, item, index) {
                        if (item.register == '0') {
                            return '<span class="badge badge-primary">' + i18n('gateways.table.label.register0') + '</span>';
                        }
                        else if (item.register == '1') {
                            return '<span class="badge badge-success">' + i18n('gateways.table.label.register1') + '</span>';
                        }
                        else if (item.register == '2') {
                            return '<span class="badge badge-warning">' + i18n('gateways.table.label.register2') + '</span>';
                        }
                    }
                },
                {
                    title: i18n('gateways.table.opra'),
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>' + i18n('btn.edit') + '</a> ');
                        if (row.purpose == 0) {
                            actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:;" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>' + i18n('btn.del') + '</a> ');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function createJson(){

            // 获取 Bootstrap Table 数据
            const tableData = $('#bootstrap-table').bootstrapTable('getSelections');
            if ($.common.isEmpty(tableData)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(tableData);

            const jsonData = tableData.map((item, index) => ({
                uuid: String(item.id), // 生成 uuid，从 "01" 开始
                updateTime: item.updateTime,
                gatewayAddr: item.gwAddr,
                callerNumber: item.caller,
                calleePrefix: item.calleePrefix,
                priority: item.priority,
                concurrency: item.maxConcurrency,
                register: item.register,
                audioCodec: item.codec,
                authUsername: item.authUsername,
                callProfile: item.profileName,
                gwName: item.gwName
            }));

            // 将数据转换为 JSON 字符串
            const jsonString = JSON.stringify(jsonData, null, 4); // 使用缩进美化 JSON 字符串
            console.log(jsonString);

            // 显示模态框并输出 JSON 字符串
            $('#jsonOutput').text(jsonString);
            $('#myModal').modal('show');
        }

    </script>
</body>
</html>