{% extends "settings_page.html" %}

{% block header_script_files %}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
{% endblock %}

{% block settings_pane %}

<div id="app">
    <settings ref="settings" v-bind:settings="settings"></settings>
    <modals ref="modal" v-bind:settings="settings"></modals>
</div>

{% endblock %}

{% block script %}

Vue.component('settings', {
    props: ['settings'],
    template: `
    <div class="row r-10 m-2">
        <div class="col col-12">
            <div class="row">
                <div class="col-sm-12 col-md-6 text-center text-md-start">
                    <h2>Application</h2>
                </div>
                <div class="col-sm-12 col-md-6 text-center text-md-end">
                    <button type="button" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#settingsModal">Modify Settings</button>
                </div>
            </div>
            <hr />
            <div class="row">
                <table class="table table-striped">
                    <tbody>
                        <tr>
                            <th scope="row">Server Port</th><td>[[ settings.server_port ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Open Registration</th><td>[[ settings.open_registration ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Comicvine API Key</th><td>[[ settings.comicvine_api_key ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Logging Level</th><td>[[ settings.log_level ]]</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    `,
    delimiters: ["[[","]]"]
})

Vue.component('modals', {
    props: ['settings'],
    template: `
    <div>
        <div class="modal" id="settingsModal" role="dialog" aria-labelledby="settingsModal" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="notesModalTitle">
                            Modify App Settings
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>

                    <form method="POST">
                    <div class="modal-body">

                            {{ settings_form.csrf_token }}

                            <div class="mb-3">
                                {{ settings_form.server_port.label }}
                                {{ settings_form.server_port(class_='form-control', placeholder=settings_form.server_port.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ settings_form.open_registration }}
                                {{ settings_form.open_registration.label }}
                            </div>
                            <div class="mb-3">
                                {{ settings_form.comicvine_api_key.label }}
                                {{ settings_form.comicvine_api_key(class_='form-control', placeholder=settings_form.comicvine_api_key.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ settings_form.log_level.label }}
                                {{ settings_form.log_level(class_='form-control') }}
                            </div>

                    </div>
                    <div class="modal-footer">
                        {{ settings_form.update_app_button(class_='btn btn-success') }}
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    `,
    methods: {},
    delimiters: ["[[","]]"]
})

var app = new Vue({
    el: '#app',
    data: {
        settings: [],
        mdset: [],
    },
    created() {
        this.getSettings()
    },
    methods: {
        getSettings() {
            axios.get('{{ url_for('api.api_get_settings_single_section', section='app') }}')
                .then(res => {
                    this.settings = res.data.results;
                    document.getElementById('server_port').value = res.data.results.server_port;
                    document.getElementById('open_registration').checked = res.data.results.open_registration;
                    document.getElementById('comicvine_api_key').value = res.data.results.comicvine_api_key;
                    document.getElementById('log_level').value = res.data.results.log_level;
                })
                .catch(err => console.log(err))
        },   
    },
    delimiters: ["[[","]]"]
})

{% endblock %}