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

import { CImgText } from '../../../components/CImgText/index'
import { CCard } from '@src/components/CCard'
import { CMyPage } from '@src/components/CMyPage'
import { CTitleBanner } from '@src/components/CTitleBanner'
import { CGridTableRec } from '@src/components/CGridTableRec'

import { useUserStore } from '@store/UserStore'

import { colTableIscrittiArcadei } from '@src/store/Modules/fieldsTable'

import MixinBase from '@src/mixins/mixin-base'
import { IParamsQuery, ISignupIscrizioneArcadeiOptions, ISignupIscrizioneConacreisOptions } from '@src/model'
import { shared_consts } from '@src/common/shared_vuejs'
import { tools } from '@tools'
import { useGlobalStore } from '@store/globalStore'
import { useQuasar } from 'quasar'
import { useI18n } from 'vue-i18n'

export default defineComponent({
  name: 'Iscrittiarcadei',
  components: { CImgText, CCard, CMyPage, CTitleBanner, CGridTableRec },
  setup(props) {
    const userStore = useUserStore()
    const globalStore = useGlobalStore()

    const $q = useQuasar()

    const { t } = useI18n()

    const arrfilterand: any = ref([])
    const myrec: any = ref([])
    const pagination = ref({
      sortBy: 'name',
      descending: false,
      page: 2,
      rowsPerPage: 5
      // rowsNumber: xx if getting data from a server
    })

    const myfilter = ref('')

    function mounted() {
      arrfilterand.value = [
        {
          label: 'Manca il pagamento',
          value: 0
        },
        {
          label: 'Da Tesserare',
          value: shared_consts.FILTER_TO_MAKE_MEMBERSHIP_CARD
        },
        {
          label: 'Tesserati',
          value: shared_consts.FILTER_MEMBERSHIP_CARD_OK
        },
      ]

    }

    function loadrec(): any {
      const sortBy = 'numshared'
      const descending = 1
      const myobj: any = {}
      if (descending)
        myobj[sortBy] = -1
      else
        myobj[sortBy] = 1

      const params: any = {
        table: 'iscrittiarcadei',
        startRow: 0,
        endRow: 10000,
        filter: '',
        filterand: myfilter.value,
        sortBy: myobj,
        descending,
        userId: userStore.my._id
      }

      console.log('myload', params)

      return globalStore.loadTable(params).then((data: any) => {
        return data.rows
      })
    }

    async function exportLista() {
      const myrecload = await loadrec()

      const sep = ';'

      let mystr = ''

      mystr += 'anno' + sep + 'numero_tessera' + sep + 'Conacreis' + sep + 'data_richiesta_iscrizione' + sep + 'data_approvazione_iscrizione' + sep
        + 'nome' + sep + 'cognome' + sep + 'codice_fiscale' + sep + 'partita_iva' + sep + 'nazione' + sep + 'indirizzo' + sep
        + 'localita' + sep + 'Prov' + sep + 'cap' + sep + 'nazione_nascita' + sep + 'data_nascita' + sep
        + 'luogo_nascita' + sep + 'provincia_nascita' + sep + 'email' + sep + 'telefono' + sep + 'quota_versata' + '\n'
      let index = 1
      for (const rec of myrecload) {
        mystr += rec.annoTesseramento + sep
        mystr += (rec.numTesseraInterna ? rec.numTesseraInterna : ' ') + sep
        mystr += (rec.codiceConacreis ? rec.codiceConacreis + sep : ' ') + sep
        mystr += tools.getstrDate(rec.dateofreg) + sep
        mystr += tools.getstrDate(rec.dateofapproved) + sep
        mystr += rec.name + sep
        mystr += rec.surname + sep
        mystr += rec.fiscalcode + sep
        mystr += ' ' + sep // partita_iva
        mystr += rec.residency_country + sep
        mystr += rec.residency_address + sep
        mystr += rec.residency_city + sep
        mystr += rec.residency_province + sep
        mystr += rec.residency_zipcode + sep
        mystr += rec.born_country + sep
        mystr += tools.getstrDate(rec.dateofbirth) + sep
        mystr += rec.born_city + sep
        mystr += rec.born_province + sep
        mystr += rec.email + sep
        mystr += rec.cell_phone + sep
        mystr += (rec.ha_pagato ? 'si' : 'no') + sep
        // mystr += 'si' + sep
        // mystr += 'si' + sep
        mystr += '\n'
        index++
      }

      tools.copyStringToClipboard($q, mystr, false)
    }

    function savefilter(filter: any) {
      console.log('filter', filter)
      myfilter.value = filter
    }

    onMounted(mounted)

    return {
      savefilter,
      exportLista,
      colTableIscrittiArcadei,
    }
  }
})
