<!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('profile列表')" />
    <style>
        .modal-custom-size {
            width: 800px; /* 自定义宽度 */
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add('internal')" shiro:hasPermission="cc:profileconf:add">
                <i class="fa fa-plus"></i> <span th:text="#{profileconf.btn.add.internal}"></span>
            </a>
            <a class="btn btn-success" onclick="$.operate.add('external')" shiro:hasPermission="cc:profileconf:add">
                <i class="fa fa-plus"></i> <span th:text="#{profileconf.btn.add.external}"></span>
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="cc:profileconf:edit">
                <i class="fa fa-edit"></i> <span th:text="#{profileconf.btn.edit}"></span>
            </a>
            <a class="btn btn-info single disabled" onclick="showProfileStatus()" shiro:hasPermission="cc:profileconf:status">
                <i class="fa fa-search"></i> <span th:text="#{profileconf.btn.status}"></span>
            </a>
            <a class="btn btn-primary single disabled" onclick="startProfile()" shiro:hasPermission="cc:profileconf:start">
                <i class="fa fa-check-circle-o"></i> <span th:text="#{profileconf.btn.start}"></span>
            </a>
            <a class="btn btn-danger single disabled" onclick="stopProfile()" shiro:hasPermission="cc:profileconf:stop">
                <i class="fa fa-close"></i> <span th:text="#{profileconf.btn.stop}"></span>
            </a>
            <a class="btn btn-primary single disabled" onclick="restartProfile()" shiro:hasPermission="cc:profileconf:restart">
                <i class="fa fa-refresh"></i> <span th:text="#{profileconf.btn.restart}"></span>
            </a>
            <a class="btn btn-info single disabled" onclick="showExtnum()" shiro:hasPermission="cc:profileconf:status">
                <i class="fa fa-search"></i> <span th:text="#{profileconf.btn.showextnum}"></span>
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
    <div class="modal fade" id="customModal" tabindex="-1" role="dialog" aria-labelledby="customModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-custom-size" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="customModalLabel" th:text="#{profileconf.label.tips}"></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" id="modal-body">
                    自定义模态框内容...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal" th:text="#{btn.close}"></button>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('cc:profileconf:edit')}]];
    var statusFlag = [[${@permission.hasPermi('cc:profileconf:status')}]];
    var startFlag = [[${@permission.hasPermi('cc:profileconf:start')}]];
    var stopFlag = [[${@permission.hasPermi('cc:profileconf:stop')}]];
    var restartFlag = [[${@permission.hasPermi('cc:profileconf:restart')}]];
    var prefix = ctx + "cc/fsconf";

    $(function() {
        var options = {
            url: prefix + "/profileList",
            createUrl: prefix + "/profileconf/add/{id}",
            updateUrl: prefix + "/profileconf/edit/{id}",
            modalName: i18n('profileconf.modalName'),
            columns: [{
                checkbox: true
            },
                {
                    field: 'profileName',
                    title: i18n('profileconf.table.profileName')
                },
                {
                    field: 'sipPort',
                    title: i18n('profileconf.table.sipPort')
                },
                {
                    field: 'sipId',
                    title: i18n('profileconf.table.sipId')
                },
                {
                    title: i18n('profileconf.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.profileName + '\')"><i class="fa fa-edit"></i>' + i18n('profileconf.btn.edit') + '</a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + statusFlag + '" href="javascript:void(0)" onclick="showProfileStatus(\'' + row.profileName + '\')"><i class="fa fa-search"></i>' + i18n('profileconf.btn.status') + '</a>');
                        actions.push('<a class="btn btn-primary btn-xs ' + startFlag + '" href="javascript:void(0)" onclick="startProfile(\'' + row.profileName + '\')"><i class="fa fa-check-circle-o"></i>' + i18n('profileconf.btn.start') + '</a>');
                        actions.push('<a class="btn btn-danger btn-xs ' + stopFlag + '" href="javascript:void(0)" onclick="stopProfile(\'' + row.profileName + '\')"><i class="fa fa-close"></i>' + i18n('profileconf.btn.stop') + '</a>');
                        actions.push('<a class="btn btn-primary btn-xs ' + restartFlag + '" href="javascript:void(0)" onclick="restartProfile(\'' + row.profileName + '\')"><i class="fa fa-refresh"></i>' + i18n('profileconf.btn.restart') + '</a>');
                        actions.push('<a class="btn btn-info btn-xs ' + statusFlag + '" href="javascript:void(0)" onclick="showExtnum(\'' + row.profileName + '\')"><i class="fa fa-search"></i>' + i18n('profileconf.btn.showextnum') + '</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function showProfileStatus(profileName) {
        if ($.common.isEmpty(profileName)) {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if ($.common.isEmpty(row)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(row);
            profileName = row.profileName
        }
        console.log(profileName)
        $.ajax({
            url: prefix + '/getProfileStatus?profileName=' + profileName,
            type: 'GET',
            success: function(response) {
                var data = response.data.data; // 假设这是一个数组
                var statusHtml = '<table class="table table-striped">'; // 使用Bootstrap的表格样式
                statusHtml += '<thead><tr><th>' + i18n('profileconf.status.name') + '</th><th>' + i18n('profileconf.status.type') + '</th><th>' + i18n('profileconf.status.data') + '</th><th>' + i18n('profileconf.status.state') + '</th></tr></thead><tbody>';

                // 遍历数组，拼接每一行的数据
                data.forEach(function(item) {
                    statusHtml += '<tr>';
                    statusHtml += '<td>' + item.name + '</td>';
                    statusHtml += '<td>' + item.type + '</td>';
                    statusHtml += '<td>' + item.data + '</td>';
                    statusHtml += '<td>' + item.state + '</td>';
                    statusHtml += '</tr>';
                });

                statusHtml += '</tbody></table>';

                // 将拼接好的HTML设置到模态框的body中
                $("#modal-body").html(statusHtml);
                $('#customModal').modal('show');
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('系统异常！');
            }
        });
    }


    function startProfile(profileName) {
        if ($.common.isEmpty(profileName)) {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if ($.common.isEmpty(row)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(row);
            profileName = row.profileName
        }
        console.log(profileName)
        $.ajax({
            url: prefix + '/startProfile?profileName=' + profileName,
            type: 'GET',
            success: function(response) {
                $("#modal-body").html(response.data.msg);
                $('#customModal').modal('show');
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('系统异常！');
            }
        });
    }

    function stopProfile(profileName) {
        if ($.common.isEmpty(profileName)) {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if ($.common.isEmpty(row)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(row);
            profileName = row.profileName
        }
        console.log(profileName)
        $.ajax({
            url: prefix + '/stopProfile?profileName=' + profileName,
            type: 'GET',
            success: function(response) {
                $("#modal-body").html(response.data.msg);
                $('#customModal').modal('show');
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('系统异常！');
            }
        });
    }

    function restartProfile(profileName) {
        if ($.common.isEmpty(profileName)) {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if ($.common.isEmpty(row)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(row);
            profileName = row.profileName
        }
        console.log(profileName)
        $.ajax({
            url: prefix + '/restartProfile?profileName=' + profileName,
            type: 'GET',
            success: function(response) {
                $("#modal-body").html(response.data.msg);
                $('#customModal').modal('show');
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('系统异常！');
            }
        });
    }


    function showExtnum(profileName) {
        if ($.common.isEmpty(profileName)) {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if ($.common.isEmpty(row)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(row);
            profileName = row.profileName
        }
        console.log(profileName)
        $.ajax({
            url: prefix + '/getExtnumList?profileName=' + profileName,
            type: 'GET',
            success: function(response) {
                var data = response.data.data; // 假设这是一个数组
                var statusHtml = '<table class="table table-striped">'; // 使用Bootstrap的表格样式
                statusHtml += '<thead><tr><th>' + i18n('profileconf.extrnum.user') + '</th><th>' + i18n('profileconf.extrnum.networkIp') + '</th><th>' + i18n('profileconf.extrnum.status') + '</th><th>' + i18n('profileconf.extrnum.agent') + '</th></tr></thead><tbody>';

                // 遍历数组，拼接每一行的数据
                data.forEach(function(item) {
                    statusHtml += '<tr>';
                    statusHtml += '<td>' + item.user + '</td>';
                    statusHtml += '<td>' + item.networkIp + ':' + item.networkPort + '</td>';
                    statusHtml += '<td>' + item.status + '</td>';
                    statusHtml += '<td>' + item.agent + '</td>';
                    statusHtml += '</tr>';
                });

                statusHtml += '</tbody></table>';

                // 将拼接好的HTML设置到模态框的body中
                $("#modal-body").html(statusHtml);
                $('#customModal').modal('show');
            },
            error: function(error) {
                console.error('Error saving configuration:', error);
                $.modal.msgError('系统异常！');
            }
        });
    }

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