{% 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>Mail Settings</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">{{ mail_form.mail_use.label.text }}</th><td>[[ settings.mail_use ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">{{ mail_form.mail_username.label.text }}</th><td>[[ settings.mail_username ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">{{ mail_form.mail_password.label.text }}</th><td>**********</td>
                        </tr>
                        <tr>
                            <th scope="row">{{ mail_form.mail_default_sender.label.text }}</th><td>[[ settings.mail_default_sender ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">{{ mail_form.mail_server.label.text }}</th><td>[[ settings.mail_server ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">{{ mail_form.mail_port.label.text }}</th><td>[[ settings.mail_port ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">{{ mail_form.mail_use_ssl.label.text }}</th><td>[[ settings.mail_use_ssl ]]</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 Directory Settings
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>

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

                            {{ mail_form.csrf_token }}

                            <div class="mb-3">
                                {{ mail_form.mail_use }}
                                {{ mail_form.mail_use.label }}
                            </div>
                            <div class="mb-3">
                                {{ mail_form.mail_username.label }}
                                {{ mail_form.mail_username(class_='form-control', placeholder=mail_form.mail_username.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ mail_form.mail_password.label }}
                                {{ mail_form.mail_password(type='password', class_='form-control', placeholder=mail_form.mail_password.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ mail_form.mail_default_sender.label }}
                                {{ mail_form.mail_default_sender(class_='form-control', placeholder=mail_form.mail_default_sender.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ mail_form.mail_server.label }}
                                {{ mail_form.mail_server(class_='form-control', placeholder=mail_form.mail_server.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ mail_form.mail_port.label }}
                                {{ mail_form.mail_port(class_='form-control', placeholder=mail_form.mail_port.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ mail_form.mail_use_ssl }}
                                {{ mail_form.mail_use_ssl.label }}
                            </div>
                        
                    </div>
                    <div class="modal-footer">
                        {{ mail_form.update_mail_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() {
            console.log('hooray')
            axios.get('{{ url_for('api.api_get_settings_single_section', section='mail') }}')
                .then(res => {
                    this.settings = res.data.results

                    document.getElementById('mail_use').checked = res.data.results.mail_use;
                    document.getElementById('mail_username').value = res.data.results.mail_username;
                    document.getElementById('mail_password').value = res.data.results.mail_password;
                    document.getElementById('mail_default_sender').value = res.data.results.mail_from;
                    document.getElementById('mail_server').value = res.data.results.mail_server;
                    document.getElementById('mail_port').value = res.data.results.mail_port;
                    document.getElementById('mail_use_ssl').checked = res.data.results.mail_use_ssl;
                })
                .catch(err => console.log(err))
        },
    },
    delimiters: ["[[","]]"]
})

{% endblock %}