import type { PropType} from 'vue';
import { computed, defineComponent, onMounted, ref, toRef, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
import { CMySelect } from '@src/components/CMySelect'

import { costanti } from '@costanti'
import { fieldsTable } from '@store/Modules/fieldsTable'
import { shared_consts } from '@src/common/shared_vuejs'
import { tools } from '@tools'
import MixinBase from '@src/mixins/mixin-base'
import type { ISpecialField } from '@src/model'

export default defineComponent({
  name: 'CMySelectCity',
  emits: ['update:modelValue'],
  props: {
    modelValue: [String, Number, Object],
    label: {
      type: String,
      required: false,
      default: undefined,
    },
    table: {
      type: String,
      required: false,
      default: ''
    },
    myclass: {
      type: String,
      required: false,
      default: ''
    },
    jointable: {
      type: String,
      required: false,
      default: ''
    },
    db_field: {
      type: String,
      required: false,
      default: '',
    },
    db_type: {
      type: Number,
      required: false,
      default: 0,
    },
    db_subfield: {
      type: String,
      required: false,
      default: '',
    },
    db_subsubfield: {
      type: String,
      required: false,
      default: '',
    },
    db_specialField: {
      type: Object as PropType<ISpecialField>,
      required: false,
      default: null,
    },
    db_id: {
      type: [String, Number],
      required: false,
      default: '',
    },
    db_rec: {
      type: Object,
      required: false,
      default: null,
    },
    value2: {
      type: String,
      required: false,
      default: undefined,
    },
  },
  components: { CMySelect },
  setup(props, { emit }) {
    const $q = useQuasar()
    const { t } = useI18n()
    const userStore = useUserStore()
    const globalStore = useGlobalStore()

    const optFiltered = ref(<any>[])
    const valori = ref(<any>[])

    const { setValDb, getValDb } = MixinBase()

    const myvalue = ref(<string | number | object | undefined>'')

    watch(() => myvalue.value, (value: any, oldval: any) => {
      update()
    },
    )

    function mounted() {
      myvalue.value = props.modelValue
      update()
    }

    function update() {
      //
      emit('update:modelValue', myvalue.value)
    }

    function changevalRec(newval: any) {
      if (props.db_type > 0)
        setValDb($q, props.db_field, newval, props.db_type, false, props.table, props.db_subfield, props.db_id, 0, props.db_subsubfield, props.db_specialField)

    }

    onMounted(mounted)

    return {
      tools,
      costanti,
      globalStore,
      shared_consts,
      fieldsTable,
      myvalue,
      changevalRec,
    }
  }
})

