<!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="../../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_input_code" class="col-sm-4 control-label">
                            code&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_code" class="form-control" ng-show="!editItem.id" ng-model="editItem.code" required autocomplete="off"/>
                            <input class="form-control" title="{{editItem.code}}" ng-show="editItem.id" ng-model="editItem.code" required readonly/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_input_name" class="col-sm-4 control-label">
                            名称&nbsp;<B>*</B>
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_name" class="form-control" type="text" ng-model="editItem.name" required autocomplete="off"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="modal_input_url" class="col-sm-4 control-label">
                            url
                        </label>
                        <div class="col-sm-5">
                            <input id="modal_input_url" class="form-control" type="text" ng-model="editItem.url" autocomplete="off"/>
                        </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="code/名称" ng-model="query.searchText">
                        </div>
                        <div class="item-inl form-group">
                            <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">
                            <thead>
                                <tr>
                                    <th>
                                        操作
                                    </th>
                                    <th>
                                        code
                                    </th>
                                    <th>
                                        名称
                                    </th>
                                    <th>
                                        url
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="item in items" ng-show="item.code.indexOf(query.searchText) >= 0 || item.name.indexOf(query.searchText) >= 0">
                                    <td>
                                        <p class="operate">
                                            <span ng-click="onEditItem(item)">
                                                <i class="glyphicon glyphicon-pencil" aria-hidden="true" title="编辑"></i>
                                            </span><br/>
                                            <a class="remove" id="{{item.id}}_removeBtn" ng-click="onRemoveItem(item)">
                                                <i class="glyphicon glyphicon-trash" aria-hidden="true" title="删除"></i>
                                            </a>
                                        </p>
                                    </td>
                                    <td>
                                        {{item.code}}
                                    </td>
                                    <td>
                                        {{item.name}}
                                    </td>
                                    <td>
                                        {{item.url}}
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<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 type="text/javascript">
    var app = angular.module('content-app', []);
    registerHttpInterceptor(app);
    app.controller('content-controller', function ($scope, $http) {
        $scope.query = {searchText: ''};
        $scope.items = [];
        $scope.editItem = null;
        $scope.targetItem = null;

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

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

        $scope.onSaveItem = function () {
            $('#btn_submit').attr('disabled', 'true');
            $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);
        };

        $scope.reload = function () {
            $http.get("./list.api")
                .success(function (data) {
                    $scope.items = data;
                });
        };

        $scope.reload();
    });
</script>
</body>
</html>