<!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"/>
    <link href="../css/script_list.css" rel="stylesheet">
</head>
<body ng-app="content-app" ng-controller="content-controller">
<div class="container-fluid animated fadeInDown">
    <div class="bw-nav">
        <div class="row">
            <ol class="breadcrumb">
                <li>脚本管理</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.text">
                        </div>
                        <div class="form-group" th:if="${session.user.root}">
                            <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_type">类型</label>
                            <select id="query_select_type" class="selectpicker show-tick"
                                    ng-model="options.query.type" ng-options="item.value as item.name for item in scriptTypeList">
                                <option value="">不限</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="query_select_agentId">代理</label>
                            <select id="query_select_agentId" class="selectpicker show-tick"
                                    ng-model="options.query.agentId" ng-options="item.id as item.name for item in agentList">
                                <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">
                    <div class="table-responsive">
                        <table class="table table-hover table-condensed">
                            <thead>
                            <tr>
                                <th>
                                    操作
                                </th>
                                <th th:if="${session.user.root}">
                                    用户
                                </th>
                                <th class="order" ng-click="sort('name', asc)">
                                    <div>名称</div>
                                    <div>
                                        <i class="glyphicon glyphicon-triangle-top" ng-class="(title === 'name' && asc === true) ? 'active' : ''"></i>
                                        <i class="glyphicon glyphicon-triangle-bottom" ng-class="(title === 'name' && asc === false) ? 'active' : ''"></i>
                                    </div>
                                </th>
                                <th>
                                    描述
                                </th>
                                <th class="order" ng-click="sort('type', asc)">
                                    <div>类型</div>
                                    <div>
                                        <i class="glyphicon glyphicon-triangle-top" ng-class="(title === 'type' && asc === true) ? 'active' : ''"></i>
                                        <i class="glyphicon glyphicon-triangle-bottom" ng-class="(title === 'type' && asc === false) ? 'active' : ''"></i>
                                    </div>
                                </th>
                                <th>
                                    集群/代理
                                </th>
                                <th>
                                    脚本代码
                                </th>
                                <th class="order" ng-click="sort('totalMemory', asc)">
                                    <div>资源占用</div>
                                    <div>
                                        <i class="glyphicon glyphicon-triangle-top" ng-class="(title === 'totalMemory' && asc === true) ? 'active' : ''"></i>
                                        <i class="glyphicon glyphicon-triangle-bottom" ng-class="(title === 'totalMemory' && asc === false) ? 'active' : ''"></i>
                                    </div>
                                </th>
                                <th class="order" ng-click="sort('createTime', asc)">
                                    <div>创建时间</div>
                                    <div>
                                        <i class="glyphicon glyphicon-triangle-top" ng-class="(title === 'createTime' && asc === true) ? 'active' : ''"></i>
                                        <i class="glyphicon glyphicon-triangle-bottom" ng-class="(title === 'createTime' && asc === false) ? 'active' : ''"></i>
                                    </div>
                                </th>
                                <th class="order" ng-click="sort('updateTime', asc)">
                                    <div>修改时间</div>
                                    <div>
                                        <i class="glyphicon glyphicon-triangle-top" ng-class="(title === 'updateTime' && asc === true) ? 'active' : ''"></i>
                                        <i class="glyphicon glyphicon-triangle-bottom" ng-class="(title === 'updateTime' && asc === false) ? 'active' : ''"></i>
                                    </div>
                                </th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr ng-repeat="item in options.items | orderBy : title : !asc">
                                <td>
                                    <p class="operate">
                                        <span ng-click="onExecute(item)">
                                            <i class="glyphicon glyphicon-play" aria-hidden="true" title="执行"></i>
                                        </span><br/>
                                        <span ng-click="onEditItem(item)">
                                            <i class="glyphicon glyphicon-pencil" aria-hidden="true" title="编辑"></i>
                                        </span><br/>
                                        <span ng-click="onOpenLog(item)">
                                            <i class="glyphicon glyphicon-time" 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 th:if="${session.user.root}">
                                    <p>{{userMap[item.uid].username}}</p>
                                </td>
                                <td>
                                    <span class="script-name">{{item.name}}</span>
                                </td>
                                <td>
                                    <span class="script-desc">{{item.description}}</span>
                                </td>
                                <td>
                                    {{scriptTypeMap[item.type].name}}
                                </td>
                                <td>
                                    <span ng-if="agentMap[item.agentId]">{{agentMap[item.agentId].name}}</span>
                                    <span ng-if="clusterMap[item.clusterId]">{{clusterMap[item.clusterId].name}}</span>
                                </td>
                                <td>
                                    <kbd class="script-script">{{item.script}}</kbd>
                                </td>
                                <td>
                                    <span ng-if="item.type != 0">
                                        内存：{{item.totalMemory}}M<br/>
                                        cores：{{item.totalCores}}
                                    </span>
                                </td>
                                <td>
                                    {{item.createTime}}
                                </td>
                                <td>
                                    {{item.updateTime}}
                                </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);
    var user = [[${session.user}]];
    app.controller('content-controller', function ($scope, $http) {
        getCluster($scope, $http);
        if (user.root) {
            getAuthUser($scope, $http);
        }
        appendScriptType($scope);
        getAgent($scope, $http);
        defineSort($scope);

        $scope.onNewItem = function () {
            if (parent.xadmin) {
                parent.xadmin.reloadScript = $scope.reload;
                parent.xadmin.add_tab('脚本新增', 'script/edit.html');
            } else {
                window.open('./edit.html', '脚本新增');
            }
        };

        $scope.onEditItem = function (item) {
            if (parent.xadmin) {
                parent.xadmin.reloadScript = $scope.reload;
                parent.xadmin.add_tab('脚本编辑 - ' + item.name, 'script/edit.html?id=' + item.id);
            } else {
                window.open('./edit.html?id=' + item.id, '脚本编辑 - ' + item.name);
            }
        };

        $scope.onExecute = function (item) {
            $http.post('execute.api', item)
                .success(function (data) {
                    swal({
                        title: '脚本执行请求成功',
                        text: '准备开始执行脚本',
                        type: 'info',
                        showConfirmButton: false,
                        timer: 1500,
                        position: 'top-end'
                    }).then(function () {
                        if (parent.xadmin) {
                            parent.xadmin.open(item.name + ' - 执行日志', 'script/cmd_record/list.html?id=' + data.id);
                        } else {
                            window.open('./cmd_record/list.html?id=' + data.id);
                        }
                    });
                });
        };

        $scope.onOpenLog = function (item) {
            if (parent.xadmin) {
                parent.xadmin.open(item.name + ' - 执行日志', 'script/cmd_record/list.html?scriptId=' + item.id);
            } else {
                window.open('./cmd_record/list.html?scriptId=' + item.id);
            }
        };

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

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

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

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