<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="base/resource"/>
    <title>集群用户 - Big Whale</title>
    <link href="../../../libs/bootstrap-select/bootstrap-select.min.css" rel="stylesheet">
    <link href="../../../css/common.css" rel="stylesheet"/>
</head>
<body ng-app="content-app" ng-controller="content-controller">
<div class="modal fade" id="editDlg" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">{{editItem.id ? "编辑" : "新增"}}</h4>
            </div>
            <form class="form-horizontal" ng-submit="onSaveItem()">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="modal_select_uid" class="col-sm-4 control-label">
                            平台用户&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <select id="modal_select_uid" class="form-control selectpicker show-tick" data-live-search="true" ng-model="editItem.uid"
                                    ng-options="item.id as item.username for item in userList" required>
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_select_clusterId_new" class="col-sm-4 control-label">
                            集群&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5" ng-if="editItem.id == null">
                            <select id="modal_select_clusterId_new" class="form-control selectpicker show-tick" data-actions-box="true" data-select-all-text="全选" data-deselect-all-text="反选"
                                    ng-model="editItem.clusterId" ng-options="item.id as item.name for item in clusterList" title="请选择" multiple required>
                            </select>
                        </div>
                        <div class="col-sm-5" ng-if="editItem.id != null">
                            <select id="modal_select_clusterId_edit" class="form-control selectpicker show-tick"
                                    ng-model="editItem.clusterId" ng-options="item.id as item.name for item in clusterList" required>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_input_queue" class="col-sm-4 control-label">
                            队列&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_queue" class="form-control" ng-model="editItem.queue" required="required">
                        </div>
                        <div class="col-sm-3">
                            <p class="help-block">多个队列用“,”分隔</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_input_proxy_user" class="col-sm-4 control-label">
                            代理用户
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_proxy_user" class="form-control" ng-model="editItem.user">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary" id="btn_submit">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="container-fluid animated fadeInDown">
    <div class="bw-nav">
        <div class="row">
            <ol class="breadcrumb">
                <li>集群管理</li>
                <li class="active">集群用户</li>
            </ol>
            <a class="bw-refresh" onclick="location.reload()" title="刷新">
                <i class="fa fa-refresh" style="font-size: 18px"></i>
            </a>
        </div>
    </div>
    <div class="bw-body">
        <div class="row">
            <div class="col-md-12">
                <div class="col-md-12 search-form">
                    <form class="form-inline" ng-submit="options.mypage.toFirst()">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="队列包含" ng-model="options.query.queue">
                        </div>
                        <div class="form-group" th:if="${session.user.check('cluster_cluster_user_manage')}">
                            <label for="query_select_uid">用户</label>
                            <select id="query_select_uid" class="selectpicker show-tick" data-live-search="true"
                                    ng-model="options.query.uid" ng-options="item.id as item.username for item in userList">
                                <option value="">不限</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="query_select_clusterId">集群</label>
                            <select id="query_select_clusterId" class="selectpicker show-tick"
                                    ng-model="options.query.clusterId" ng-options="item.id as item.name for item in clusterList">
                                <option value="">不限</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success">搜索</button>
                            <button type="button" class="btn btn-primary" ng-click="onNewItem()">新增</button>
                        </div>
                    </form>
                </div>
                <div class="col-md-12 table-responsive">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>
                                        操作
                                    </th>
                                    <th>
                                        用户
                                    </th>
                                    <th>
                                        集群
                                    </th>
                                    <th>
                                        队列
                                    </th>
                                    <th>
                                        代理用户
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                            <tr ng-repeat="item in options.items">
                                <td>
                                    <p class="operate">
                                        <span ng-click="onEditItem(item)">
                                            <i class="glyphicon glyphicon-pencil" aria-hidden="true" title="编辑"></i>
                                        </span><br/>
                                        <span class="remove" id="{{item.id}}_removeBtn" ng-click="onRemoveItem(item)">
                                            <i class="glyphicon glyphicon-trash" aria-hidden="true" title="删除"></i>
                                        </span>
                                    </p>
                                </td>
                                <td>
                                    {{userMap[item.uid].username}}
                                </td>
                                <td>
                                    {{clusterMap[item.clusterId].name}}
                                </td>
                                <td>
                                    {{item.queue}}
                                </td>
                                <td>
                                    {{item.user}}
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <mypage ng-model="options"></mypage>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>
</div>
<script src="../../../libs/bootstrap-select/bootstrap-select.min.js" type="text/javascript"></script>
<script src="../../../libs/bootstrap-select/i18n/defaults-zh_CN.js" type="text/javascript"></script>
<script src="../../../libs/angular-1.3.9/angular.min.js" type="text/javascript"></script>
<script src="../../../libs/angular-1.3.9/myangular.js" type="text/javascript"></script>
<script src="../../../js/common.js" type="text/javascript"></script>
<script th:inline="javascript">
    var app = angular.module('content-app', []);
    registerHttpInterceptor(app);
    registerPage(app);
    app.controller('content-controller', function ($scope, $http) {
        var clusterId = [[${clusterId}]];
        getCluster($scope, $http);
        getAuthUser($scope, $http);

        $scope.onNewItem = function () {
            $scope.editItem = {};
            $('#editDlg').modal();
        };

        $scope.onEditItem = function(item) {
            $scope.editItem = angular.copy(item);
            $('#editDlg').modal();
        };

        $('#editDlg').on('shown.bs.modal', function () {
            $('.selectpicker').selectpicker('refresh');
        });

        $scope.onSaveItem = function() {
            $('#btn_submit').attr('disabled', 'true');
            if ($.isArray($scope.editItem.clusterId)) {
                var clusterIds = '';
                for (var pos = 0; pos < $scope.editItem.clusterId.length; pos++) {
                    clusterIds += $scope.editItem.clusterId[pos];
                    if (pos < $scope.editItem.clusterId.length - 1) {
                        clusterIds += ",";
                    }
                }
                $scope.editItem.clusterId = clusterIds;
            }
            $http.post('./save.api', $scope.editItem)
                .success(function(data) {
                    $('#btn_submit').removeAttr('disabled');
                    $('#editDlg').modal('hide');
                    $scope.reload();
                    swal({
                        title: '保存成功',
                        type: 'success',
                        showConfirmButton: false,
                        timer: 1500
                    });
                })
                .error(function (data) {
                    $('#btn_submit').removeAttr('disabled');
                    $('#editDlg').modal('hide');
                });
        };

        $scope.onRemoveItem = function(item) {
            removeItem('./delete.api', item, $http, $scope.reload);
        };

        $('#modal_select_uid').change(function () {
            if (this.value) {
                $('#modal_input_proxy_user').val($scope.userList[this.value].username);
                $scope.editItem.user = $scope.userList[this.value].username;
            } else {
                $('#modal_input_proxy_user').val('');
                $scope.editItem.user = null;
            }
        });

        // 获取数据列表
        $scope.options = {
            query: {
                clusterId: clusterId
            },
            listUrl: './getpage.api'
        };

        // 刷新
        $scope.reload = function () {
            $scope.options.mypage.reload();
        };

    });
</script>
</body>
</html>