{% 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>Directories</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">Temp Files</th><td>[[ settings.temp ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Comic Files</th><td>[[ settings.comics ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Log Files</th><td>[[ settings.log ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Backup Files</th><td>[[ settings.backup ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Plugin Files</th><td>[[ settings.plugins ]]</td>
                        </tr>
                        <tr>
                            <th scope="row">Image Files</th><td>[[ settings.images ]]</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">

                            {{ directories_form.csrf_token }}

                            <div class="mb-3">
                                {{ directories_form.temp_directory.label }}
                                {{ directories_form.temp_directory(class_='form-control', placeholder=directories_form.temp_directory.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ directories_form.comics_directory.label }}
                                {{ directories_form.comics_directory(class_='form-control', placeholder=directories_form.comics_directory.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ directories_form.log_directory.label }}
                                {{ directories_form.log_directory(class_='form-control', placeholder=directories_form.log_directory.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ directories_form.backup_directory.label }}
                                {{ directories_form.backup_directory(class_='form-control', placeholder=directories_form.backup_directory.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ directories_form.plugins_directory.label }}
                                {{ directories_form.plugins_directory(class_='form-control', placeholder=directories_form.plugins_directory.label.text) }}
                            </div>
                            <div class="mb-3">
                                {{ directories_form.images_directory.label }}
                                {{ directories_form.images_directory(class_='form-control', placeholder=directories_form.images_directory.label.text) }}
                            </div>
                        
                    </div>
                    <div class="modal-footer">
                        {{ directories_form.update_directory_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='directory') }}')
                .then(res => {
                    this.settings = res.data.results
                    document.getElementById('temp_directory').value = res.data.results.temp;
                    document.getElementById('comics_directory').value = res.data.results.comics;
                    document.getElementById('log_directory').value = res.data.results.log;
                    document.getElementById('backup_directory').value = res.data.results.backup;
                    document.getElementById('plugins_directory').value = res.data.results.plugins;
                    document.getElementById('images_directory').value = res.data.results.images;
                })
                .catch(err => console.log(err))
        },
    },
    delimiters: ["[[","]]"]
})

{% endblock %}