<template>
  <v-row v-if="this.formState.ready" align="start" justify="center">
    <gz-error :error-box-message="formState.errorBoxMessage"></gz-error>
    <v-col cols="12" md="7">
      <v-card data-cy="configCard">
        <v-list two-line>
          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_DEFAULT_TRANSLATION</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_DEFAULT_TRANSLATION
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_USE_URLS</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_USE_URLS
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_DB_CONNECTION</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_DB_CONNECTION
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_FOLDER_USER_FILES</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_FOLDER_USER_FILES
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_FOLDER_BACKUP_FILES</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_FOLDER_BACKUP_FILES
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title
                >AYANOVA_FOLDER_TEMPORARY_SERVER_FILES</v-list-item-title
              >
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_FOLDER_TEMPORARY_SERVER_FILES
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_BACKUP_PG_DUMP_PATH</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_BACKUP_PG_DUMP_PATH
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_LOG_PATH</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_LOG_PATH
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title>AYANOVA_LOG_LEVEL</v-list-item-title>
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_LOG_LEVEL
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>

          <v-list-item>
            <v-list-item-content>
              <v-list-item-title class="text-wrap"
                >AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG</v-list-item-title
              >
              <v-list-item-subtitle class="text-wrap">{{
                config.ayanovA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG
              }}</v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>
        </v-list>
      </v-card>
    </v-col>
  </v-row>
</template>

<script>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*  Xeslint-disable    */
////////////////////////////////////////////////////////////////////////////////////////////////////////////

const FORM_KEY = "ops-view-configuration";

export default {
  async created() {
    let vm = this;
    try {
      vm.formState.ready = true;
      window.$gz.eventBus.$on("menu-click", clickHandler);
      generateMenu(vm);
      await vm.getDataFromApi();
      vm.formState.loading = false;
    } catch (err) {
      vm.formState.ready = true;
      window.$gz.errorHandler.handleFormError(err, vm);
    }
  },
  data() {
    return {
      config: {},
      formState: {
        ready: false,
        loading: true,
        errorBoxMessage: null,
        appError: null,
        serverError: {}
      }
    };
  },
  beforeDestroy() {
    window.$gz.eventBus.$off("menu-click", clickHandler);
  },
  methods: {
    async getDataFromApi() {
      let vm = this;
      vm.formState.loading = true;
      let url = "server-state/active-configuration";
      window.$gz.form.deleteAllErrorBoxErrors(vm);
      try {
        let res = await window.$gz.api.get(url);
        if (res.error) {
          vm.formState.serverError = res.error;
          window.$gz.form.setErrorBoxErrors(vm);
        } else {
          vm.config = res.data;

          //Update the form status
          window.$gz.form.setFormState({
            vm: vm,
            dirty: false,
            valid: true,
            loading: false
          });
        }
      } catch (error) {
        window.$gz.errorHandler.handleFormError(error, vm);
      } finally {
        window.$gz.form.setFormState({
          vm: vm,
          loading: false
        });
      }
    }
  }
};

//////////////////////
//
//
function generateMenu(vm) {
  let menuOptions = {
    isMain: true,
    icon: "$ayiInfoCircle",
    title: "ViewServerConfiguration",
    helpUrl: "ops-config-environment-variables",
    menuItems: []
  };

  window.$gz.eventBus.$emit("menu-change", menuOptions);
}

/////////////////////////////
//
//
function clickHandler(menuItem) {
  if (!menuItem) {
    return;
  }
  let m = window.$gz.menu.parseMenuItem(menuItem);
  if (m.owner == FORM_KEY && !m.disabled) {
    switch (m.key) {
      default:
        window.$gz.eventBus.$emit(
          "notify-warning",
          FORM_KEY + "::context click: [" + m.key + "]"
        );
    }
  }
}

/////////////////////////////////
//
//
async function initForm(vm) {
  //await fetchTranslatedText(vm);
}

// //////////////////////////////////////////////////////////
// //
// // Ensures UI translated text is available
// //
// async function fetchTranslatedText(vm) {
//   await window.$gz.translation.cacheTranslations(["ViewServerConfiguration"]);
// }

/*
AYANOVA_DEFAULT_TRANSLATION = ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION,
                    AYANOVA_USE_URLS = ServerBootConfig.AYANOVA_USE_URLS,
                    AYANOVA_DB_CONNECTION = ServerBootConfig.AYANOVA_DB_CONNECTION,
                    AYANOVA_FOLDER_USER_FILES = ServerBootConfig.AYANOVA_FOLDER_USER_FILES,
                    AYANOVA_FOLDER_BACKUP_FILES = ServerBootConfig.AYANOVA_FOLDER_BACKUP_FILES,
                    AYANOVA_BACKUP_PG_DUMP_PATH = ServerBootConfig.AYANOVA_BACKUP_PG_DUMP_PATH,
                    AYANOVA_LOG_PATH = ServerBootConfig.AYANOVA_LOG_PATH,
                    AYANOVA_LOG_LEVEL = ServerBootConfig.AYANOVA_LOG_LEVEL,
                    AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG = ServerBootConfig.AYANOVA_LOG_ENABLE_LOGGER_DIAGNOSTIC_LOG
*/
</script>
