<!DOCTYPE html>
<html>

<head>
    <title>Telegram Bot Configuration</title>
    <!-- <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet"> -->
    <link href="/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="/css/vuetify.min.css" rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>

<body>
    <div id="app">
        <v-app>
            <v-app-bar app>
                <v-toolbar-title>Telegram Bot Configuration</v-toolbar-title>
                <v-spacer></v-spacer>
                <v-btn color="success" @click="storeConfig()">Store & Restart</v-btn>
                <v-checkbox label="Expert Mode" v-model="EXPERTMODE"></v-checkbox>
            </v-app-bar>
            <v-content>
                <v-form>
                    <v-tabs v-model="configTab" next-icon="mdi-arrow-right-bold-box-outline" prev-icon="mdi-arrow-left-bold-box-outline" show-arrows>
                        <v-tab key="botSettings">Bot Settings</v-tab>
                        <v-tab v-show="EXPERTMODE" key="notificationConfig">Notification Configuration</v-tab>
                    </v-tabs>
                    <v-tabs-items v-model="configTab">
                        <v-tab-item key="botSettings">
                            <v-container>
                                <v-row>
                                    <v-col cols="12" sm="12" md="12">
                                        <v-text-field v-model="configData.bottoken" label="Bot-Token"></v-text-field>
                                    </v-col>
                                </v-row>
                                <p>Targets:</p>
                                <v-row>
                                    <v-btn color="success" @click="addDeliveryMode()">Add</v-btn>
                                </v-row>
                                <v-row v-for="(deliveryMode, index) in configData.deliveryModes" :key="deliveryMode._id" style="border-bottom: 2px solid black;">
                                    <pre>Index: {{ index }}</pre>
                                    <v-col cols="12" sm="12" md="12">
                                        <v-btn color="error" @click="configData.deliveryModes.splice(index, 1)" icon><v-icon>mdi-delete</v-icon></v-btn>
                                    </v-col>
                                    <v-col cols="6" sm="6" md="4">
                                        <v-text-field v-model="deliveryMode.name" label="Name"></v-text-field>
                                        <v-autocomplete
                                            v-model="deliveryMode.preset"
                                            :items="presetSearchItems"
                                            :loading="!presetSearchItems.length > 0"
                                            color="white"
                                            hide-no-data
                                            dense
                                            label="Profile"
                                            placeholder="Start typing to Search"
                                            prepend-icon="mdi-database-search"
                                        ></v-autocomplete>
                                    </v-col>
                                </v-row>
                            </v-container>
                        </v-tab-item>
                        <v-tab-item key="notificationConfig">
                            <v-container>
                                <b>Routing Paramters:</b>
                                <v-row>
                                <v-col cols="12" sm="12" md="6">
                                    <v-text-field label="Daemon Endpoint URL" v-model="configData.pager.url"></v-text-field>
                                </v-col>
                            </v-container>
                        </v-tab-item>
                    </v-tabs-items>
                </v-form>
            </v-content>
        </v-app>
    </div>

    <script src="/js/vue/vue.js"></script>
    <script src="/js/vue/vuetify.js"></script>
    <script src="/js/vue/vue-resource_1.5.1.js"></script>
    <script>
        new Vue({
            el: '#app',
            vuetify: new Vuetify(),
            http: { root: '/' },
            data() {
                return {
                    EXPERTMODE: false,
                    configTab: null,
                    configData: {
			            "bottoken": "",
                        "pager": {
                            "url": "",
                        },
                        "menuSupport": false,
                        "deliveryModes": []
                    },
                    presetSearchItems: [],
                }
            },
            created() {
                this.loadPresets()
                this.loadConfig()
            },
            methods: {
                loadPresets() {
                    this.$http.get(window.location.pathname + 'api/deliveryPresets')
                    .then(response => {
                        this.presetSearchItems = response.body.map(x => { return {
                            text: x.name,
                            value: x.key,
                        }})
                    }, response => {
                    })
                },
                loadConfig() {
                    this.$http.get(window.location.pathname + 'config').then(response => {
                        const newConfig = response.body
                        newConfig.deliveryModes = newConfig.deliveryModes.map((x) => {
                            x._id = btoa(JSON.stringify(x))
                            return x
                        })
                        this.configData = newConfig
                    }, response => {
                    })
                },
                storeConfig() {
                    const storeConfig = JSON.parse(JSON.stringify(this.configData))
                    this.$http.post(window.location.pathname + 'config', storeConfig)
                    .then(response => {
                        document.body.style = 'display:none'
                        setTimeout(() => window.location.reload(), 1e3)
                    this.$http.post(window.location.pathname + 'restart') 
                    .then(() => {
                    })
                    })
                },
                addDeliveryMode() {
                    this.configData.deliveryModes.push({
                        name: "",
                        preset: null
                    })
                },
            }
        })
    </script>
</body>

</html>