<template>
    <form class="form vue-form" @submit.prevent="submit">
        <section class="card" role="region">
            <div class="card-header bg-primary-dark">
                <h2 class="card-title">
                    <translate key="lang_hdr">Edit Liquidsoap Configuration</translate>
                </h2>
            </div>

            <info-card>
                <p class="card-text">
                    <translate key="lang_ls_config_1">Using this page, you can customize several sections of the Liquidsoap configuration. This allows you to add advanced functionality to your station's AutoDJ.</translate>
                </p>
                <p class="card-text">
                    <translate key="lang_ls_config_2">The editable text boxes are areas where you can insert custom configuration code. The non-editable sections are automatically generated by AzuraCast.</translate>
                </p>
                <p class="card-text">
                    <translate key="lang_ls_config_3">This is an advanced feature and custom code is not officially supported by AzuraCast. You may break your station by adding custom code, but removing it should fix any issues.</translate>
                </p>
            </info-card>

            <b-overlay variant="card" :show="loading">
                <div class="card-body">
                    <b-form-fieldset v-for="(row, index) in config" :key="index" class="mb-0">
                        <b-wrapped-form-group v-if="row.is_field" :field="$v.form[row.field_name]"
                                              :id="'form_edit_'+row.field_name" input-type="textarea"
                                              :input-attrs="{class: 'text-preformatted mb-3', spellcheck: 'false', style: 'height: 150px;'}">
                        </b-wrapped-form-group>
                        <b-form-markup v-else :id="'form_section_'+index">
                            <pre class="typography-body-1">{{ row.markup }}</pre>
                        </b-form-markup>
                    </b-form-fieldset>

                    <b-button size="lg" type="submit" :variant="($v.form.$invalid) ? 'danger' : 'primary'">
                        <translate key="lang_btn_save_changes">Save Changes</translate>
                    </b-button>
                </div>
            </b-overlay>
        </section>
    </form>
</template>

<script>
import BFormFieldset from "~/components/Form/BFormFieldset";
import BWrappedFormGroup from "~/components/Form/BWrappedFormGroup";
import BFormMarkup from "~/components/Form/BFormMarkup";
import {validationMixin} from "vuelidate";
import _ from "lodash";
import mergeExisting from "~/functions/mergeExisting";
import InfoCard from "~/components/Common/InfoCard";

export default {
    name: 'StationsLiquidsoapConfig',
    components: {InfoCard, BFormFieldset, BWrappedFormGroup, BFormMarkup},
    props: {
        settingsUrl: String,
        config: Array,
        sections: Array,
    },
    mixins: [
        validationMixin
    ],
    validations() {
        let validations = {form: {}};
        _.forEach(this.sections, (section) => {
            validations.form[section] = {};
        });
        return validations;
    },
    data() {
        return {
            loading: true,
            form: {},
        }
    },
    mounted() {
        this.relist();
    },
    methods: {
        resetForm() {
            let form = {};
            _.forEach(this.sections, (section) => {
                form[section] = null;
            });
            this.form = form;
        },
        relist() {
            this.resetForm();
            this.$v.form.$reset();

            this.loading = true;
            this.axios.get(this.settingsUrl).then((resp) => {
                this.form = mergeExisting(this.form, resp.data);
                this.loading = false;
            });
        },
        submit() {
            this.$v.form.$touch();
            if (this.$v.form.$anyError) {
                return;
            }

            this.$wrapWithLoading(
                this.axios({
                    method: 'PUT',
                    url: this.settingsUrl,
                    data: this.form
                })
            ).then((resp) => {
                this.$notifySuccess();
                this.relist();
            });
        }
    }
}
</script>
