$(document).ready(function () {
    var app = {
        data: {
            agents: []
        },
        methods: {
            getList: function () {
                $.ajax({
                    url: 'mgm/agent',
                    method: 'GET',
                    success: function (content) {
                        if (content.success) {
                            app.data.agents = content.data;
                            $('.table tbody').html(template($('#template-html').text(), content));
                            $('.btn-edit').click(function(){
                                var index = $(this).data('index'),
                                    agent = app.data.agents[index];
                                $('#codeInput').val(agent.code);
                                $('#descriptionInput').val(agent.description);
                                $('#agentIdInput').val(agent.agentId);
                                $('#secretInput').val(agent.secret);
                                $('#readonlyInput').val(agent.readonly);
                                $('#outerUrlInput').val(agent.outerUrl);
                            });
                            $('.btn-delete').click(function(){
                                var index = $(this).data('index'),
                                    agent = app.data.agents[index];
                                app.methods.remove(agent.code);
                            });
                            $('.btn-auth').click(function(){
                                var index = $(this).data('index'),
                                    agent = app.data.agents[index];
                                app.methods.getAuthUrl(agent.code);
                            });
                        } else {
                            alert(content.message);
                        }
                    }
                });
            },
            save: function () {
                $.ajax({
                    url: 'mgm/agent',
                    method: 'POST',
                    data: $('#form').serialize(),
                    success: function (content) {
                        if (content.success) {
                            window.location.reload();
                        } else {
                            alert(content.message);
                        }
                    }
                });
            },
            remove: function (code) {
                $.ajax({
                    url: 'mgm/agent',
                    params: {
                      code: code
                    },
                    method: 'DELETE',
                    success: function (content) {
                        if (content.success) {
                            window.location.reload();
                        } else {
                            alert(content.message);
                        }
                    }
                });
            },
            getAuthUrl: function (code) {
                $.ajax({
                    url: 'mgm/agent/auth_url?code=' + code,
                    method: 'GET',
                    success: function (content) {
                        if (content.success) {
                            app.methods.alert(code + '授权地址', content.data);
                        } else {
                            alert(content.message);
                        }
                    }
                });
            },
            alert: function(title, content) {
                $('#alertModal .modal-title').html(title);
                $('#alertModal .alert-content').val(content);
                $('#alertModal').modal();
            }
        },
        init: function () {
            $('.btn-save').click(function(){
                app.methods.save();
            });

            app.methods.getList();
        }
    };

    app.init();
});