import type { PropType } from 'vue';
import { defineComponent, onMounted, ref, watch } from 'vue'
import { useQuasar } from 'quasar'
import { useI18n } from 'vue-i18n'
import { useGlobalStore } from '@store/globalStore'
import { fieldsTable } from '@store/Modules/fieldsTable'
import { tools } from '@tools'
import { costanti } from '@costanti'
import { CMyFieldDb } from '@src/components/CMyFieldDb'
import { CMyFieldRec } from '@src/components/CMyFieldRec'
import type { IUserFields } from 'model';
import { IColGridTable, IOperators, ISpecialField } from 'model'
import MixinBase from '@src/mixins/mixin-base'
import { useCalendarStore } from '@src/store/CalendarStore'


export default defineComponent({
  name: 'CUserNote',
  emits: ['closenote', 'save'],
  props: {
    username: String,
    userprofile_param: {
      type: Object as PropType<IUserFields>,
      required: true,
    }
  },
  components: { CMyFieldDb, CMyFieldRec },
  setup(props, { emit }) {
    const $q = useQuasar()
    const { t } = useI18n()
    const globalStore = useGlobalStore()

    const shownote = ref(false)
    const userprofile = ref(<IUserFields>{})

    function mounted() {
      shownote.value = true
      userprofile.value = props.userprofile_param
    }

    function UpdateDbNote() {
      const globalStore = useGlobalStore()
      const { t } = useI18n()

      const mydatatosave = {
        id: userprofile.value._id,
        table: 'users',
        fieldsvalue: { 'profile.note': userprofile.value.profile.note }
      }

      globalStore.saveFieldValue(mydatatosave).then((esito) => {
        if (esito) {
          emit('save', userprofile.value)
          tools.showPositiveNotif($q, t('db.recupdated'))
          shownote.value = false
        } else {
          tools.showNegativeNotif($q, t('db.recfailed'))
        }
      })
    }



    onMounted(mounted)

    return {
      tools,
      costanti,
      fieldsTable,
      globalStore,
      shownote,
      userprofile,
      UpdateDbNote,
      t,
    }
  },
})

