<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";

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

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

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

const getModuleConfig = async () => {
  await SessionUser.superUser.modules.reCAPTCHA.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);
  });
};

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();
};

load();
</script>

<template>
  <RestrictedPageWrapper :hasPermission="SessionUser.canAccessSuperUser()">
    <ConfigurationSubPageWrapper>
      <template #title>
        <PageTitle title="reCAPTCHA configuration" subtitle="The configuration of the reCAPTCHA integration" />
      </template>
      <template v-if="module_config.length > 0" #content>
        <ConfigurationCategory
          class="mt-2"
          module="reCAPTCHA"
          title="General settings"
          description="The general settings for the reCAPTCHA integration."
          icon="fas fa-cogs"
        >
        <ConfigurationSwitch
          class="mt-2"
          module="reCAPTCHA"
          title="Enable reCAPTCHA"
          description="Enable or disable the reCAPTCHA integration."
          icon="fas fa-cogs"
          :value="getModuleConfigValue('enabled') === true"
          :on-switch="SessionUser.superUser.modules.reCAPTCHA.config.enabled.set"
        />
        </ConfigurationCategory>
        <ConfigurationCategory
          class="mt-2"
          module="reCAPTCHA"
          title="API keys"
          description="The reCAPTCHA API keys are used to verify that the user is a human, and not a bot."
          icon="fas fa-cogs"
        >
          <ConfigurationSecretKey
            class="mt-2"
            module="reCAPTCHA"
            title="Site key (v2)"
            description="The site key is used to verify that the user is a human, and not a bot."
            icon="fas fa-cogs"
            :isSet="getModuleConfigValue('site_key_v2') !== ''"
            :on-save="SessionUser.superUser.modules.reCAPTCHA.config.keys.site_key_v2.set"
          />
          <ConfigurationSecretKey
            class="mt-2"
            module="reCAPTCHA"
            title="Secret key (v2)"
            description="The secret key is used to verify that the user is a human, and not a bot."
            icon="fas fa-cogs"
            :is-set="getModuleConfigValue('secret_key_v2') !== ''"
            :on-save="SessionUser.superUser.modules.reCAPTCHA.config.keys.secret_key_v2.set"
          />
        </ConfigurationCategory>
      </template>
    </ConfigurationSubPageWrapper>
  </RestrictedPageWrapper>
</template>

<style scoped>

</style>