import type { PropType} from 'vue';
import { computed, defineComponent, onMounted, ref, watch } from 'vue'

import { CMyFieldDb } from '@src/components/CMyFieldDb'
import { CTitleBanner } from '@src/components/CTitleBanner'
import { CProfile } from '@src/components/CProfile'
import { CDateTime } from '@src/components/CDateTime'
import { CMyPage } from '@src/components/CMyPage'
import { CCheckIfIsLogged } from '@src/components/CCheckIfIsLogged'
import { CMyFieldRec } from '@src/components/CMyFieldRec'
import { tools } from '@tools'
import { useUserStore } from '@store/UserStore'
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 type { IColGridTable, ICircuit} from 'model';
import { IImgGallery, IUserFields } from 'model'
import { shared_consts } from '@src/common/shared_vuejs'
import { colCitys, fieldsTable } from '@store/Modules/fieldsTable'
import { useRoute } from 'vue-router'
import { useNotifStore } from '@store/NotifStore'

export default defineComponent({
  name: 'CMyCardCircuitPopup',
  components: { CProfile, CTitleBanner, CMyFieldDb, CDateTime, CMyPage, CMyFieldRec, CCheckIfIsLogged },
  props: {
    table: {
      type: String,
      required: true,
    },
    prop_myrec: {
      type: Object as PropType<any>,
      required: false,
      default: null,
    },
    idRec: {
      type: Number,
      required: false,
      default: 0
    }
  },
  setup(props) {

    const userStore = useUserStore()
    const notifStore = useNotifStore()
    const globalStore = useGlobalStore()
    const $q = useQuasar()
    const $route = useRoute()
    const { t } = useI18n()

    const showPic = ref(false)

    const idnotif = computed(() => $route.query.idnotif ? $route.query.idnotif.toString() : '')

    const myrec = ref(<ICircuit | undefined>{})
    const col = ref(<IColGridTable>{})

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

    function load() {
      // Carica il profilo di quest'utente
      if (props.idRec) {
        userStore.loadGeneric(props.table, props.idRec, idnotif.value).then((ris) => {
          myrec.value = ris
          notifStore.setAsRead(idnotif.value)
        })

      } else {
        myrec.value = props.prop_myrec
      }

      col.value = fieldsTable.getArrColsByTable(props.table)
    }

    watch(() => props.idRec, (to: any, from: any) => {
      load()
    })

    function mounted() {
      load()
    }

    function condividipag() {
      const msg = self.location.host + tools.getPathByTableAndRec(props.table, myrec.value)
      return tools.copyStringToClipboard($q, msg, true)
    }

    onMounted(mounted)

    return {
      profile,
      tools,
      costanti,
      myrec,
      shared_consts,
      globalStore,
      showPic,
      userStore,
      t,
      fieldsTable,
      colCitys,
      toolsext,
      col,
      condividipag,
    }
  }
})
