import { CMyFieldDb } from '@src/components/CMyFieldDb'
import { CTitleBanner } from '@src/components/CTitleBanner'
import { CProfile } from '@src/components/CProfile'
import { CSkill } from '@src/components/CSkill'
import { tools } from '@tools'
import { defineComponent, onMounted, ref } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useRouter } from 'vue-router'
import { useGlobalStore } from '@store/globalStore'
import { useI18n } from 'vue-i18n'
import { toolsext } from '@store/Modules/toolsext'
import { useQuasar } from 'quasar'
import { costanti } from '@costanti'


export default defineComponent({
  name: 'ProfileMy',
  components: { CProfile, CTitleBanner, CMyFieldDb, CSkill },
  props: {},
  setup() {
    const userStore = useUserStore()
    const $router = useRouter()
    const globalStore = useGlobalStore()
    const $q = useQuasar()
    const { t } = useI18n();

    const log = ref('');
    const log2 = ref('');
    const numval = ref(20);
    const loading = ref(false);
    const filtroutente = ref(<any[]>[])

    function getpayment() {
      return userStore.my.profile.paymenttypes
    }

    function profile() {
      return userStore.my.profile
    }

    function eliminaAccount() {

      $q.dialog({
        message: t('reg.cancellami', { sitename: t('ws.sitename') }),
        ok: {
          label: t('dialog.yes'),
          push: true
        },
        cancel: {
          label: t('dialog.cancel')
        },
        title: t('pages.profile')
      }).onOk(() => {
        $q.dialog({
          message: t('reg.cancellami_2', { sitename: t('ws.sitename') }),
          ok: {
            label: t('dialog.yes'),
            push: true
          },
          cancel: {
            label: t('dialog.cancel')
          },
          title: t('pages.profile')
        })
          .onOk(() => {
            globalStore.DeleteRec({ table: toolsext.TABUSER, id: userStore.my._id })
              .then((ris: any) => {
                if (ris) {
                  tools.showPositiveNotif($q, t('reg.account_cancellato'))
                  userStore.logout()
                  $router.replace('/')
                } else
                  tools.showNegativeNotif($q, t('db.recfailed'))

              })
          })
      })
    }

    function mounted() {
      filtroutente.value = [
        { userId: userStore.my._id }
      ]
    }

    async function runTest(ind: number) {
      loading.value = true;
      log.value = '';
      try {
        // Assicurati che l'URL di base di axios sia impostato correttamente
        // Puoi configurarlo globalmente o usare direttamente import.meta.env.VITE_API_BASE_URL
        const response = await globalStore.loadTest(ind, numval.value);
        if (ind === 1)
          log.value = response.data.log;
        else if (ind === 2)
          log2.value = response.data.log;
      } catch (error) {
        console.error('Errore nella chiamata a /testpao:', error);

        log.value = 'Errore: ' + error.message;
      } finally {
        loading.value = false;
      }
    }

    onMounted(mounted)

    return {
      eliminaAccount,
      profile,
      getpayment,
      tools,
      costanti,
      filtroutente,
      log,
      log2,
      loading,
      runTest,
      numval,
    }
  }
})
