import { defineComponent, onMounted, ref } from 'vue'

import { computed } from 'vue'
import { CMyPage } from '@/components/CMyPage'
import { CCopyBtn } from '@/components/CCopyBtn'
import { CKeyAndValue } from '@/components/CKeyAndValue'
import { CGridTableRec } from '@/components/CGridTableRec'
import { tools } from '@store/Modules/tools'
import { static_data } from '@/db/static_data'

import { fieldsTable } from '@src/store/Modules/fieldsTable'
import { shared_consts } from '@/common/shared_vuejs'
import { DefaultProfile, useUserStore } from '@store/UserStore'
import { costanti } from '@costanti'
import { useQuasar } from 'quasar'
import { useNotifStore } from '@store/NotifStore'
import { INotif } from 'model'
import { IUserFields } from '@model/UserStore'
import { useI18n } from '@/boot/i18n'
import MixinUsers from '@/mixins/mixin-users'

export default defineComponent({
  name: 'userPanel',
  components: { CMyPage, CKeyAndValue, CCopyBtn },
  setup() {

    const arrfilterand: any = ref([])
    const $q = useQuasar()

    const search = ref('')
    const colVisib = ref('')
    const mycolumns = ref([])
    const myuser = ref(<IUserFields>{_id: '', username: '', name: '', surname: '', profile: DefaultProfile})
    const risultato = ref('')
    const mynotif = ref('')
    const title = ref('')
    const notifdirtype = ref(1)
    const notifidtype = ref(1)
    const notifdirtypeTest = ref(1)
    const notifidtypeTest = ref(1)

    const listnotif = ref(<any>[])
    const listnotiftype = ref(<any>[])
    const listnotiftypeTest = ref(<any>[])
    const { t } = useI18n();

    const listnotifid = computed(() => {
      if (notifdirtype.value) {
        const mylist: any = shared_consts.TypeNotifs_Arr.find((rec: any) => rec.value === notifdirtype.value)
        if (mylist) {
          for (const rec of mylist.list) {
            rec.label = t(rec.labeltrans)
          }
        }

        return mylist.list
      }
      return []
    })

    const listnotifidTest = computed(() => {
      if (notifdirtypeTest.value) {
        const mylist: any = shared_consts.TypeNotifs_Test.find((rec: any) => rec.value === notifdirtypeTest.value)
        if (mylist) {
          for (const rec of mylist.list) {
            rec.label = t(rec.labeltrans)
          }
        }

        return mylist.list
      }
      return []
    })

    const userStore = useUserStore()
    const notifStore = useNotifStore()

    const { getMyUsername } = MixinUsers()

    async function mounted() {
      //
      search.value = tools.getCookie(tools.COOK_SEARCH + 'searchpanel')
      await refresh()

      listnotif.value = shared_consts.UsersNotif_Adv_List
      for (const rec of listnotif.value) {
        rec.label = t(rec.labeltrans)
      }

      listnotiftype.value = shared_consts.TypeNotifs_Arr
      for (const rec of listnotiftype.value) {
        rec.label = t(rec.labeltrans)
      }

      listnotiftypeTest.value = shared_consts.TypeNotifs_Test
      for (const rec of listnotiftypeTest.value) {
        rec.label = t(rec.labeltrans)
      }

    }

    function changeCol(newval: any) {
      //
    }

    async function refresh() {
      if (!!search.value)
        myuser.value = await userStore.loadUserPanel(search.value)
      else
        myuser.value = {_id: '', username: '', name: '', surname: '', profile: DefaultProfile}

    }

    function db_fieldsTable() {
      return fieldsTable
    }

    async function doSearch() {
      tools.setCookie(tools.COOK_SEARCH + 'searchpanel', search.value)
      await refresh()
    }

    async function exportListaEmail() {
      risultato.value = await tools.exportListaEmail()

      tools.copyStringToClipboard($q, risultato.value, false)
    }

    async function sendNotifToUser() {

      if (!!myuser.value) {
        const notif: INotif = {
          typedir: notifdirtype.value,
          typeid: notifidtype.value,
          sender: userStore.my.username,
          dest: myuser.value.username,
          descr: mynotif.value,
          title: title.value,
        }
        await notifStore.SendNotifEvent(notif)

      }
    }

    async function sendNotifToUserTest() {

      if (!!myuser.value) {
        const notif: INotif = {
          typedir: notifdirtypeTest.value,
          typeid: notifidtypeTest.value,
          sender: userStore.my.username,
          dest: myuser.value.username,
          descr: mynotif.value,
          title: title.value,
        }
        await notifStore.SendNotifEvent(notif)

      }
    }

    onMounted(mounted)

    return {
      arrfilterand,
      fieldsTable,
      search,
      tools,
      shared_consts,
      doSearch,
      changeCol,
      myuser,
      refresh,
      mycolumns,
      colVisib,
      exportListaEmail,
      sendNotifToUser,
      sendNotifToUserTest,
      risultato,
      mynotif,
      title,
      notifdirtype,
      notifidtype,
      notifdirtypeTest,
      notifidtypeTest,
      listnotif,
      listnotiftype,
      listnotiftypeTest,
      listnotifid,
      listnotifidTest,
      getMyUsername,
    }
  }
})


