import { CMyFieldDb } from '@src/components/CMyFieldDb'
import { CMyFieldRec } from '@src/components/CMyFieldRec'
import { CTitleBanner } from '@src/components/CTitleBanner'
import { CProfile } from '@src/components/CProfile'
import { CSkill } from '@src/components/CSkill'
import { CMySelectCity } from '@src/components/CMySelectCity'
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'
import { static_data } from '@src/db/static_data'
import { IUserFields } from 'model'


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

    const myuser = ref({})

    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') }),
        cancel: {
          label: t('dialog.cancel')
        },
        ok: {
          label: t('dialog.yes'),
          push: true
        },
        title: t('pages.profile')
      }).onOk(() => {
        $q.dialog({
          message: t('reg.cancellami_2', { sitename: t('ws.sitename') }),
          cancel: {
            label: t('dialog.cancel')
          },
          ok: {
            label: t('dialog.yes'),
            push: true
          },
          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'))

              })
          })
      })
    }

    async function loadProfile() {
      // Carica il profilo di quest'utente
      await userStore.loadUserProfile({ username: userStore.my.username }).then((ris) => {
        myuser.value = ris
        // console.log('myuser._id', ris)
      })
    }


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

    onMounted(mounted)

    return {
      eliminaAccount,
      profile,
      getpayment,
      tools,
      costanti,
      static_data,
      filtroutente,
      myuser,
      t,
      userStore,
      site: globalStore.site,
      globalStore,
    }
  }
})
