import { serv_constants } from '@store/Modules/serv_constants'

import { tools } from '@tools'

import { computed, defineComponent, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
import { useRoute } from 'vue-router'

export default defineComponent({
  name: 'unsubscribe_user',
  components: {},
  setup(props, { emit }) {
    const $q = useQuasar()
    const { t } = useI18n()
    const userStore = useUserStore()
    const $route = useRoute()

    const risultato = ref('...')
    const riscode = ref(0)
    const param = ref(<any>null)

    const disiscritto = computed(() => {
      return riscode.value === serv_constants.RIS_UNSUBSCRIBED_OK
    })

    const errore = computed(() => {
      return riscode.value !== serv_constants.RIS_UNSUBSCRIBED_OK
    })

    const email = computed(() => {
      return $route.query.email
    })

    function load() {
      // console.log('load')
      param.value = { em: $route.query.em, mc: $route.query.mc, locale: tools.getLocale(), email: $route.query.email, diario_on: $route.query.diario }
      console.log('idlink = ', param.value)
      let ris = null;
      ris = userStore.unsubscribe_news_on_fielduser(param.value)
      return ris
        .then((ris: any) => {
          riscode.value = ris.code
          risultato.value = ris.msg

        }).catch((err: any) => {
          console.log('ERR = ' + err)
        })
    }

    load()

    return {
      disiscritto,
      errore,
      email,
      risultato,
      param,
    }
  }
})
