<script setup>
import PageTitle from "@/components/global/PageTitle.vue";
import RestrictedPageWrapper from "@/components/page/wrappers/RestrictedPageWrapper.vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import { useRouter } from 'vue-router'
import { ref } from 'vue';
import { ConfigurationSelectOption } from "@/components/displays/superuser/configuration/ConfigurationSelectObject.vue";
import ConfigurationSubPageWrapper
  from "@/views/dashboards/superUserDashboard/configuration/ConfigurationSubPageWrapper.vue";
import ConfigurationCategory from "@/components/displays/superuser/configuration/ConfigurationCategory.vue";
import ConfigurationSwitch from "@/components/displays/superuser/configuration/ConfigurationSwitch.vue";
import ConfigurationSelect from "@/components/displays/superuser/configuration/ConfigurationSelect.vue";
import ConfigurationSecretKey from "@/components/displays/superuser/configuration/ConfigurationSecretKey.vue";
import ConfigurationInput from "@/components/displays/superuser/configuration/ConfigurationInput.vue";
import Swal from "sweetalert2";
import ConfigurationError from "@/components/displays/superuser/configuration/ConfigurationError.vue";

// Get the department from the route
const router = useRouter()

const enabled = ref(false);
const module_config = ref([]);
const errors = ref([]);

const addError = (error) => {
  errors.value.push(error);
};

const getEnabled = async () => {
  await SessionUser.superUser.modules.backups.config.enabled.get().then((response) => {
    console.log('Backups enabled: ', response.data.data);
    enabled.value = response.data.data;
  }).catch((error) => {
    console.log(error);
    addError(error);
  });
};

const getModuleConfig = async () => {
  await SessionUser.superUser.modules.backups.config.get_all().then((response) => {
    let tmp_module_config = response.data.data;
    let tmp_module_config_array = [];
    for (const [key, value] of Object.entries(tmp_module_config)) {
      console.log(`${value.variable}: ${value.value}`);
      tmp_module_config_array.push({
        variable: value.variable,
        value: value.value,
      });
    }
    module_config.value = tmp_module_config_array;
    console.log(module_config.value);
  }).catch((error) => {
    console.log(error);
    addError(error);
  });
};

const getModuleConfigValue = (variable) => {
  const config = module_config.value.find((config) => config.variable === variable);
  // Log to the console, what the value was found
  if (config) {
    console.log(`Found value for ${variable}: ${config.value}`);
  } else {
    console.log(`No value found for ${variable}`);
  }
  return config ? config.value : '';
};

const load = async () => {
  await getModuleConfig();
};


const showCreateBackup = async () => {
  await Swal.fire({
    title: 'Create backup',
    text: 'Are you sure you want to create a backup?',
    showCancelButton: true,
    confirmButtonText: 'Create backup',
    showLoaderOnConfirm: true,
    preConfirm: () => {
      SessionUser.superUser.modules.backups.createBackup(null, null)
          .then((response) => {
            Swal.fire({
              title: 'Backup created',
              text: 'The backup was created successfully.',
              icon: 'success',
            });
          })
          .catch((error) => {
            Swal.fire({
              title: 'Error',
              text: 'An error occurred while creating the backup.',
              icon: 'error',
            });
          });
    },
    allowOutsideClick: () => !Swal.isLoading()
  });
};

load();
</script>

<template>
  <RestrictedPageWrapper :hasPermission="SessionUser.canAccessSuperUser()">
    <ConfigurationSubPageWrapper>
      <template #title>
        <PageTitle title="Backup configuration" subtitle="The configuration of the backup system."/>
      </template>
      <template v-if="module_config.length > 0" #content>
        <ConfigurationError v-if="errors" :errors="errors"/>
        <ConfigurationCategory
          class="mt-2"
          module="Backups"
          title="General settings"
          description="The general settings for the backup system."
          icon="fas fa-cogs"
        >
        <ConfigurationSwitch
          class="mt-2"
          module="Backups"
          title="Enable backup system"
          description="Enable or disable the backup system."
          icon="fas fa-cogs"
          :value="getModuleConfigValue('enabled') === true"
          :on-switch="SessionUser.superUser.modules.backups.config.enabled.set"
        />
        </ConfigurationCategory>

          <button class="button is-primary mt-2" @click="showCreateBackup">Create backup</button>
      </template>
    </ConfigurationSubPageWrapper>
  </RestrictedPageWrapper>
</template>

<style scoped>

</style>