import type { PropType } from 'vue';
import { defineComponent, ref, computed, toRef, onMounted } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useRouter } from 'vue-router'
import { useGlobalStore } from '@store/globalStore'
import { useI18n } from 'vue-i18n'

import { CMyImgUser } from '@src/components/CMyImgUser'
import { CCurrencyValue } from '@src/components/CCurrencyValue'
import { tools } from '@tools'
import type { IMovQuery, IMovement } from '@src/model'

import { shared_consts } from '@src/common/shared_vuejs'

export default defineComponent({
  name: 'CSingleMovement',
  components: { CMyImgUser, CCurrencyValue },
  props: {
    mov: {
      type: Object as PropType<IMovement>,
      required: true,
    },
    index: {
      type: Number,
      required: true,
    },
    visu: {
      type: Number,
      required: false,
      default: 0,
    }
  },
  setup(props, { emit }) {

    const userStore = useUserStore()
    const $router = useRouter()
    const globalStore = useGlobalStore()
    const { t } = useI18n();

    function getFromToStr(mov: any) {

      let mystr = ''
      if (mov) {

        mystr += mov.str
      }
      return mystr
    }

    function navigabyMov(mov: IMovQuery, from: boolean) {
      let link = ''
      if (from) {
        if (mov.tipocontofrom === shared_consts.AccountType.USER) {
          link = `/my/` + mov.userfrom.username
        } else if (mov.tipocontofrom === shared_consts.AccountType.CONTO_DI_GRUPPO) {
          link = tools.getPathByGroup(mov.groupfrom)
        } else if (mov.tipocontofrom === shared_consts.AccountType.COMMUNITY_ACCOUNT) {
          link = '' // mov.contocomfrom.name
        }
      } else {
        if (mov.tipocontoto === shared_consts.AccountType.USER) {
          link = `/my/` + mov.userto.username
        } else if (mov.tipocontoto === shared_consts.AccountType.CONTO_DI_GRUPPO) {
          link = tools.getPathByGroup(mov.groupto)
        } else if (mov.tipocontoto === shared_consts.AccountType.COMMUNITY_ACCOUNT) {
          link = ''
        }
      }
      $router.push(link)
    }

    function getUsername(user: any) {
      if (user && user.username) {
        return user.username
      }

      return user
    }

    onMounted(() => {

    })

    return {
      userStore,
      tools,
      getFromToStr,
      t,
      navigabyMov,
      getUsername,
    }
  }
})
