<!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('模型对象数据库配置列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <div class="select-list">
                    <div class="form-group">
                        <div class="col-sm-12">
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'模型名称 ：' + ${modelProject.modelName}"></p>
                        </div>
                        <div class="col-sm-12">
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'模型描述 ：' + ${modelProject.modelRemark}"></p>
                        </div>
                    </div>
                </div>
                <form id="formId">
                    <input type="hidden" id="projectId" name="projectId" th:value="${modelProject.id}">
                </form>
            </div>
<!--            <div class="col-sm-12 search-collapse">-->
<!--                <form id="formId">-->
<!--                    <input type="hidden" id="projectId" name="projectId" th:value="${modelProject.id}">-->
<!--                    <div class="select-list">-->
<!--                        <ul>-->
<!--                            <li>-->
<!--                                数据库名称：-->
<!--                                <input type="text" name="dbName"/>-->
<!--                            </li>-->
<!--                            <li>-->
<!--                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>-->
<!--                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>-->
<!--                            </li>-->
<!--                        </ul>-->
<!--                    </div>-->
<!--                </form>-->
<!--            </div>-->

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="add()" shiro:hasPermission="model:modelDbs:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="model:modelDbs:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="model:modelDbs:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
<!--                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="model:modelDbs:export">-->
<!--                    <i class="fa fa-download"></i> 导出-->
<!--                </a>-->
                <a class="btn btn-danger" onclick="closeItem()">
                    <i class="fa fa-reply-all"></i> 关闭
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('model:modelDbs:edit')}]];
        var removeFlag = [[${@permission.hasPermi('model:modelDbs:remove')}]];

        var sysYesNo = [[${@dict.getType('sys_yes_no')}]];

        var prefix = ctx + "model/modelDbs";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "模型对象数据库配置",
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'projectId',
                    title: '模型id',
                    visible: false
                },
                {
                    field: 'dbName',
                    title: '数据库名称'
                },
                {
                    field: 'defaultDb',
                    // visible: editFlag == 'hidden' ? false : true,
                    title: '是否默认',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return defaultDbTools(row);
                    }

                },
                {
                    field: 'driverClassName',
                    title: 'driverClass'
                },
                {
                    field: 'url',
                    title: 'URL',
                    formatter: function(value, row, index) {
                        return $.table.tooltip(value,50);
                    }
                },
                {
                    field: 'userName',
                    title: '用户名'
                },
                {
                    field: 'password',
                    title: '密码'
                },
                {
                    title: '操作',
                    align: 'left',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="pingLoadDriver(\'' + row.id + '\')"><i class="fa fa-link"></i> 连接测试 </a> ');
                        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> 编辑 </a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i> 删除 </a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        /*新增数据库连接*/
        function add() {
            var projectId = $("#projectId").val();
            $.operate.add(projectId);
        }

        /* 是否默认显示 */
        function defaultDbTools(row) {
            if (row.defaultDb == 'N') {
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.id + '\',\'' + row.projectId + '\')"></i> ';
            } else {
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.id + '\',\'' + row.projectId +'\')"></i> ';
            }
        }

        /* 是否默认 - 否*/
        function disable(id,projectId) {
            $.modal.alertWarning("请选择其它数据库为默认.");
            return;
        }

        /* 是否默认 - 是 */
        function enable(id,projectId) {
            $.modal.confirm("确认要设置为默认数据库吗？", function() {
                $.operate.post(prefix + "/setDefaultDb", { "id": id, "projectId":projectId, "defaultDb":"Y"});
            });
        }

        function pingLoadDriver(id) {
            var config = {
                url: prefix + "/pingLoadDriver",
                type: "post",
                dataType: "json",
                data: {
                    "id": id
                },
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍候...");
                },
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.alertSuccess(result.msg)
                    }  else if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                    }  else {
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                }
            };
            $.ajax(config);
        }
    </script>
</body>
</html>