import {
  computed,
  provide, defineComponent, onBeforeMount, onBeforeUnmount, onMounted, ref, toRef, toRefs, watch,
} from 'vue'

import { tools } from '@tools'
import { CMyFieldDb } from '@src/components/CMyFieldDb'
import { costanti } from '@costanti'
import { useGlobalStore } from '@store/globalStore'
import { useUserStore } from '@store/UserStore'

import { CTitlePage } from '@src/components/CTitlePage'
import { CGridTableRec } from '@src/components/CGridTableRec'
import type { IColGridTable, ISearchList} from 'model';
import { IMyBacheca, IMySkill, ISkill } from 'model'
import { shared_consts } from '@src/common/shared_vuejs'
import { useI18n } from 'vue-i18n'
import { toolsext } from '@store/Modules/toolsext'
import { fieldsTable } from '@store/Modules/fieldsTable'
import { useQuasar } from 'quasar'

export default defineComponent({
  name: 'CFinder',
  emits: ['clickButtBar'],
  props: {
    table: {
      type: String,
      required: true,
    },
    showFilterPersonal: {
      type: Boolean,
      required: false,
      default: false,
    },
    showBarSelection: {
      type: Boolean,
      required: false,
      default: true,
    },
    showMap: {
      type: Boolean,
      required: false,
      default: false,
    },
    noButtAdd: {
      type: Boolean,
      required: false,
      default: false,
    },
    labelBtnAddExtra: {
      type: String,
      required: false,
      default: '',
    },
    filtrocircuito: {
      type: String,
      required: false,
      default: '',
    },
    selector: {
      type: Object,
      required: false,
      default: null,
    },
    ind: {
      type: Number,
      required: false,
      default: -1,
    }
  },
  components: {
    CMyFieldDb, CGridTableRec, CTitlePage,
  },
  setup(props, { attrs, slots, emit }) {
    const { t } = useI18n()
    const $q = useQuasar()
    const globalStore = useGlobalStore()
    const userStore = useUserStore()

    const arrfilterand: any = ref([])
    const filtercustom: any = ref([])
    const searchList_Servizi = ref(<ISearchList[]>[])
    const searchList_Beni = ref(<ISearchList[]>[])
    const searchList_MyGroups = ref(<ISearchList[]>[])
    const searchList_MyCircuits = ref(<ISearchList[]>[])
    const searchList_Events = ref(<ISearchList[]>[])
    const searchList_Hosp = ref(<ISearchList[]>[])
    const searchList_Attivita = ref(<ISearchList[]>[])

    const search = ref('')
    const myrecfiltertoggle = ref(tools.FILTER_ALL)

    const prop_colkey = ref('idSkill')
    const col_title = ref('descr')
    const col_footer = ref('idCity')
    const col_tabfooter = ref('mycities')

    const strextra = ref('')
    const myoptions = ref(<any>[])

    const col = ref(<IColGridTable>{})

    const myGridRef = ref(<any>null)

    /*
    const idSectorServizi = computed(() => {
      let myval: any = null
      myval = searchList_Servizi.value.find((rec) => (rec.table === toolsext.TABSECTORS))
      if (myval) {
        const ris = myval.value || 0
        // console.log('idSectorServizi=', ris)
        return ris
      } else {
        return 0
      }
    })

     */


    function updatefilter(value: any) {
      if (value === tools.FILTER_MYREC) {
        if (props.table === shared_consts.TABLES_MYGROUPS) {
          filtercustom.value = [{
            'admins': {
              $elemMatch: { username: { $eq: userStore.my.username } }
            },
          }]
        } else if (props.table === shared_consts.TABLES_CIRCUITS) {
          filtercustom.value = [{
            'admins': {
              $elemMatch: { username: { $eq: userStore.my.username } }
            },
          }]

        } else {
          filtercustom.value = [{ userId: userStore.my._id }]
        }

      } else if (value === tools.FILTER_MYFOLLOW) {
        if (props.table === shared_consts.TABLES_MYGROUPS) {

        }
      } else {
        filtercustom.value = []
      }
    }


    /*watch(() => filtercustom.value, (value: any, oldval: any) => {
      updatefilter(myrecfiltertoggle.value)
    })

     */

    watch(() => myrecfiltertoggle.value, (value: any, oldval: any) => {
      updatefilter(value)
    },
    )

    const mypagination = computed(() => {
      if (props.table === shared_consts.TABLES_MYBACHECAS)
        return { sortBy: 'dateTimeStart', descending: false, page: 1, rowsNumber: 0, rowsPerPage: 10 }
      else if (props.table === shared_consts.TABLES_MYGROUPS)
        return { sortBy: 'date_created', descending: true, page: 1, rowsNumber: 0, rowsPerPage: 10 }
      else if (props.table === toolsext.TABMOVEMENTS)
        return { sortBy: 'transactionDate', descending: true, page: 1, rowsNumber: 0, rowsPerPage: 20 }
      else if (props.table === shared_consts.TABLES_CIRCUITS)
        return { sortBy: 'numMembers', descending: true, page: 1, rowsNumber: 0, rowsPerPage: 20 }
      else if (props.table === toolsext.TABUSER)
        return userStore.getMypaginationMembers()
      else if (shared_consts.TABLES_ORDER_DATE_UPDATED.includes(props.table))
        return { sortBy: 'date_updated', descending: true, page: 1, rowsNumber: 0, rowsPerPage: 10 } // date_updated
      else if (shared_consts.TABLES_ORDER_DESCR.includes(props.table))
        return { sortBy: 'desc', descending: false, page: 1, rowsNumber: 0, rowsPerPage: 10 }

      return { sortBy: 'date_created', descending: true, page: 1, rowsNumber: 0, rowsPerPage: 10 }
    })

    const searchList = computed(() => {
      if (props.table === shared_consts.TABLES_MYGOODS)
        return searchList_Beni.value
      else if (props.table === shared_consts.TABLES_MYSKILLS)
        return searchList_Servizi.value
      else if (props.table === shared_consts.TABLES_MYGROUPS)
        return searchList_MyGroups.value
      else if (props.table === shared_consts.TABLES_CIRCUITS)
        return searchList_MyCircuits.value
      else if (props.table === shared_consts.TABLES_MYBACHECAS)
        return searchList_Events.value
      else if (props.table === shared_consts.TABLES_MYHOSPS)
        return searchList_Hosp.value
      else if (props.table === shared_consts.TABLES_ATTIVITAS)
        return searchList_Attivita.value

      return searchList_Servizi.value
    })

    const showType = computed(() => {
      return costanti.SHOW_MYCARD
    })

    const hint = computed(() => {
      if (props.table === shared_consts.TABLES_MYGOODS)
        return 'digita il nome del Bene o del settore'
      else if (props.table === shared_consts.TABLES_MYSKILLS)
        return 'digita il nome del Servizio o del settore'
      else if (props.table === shared_consts.TABLES_MYGROUPS)
        return 'digita il nome del Gruppo'
      else if (props.table === shared_consts.TABLES_CIRCUITS)
        return 'digita il nome della provincia'
      else if (props.table === shared_consts.TABLES_MYBACHECAS)
        return 'digita il nome dell\'Evento'
      else if (props.table === shared_consts.TABLES_MYHOSPS)
        return 'digita delle parole da cercare nella descrizione dell\'Ospitalità'
      else if (props.table === shared_consts.TABLES_ATTIVITAS)
        return 'digita un\'attività da cercare'

      return 'digita una parola da cercare'
    })

    const visuType = computed(() => {
      if (props.table === shared_consts.TABLES_MYGOODS)
        return false
      else if (props.table === shared_consts.TABLES_MYSKILLS)
        return false
      else if (props.table === shared_consts.TABLES_MYGROUPS)
        return false
      else if (props.table === shared_consts.TABLES_MYBACHECAS)
        return false

      return false // $q.screen.gt.xs
    })

    const noMsgRecord = computed(() => {
      if (props.table === shared_consts.TABLES_MYGOODS)
        return 'Nessun Bene trovato con i filtri selezionati'
      else if (props.table === shared_consts.TABLES_MYSKILLS)
        return 'Nessun Servizio trovato con i filtri selezionati'
      else if (props.table === shared_consts.TABLES_MYGROUPS)
        return 'Nessun Gruppo trovato con i filtri selezionati'
      else if (props.table === shared_consts.TABLES_CIRCUITS)
        return 'Nessun Circuito trovato con i filtri selezionati'
      else if (props.table === shared_consts.TABLES_MYBACHECAS)
        return 'Nessun Evento trovato con i filtri selezionati'
      else if (props.table === shared_consts.TABLES_MYHOSPS)
        return 'Nessuna Ospitalità trovata con i filtri selezionati'
      else if (props.table === shared_consts.TABLES_ATTIVITAS)
        return 'Nessuna Attività trovata con i filtri selezionati'

      return 'Nessun dato trovato con i filtri selezionati'
    })


    function mounted() {

      let obj = tools.getParamsByTable(props.table)

      if (props.ind >= 0) {
        strextra.value = costanti.MAINCARDS[props.ind].strsingolo!
      }

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

      prop_colkey.value = obj.prop_colkey
      col_title.value = obj.col_title
      col_footer.value = obj.col_footer
      col_tabfooter.value = obj.col_tabfooter

      if (props.table === shared_consts.TABLES_MYGROUPS) {
        myoptions.value = [
          { label: '🌎 ' + t('groups.tutti'), value: tools.FILTER_ALL },
          { label: '👤 ' + t('groups.che_gestisci'), value: tools.FILTER_MYREC },
          // {label: 'Seguo', value: tools.FILTER_MYFOLLOW},
        ]

      } else if (props.table === shared_consts.TABLES_CIRCUITS) {
        myoptions.value = []
        if (tools.gestiscoAlmeno1Circuito()) {
          myoptions.value = [
            { label: '🌎 ' + t('groups.tutti'), value: tools.FILTER_ALL },
            { label: '👤 Circuiti che Gestisci', value: tools.FILTER_MYREC },
            // {label: 'Seguo', value: tools.FILTER_MYFOLLOW},
          ]

        }

      } else {
        /*
        myoptions.value = [
          { label: '🌎 Tutti', value: tools.FILTER_ALL },
          { label: '👤 Personali', value: tools.FILTER_MYREC },
          // {label: 'Seguo', value: tools.FILTER_MYFOLLOW},
        ]
        */
      }

      function getFilterSkills(recSkill: any, index: number, arr: any) {
        const recsectors: any = searchList.value.find((rec) => rec.table === toolsext.TABSECTORS)
        // console.log('getFilterSkills', recSkill.idSector, recsectors.value)
        if (recsectors && recSkill.idSector) {
          return recSkill.idSector.includes(recsectors.value)
        } else {
          return true
        }
      }

      function getFilterGoods(recGood: any, index: number, arr: any) {
        const recsectorGoods: any = searchList.value.find((rec) => rec.table === 'sectorgoods')
        // console.log('getFilterSkills', recSkill.idSector, recsectors.value)
        if (recsectorGoods && recGood.idSectorGood) {
          return recGood.idSectorGood.includes(recsectorGoods.value)
        } else {
          return true
        }
      }

      function getFilterSubSkills(recSubSkill: any, index: number, arr: any) {
        const recskills: any = searchList.value.find((rec) => rec.table === 'skills')
        // console.log('recSubSkill', recSubSkill, 'recskills', recskills)
        if (recskills) {
          return recSubSkill.idSkill === recskills.value
        } else {
          return true
        }
      }

      /*function getFilterCitiesByProvince(recCities: any, index: number, arr: any) {
        const recprov: any = searchList.value.find((rec) => rec.table === 'provinces')
        // console.log('recSubSkill', recSubSkill, 'recskills', recskills)
        if (recprov) {
          return recCities.idProvince === recprov.value
        } else {
          return true
        }
      }

       */

      function getFilterProvinceByRegion(recProvince: any, index: number, arr: any) {
        const recreg: any = searchList.value.find((rec) => rec.table === 'regions')
        if (recreg) {
          return recProvince.reg === recreg.value
        } else {
          return true
        }
      }


      searchList_Servizi.value = [
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
          tablesel: 'provinces',
        },
        {
          visible: props.filtrocircuito ? false : true,
          label: 'Circuito',
          table: 'circuits',
          key: 'profile.mycircuits.circuitname',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? props.filtrocircuito : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'circuits', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          // filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
          filteradv: true,
        },
        /*{
          label: 'Visibilità',
          table: 'pub_to_share',
          key: 'pub_to_share',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + 'pub_to_share', shared_consts.PUBTOSHARE.ALL, true),
          addall: false,
          arrvalue: [],
          useinput: false,
          filter: null,
        },*/
        {
          visible: true,
          label: 'Settore',
          table: toolsext.TABSECTORS,
          key: 'idSector',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + toolsext.TABSECTORS, 0, true),
          arrvalue: [],
          type: costanti.FieldType.select,
          filter: null,
          addall: true,
          notinsearch: false,
          useinput: false,
        },
        /*{
          label: 'Categoria',
          table: 'skills',
          key: 'idSkill',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + tools.getCookie(tools.COOK_SEARCH + toolsext.TABSECTORS, costanti.FILTER_TUTTI), costanti.FILTER_TUTTI, true),
          arrvalue: [],
          type: costanti.FieldType.select,
          addall: true,
          filter: getFilterSkills,
          showcount: true,
          useinput: false,
        },*/
        {
          visible: true,
          label: 'Offro/Cerco',
          table: 'adtypes',
          key: 'adType',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'adtypes', costanti.FILTER_TUTTI, true),
          arrvalue: [],
          addall: true,
          type: costanti.FieldType.select,
          filter: null,
          useinput: false,
          filteradv: true,
        },
        /*{
          label: 'Specializzazione',
          table: 'subskills',
          key: 'idSubSkill',
          value: tools.getCookie(tools.COOK_SEARCH + 'subskills' + '_' + tools.getCookie(tools.COOK_SEARCH + 'skills', costanti.FILTER_TUTTI), costanti.FILTER_TUTTI),
          type: costanti.FieldType.select,
          arrvalue: [],
          addall: true,
          filter: getFilterSubSkills,
          showcount: true,
          useinput: false,
          icon: 'far fa-id-card',
        },
        */



        /*
        {
          label: 'Livello',
          table: 'levels',
          key: 'numLevel',
          value: tools.getCookie(tools.COOK_SEARCH + 'levels', costanti.FILTER_TUTTI),
          arrvalue: [],
          addall: true,
          type: costanti.FieldType.select,
          filter: null,
          useinput: false,
          filteradv: true,
        },


         */
        {
          visible: true,
          label: 'Disponibile',
          table: 'statusSkills',
          key: 'idStatusSkill',
          value: 0,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'statusSkills', []),
          type: costanti.FieldType.multiselect,
          filter: null,
          useinput: false,
          icon: 'fas fa-exchange-alt',
          filteradv: true,
        },
        {
          visible: true,
          label: 'In cambio di',
          table: 'contribtypes',
          key: 'idContribType',
          value: 0,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + 'contribtypes', []),
          type: costanti.FieldType.multiselect,
          filter: null,
          useinput: false,
          icon: 'fas fa-hand-holding',
          filteradv: true,
          //icon: 'swap_horizontal_circle',
        },
        {
          visible: true,
          label: '',
          table: '',
          key: '',
          value: 0,
          type: costanti.FieldType.separator,
          arrvalue: [],
          addall: true,
          filter: null,
          showcount: true,
          useinput: false,
          notinsearch: true,
          icon: '',
        },

      ]

      searchList_Events.value = [
        /*{
          label: 'Scambio',
          table: 'statusSkills',
          key: 'idStatusSkill',
          value: 0,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + 'statusSkills', []),
          type: costanti.FieldType.multiselect,
          filter: null,
          useinput: false,
          icon: 'mood',
          filteradv: false,
        },*/
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
        },
        {
          visible: props.filtrocircuito ? false : true,
          label: 'Circuito',
          table: 'circuits',
          key: 'profile.mycircuits.circuitname',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? props.filtrocircuito : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'circuits', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          // filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
          filteradv: true,
        },
        /*{
          label: 'Visiblità',
          table: 'pub_to_share',
          key: 'pub_to_share',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + 'pub_to_share', shared_consts.PUBTOSHARE.ALL, true),
          addall: false,
          arrvalue: [],
          useinput: true,
          filter: null,
          tablesel: 'pub_to_share',
        },*/
        {
          visible: true,
          label: 'Settore',
          table: toolsext.TABSECTORS,
          key: 'idSector',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + toolsext.TABSECTORS, 0, true),
          arrvalue: [],
          type: costanti.FieldType.select,
          filter: null,
          addall: true,
          notinsearch: false,
          useinput: false,
        },
        {
          visible: true,
          label: 'Data Inizio',
          table: 'caldate',
          key: 'dateTimeStart',
          value: 2,
          arrvalue: [],
          type: costanti.FieldType.select,
          addall: true,
          filter: null,
          useinput: false,
          filteradv: true,
        },
        /*{
          label: 'Categoria',
          table: 'skills',
          key: 'idSkill',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + tools.getCookie(tools.COOK_SEARCH + toolsext.TABSECTORS, costanti.FILTER_TUTTI), costanti.FILTER_TUTTI, true),
          arrvalue: [],
          type: costanti.FieldType.select,
          addall: true,
          filter: getFilterSkills,
          showcount: true,
          useinput: false,
        },*/
        {
          visible: true,
          label: 'In cambio di',
          table: 'contribtypes',
          key: 'idContribType',
          value: 0,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + 'contribtypes', []),
          type: costanti.FieldType.multiselect,
          filter: null,
          useinput: false,
          icon: 'fas fa-hand-holding',
          filteradv: true,
          //icon: 'swap_horizontal_circle',
        },
        {
          visible: true,
          label: '',
          table: '',
          key: '',
          value: 0,
          type: costanti.FieldType.separator,
          arrvalue: [],
          addall: true,
          filter: null,
          showcount: true,
          useinput: false,
          notinsearch: false,
          icon: '',
        },

      ]

      searchList_Hosp.value = [
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          keycookie: '_hosp',
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          keycookie: '_hosp',
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
        },
        {
          visible: props.filtrocircuito ? false : true,
          label: 'Circuito',
          table: 'circuits',
          key: 'profile.mycircuits.circuitname',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? props.filtrocircuito : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'circuits', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          keycookie: '_hosp',
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          // filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
          filteradv: true,
          visible: true,
        },
        {
          label: 'Offro/Cerco',
          table: 'adtypes',
          key: 'adType',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'adtypes', costanti.FILTER_TUTTI, true),
          arrvalue: [],
          addall: true,
          type: costanti.FieldType.select,
          filter: null,
          useinput: false,
          filteradv: true,
          visible: true,
        },
        {
          label: 'Tipologia',
          table: toolsext.TABTYPEHOSP,
          key: 'typeHosp',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + toolsext.TABTYPEHOSP, costanti.FILTER_TUTTI, true),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'flag',
          filteradv: true,
          visible: true,
        },
        {
          label: 'N° max Ospiti',
          table: toolsext.TABPEOPLE,
          key: 'numMaxPeopleHosp',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + toolsext.TABPEOPLE, costanti.FILTER_TUTTI, true),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-users',
          filteradv: true,
          visible: true,
        },
        {
          label: 'Preferenze',
          table: toolsext.TABPREF,
          key: 'preferences',
          type: costanti.FieldType.multiselect,
          value: 0,
          addall: false,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + toolsext.TABPREF, []),
          filter: null,
          useinput: false,
          icon: 'fas fa-asterisk',
          filteradv: true,
          visible: true,
        },
        {
          label: 'In cambio di',
          table: 'contribtypes',
          key: 'idContribType',
          value: 0,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'contribtypes', []),
          type: costanti.FieldType.multiselect,
          filter: null,
          useinput: false,
          icon: 'fas fa-hand-holding',
          filteradv: true,
          visible: true,
          //icon: 'swap_horizontal_circle',
        },
        {
          label: '',
          table: '',
          key: '',
          value: 0,
          type: costanti.FieldType.separator,
          arrvalue: [],
          addall: true,
          filter: null,
          showcount: true,
          useinput: false,
          notinsearch: true,
          icon: '',
          visible: true,
        },

      ]

      searchList_Beni.value = [
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe',
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
        },
        {
          visible: props.filtrocircuito ? false : true,
          label: 'Circuito',
          table: 'circuits',
          key: 'profile.mycircuits.circuitname',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? props.filtrocircuito : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'circuits', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe',
        },
        {
          visible: true,
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          value: props.filtrocircuito ? costanti.FILTER_TUTTI : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          filteradv: true,
          //filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
        },
        /*{
          label: 'Visiblità',
          table: 'pub_to_share',
          key: 'pub_to_share',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + 'pub_to_share', shared_consts.PUBTOSHARE.ALL, true),
          addall: false,
          arrvalue: [],
          useinput: true,
          filter: null,
          tablesel: 'pub_to_share',
        },*/
        {
          visible: true,
          label: 'Settore',
          table: 'sectorgoods',
          key: 'idSectorGood',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + toolsext.TABSECTORGOODS, 0, true),
          arrvalue: [],
          type: costanti.FieldType.select,
          filter: null,
          addall: true,
          notinsearch: false,
          useinput: false,
        },
        {
          visible: true,
          label: 'Categoria',
          table: 'goods',
          key: 'idGood',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + tools.getCookie(tools.COOK_SEARCH + toolsext.TABSECTORGOODS, costanti.FILTER_TUTTI), costanti.FILTER_TUTTI, true),
          arrvalue: [],
          type: costanti.FieldType.select,
          addall: true,
          filter: getFilterGoods,
          showcount: true,
          useinput: false,
        },
        {
          visible: true,
          label: 'Offro/Cerco',
          table: 'adtypes',
          key: 'adType',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'adtypes', costanti.FILTER_TUTTI, true),
          arrvalue: [],
          addall: true,
          type: costanti.FieldType.select,
          filter: null,
          useinput: false,
          filteradv: true,
        },
        /*
        {
          label: 'Consegna',
          table: 'shippings',
          key: 'idShipping',
          value: tools.getCookie(tools.COOK_SEARCH + 'shippings', costanti.FILTER_TUTTI),
          arrvalue: [],
          type: costanti.FieldType.select,
          addall: true,
          filter: null,
          useinput: false,
        },*/
        /*{
          label: 'Specializzazione',
          table: 'subskills',
          key: 'idSubSkill',
          value: tools.getCookie(tools.COOK_SEARCH + 'subskills' + '_' + tools.getCookie(tools.COOK_SEARCH + 'skills', costanti.FILTER_TUTTI), costanti.FILTER_TUTTI),
          type: costanti.FieldType.select,
          arrvalue: [],
          addall: true,
          filter: getFilterSubSkills,
          showcount: true,
          useinput: false,
          icon: 'far fa-id-card',
        },
        */



        /*
        {
          label: 'Livello',
          table: 'levels',
          key: 'numLevel',
          value: tools.getCookie(tools.COOK_SEARCH + 'levels', costanti.FILTER_TUTTI),
          arrvalue: [],
          addall: true,
          type: costanti.FieldType.select,
          filter: null,
          useinput: false,
          filteradv: true,
        },


         */
        {
          visible: true,
          label: 'In cambio di',
          table: 'contribtypes',
          key: 'idContribType',
          value: 0,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'contribtypes', []),
          type: costanti.FieldType.multiselect,
          filter: null,
          useinput: false,
          icon: 'currency_exchange',
          filteradv: true,
          //icon: 'swap_horizontal_circle',
        },
        {
          visible: true,
          label: 'Altri Filtri',
          table: 'otherfilters',
          key: 'otherfilters',
          arrvalue: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'otherfilters', []),
          value: 0,
          type: costanti.FieldType.multiselect,
          addall: false,
          filter: null,
          useinput: false,
          icon: 'fas fa-filter',
          filteradv: true,
        },
        {
          visible: true,
          label: '',
          table: '',
          key: '',
          value: 0,
          type: costanti.FieldType.separator,
          arrvalue: [],
          addall: true,
          filter: null,
          showcount: true,
          useinput: false,
          notinsearch: true,
          icon: '',
        },

      ]

      searchList_MyCircuits.value = [
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
        },
        {
          visible: true,
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          // filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
        },
      ]

      searchList_Attivita.value = [
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
        },
        {
          visible: true,
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          // filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
        },
        {
          visible: true,
          label: '',
          table: '',
          key: '',
          value: 0,
          type: costanti.FieldType.separator,
          arrvalue: [],
          addall: true,
          filter: null,
          showcount: true,
          useinput: false,
          notinsearch: true,
          icon: '',
        },
      ]

      searchList_MyGroups.value = [
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'regions', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'idProvince',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'provinces', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
        },
        {
          visible: true,
          label: 'Comune',
          table: 'cities',
          key: 'idCity',
          type: costanti.FieldType.select_by_server,
          arrvalue: [],
          addall: true,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'cities', costanti.FILTER_TUTTI),
          useinput: true,
          filter: null,
          // filter: getFilterCitiesByProvince,
          // param1: shared_consts.PARAM_SHOW_PROVINCE,
          tablesel: 'cities',
          icon: 'fas fa-map-marker-alt',
          filteradv: true,
        },
        {
          visible: props.filtrocircuito ? false : true,
          label: 'Circuito',
          table: 'circuits',
          key: 'profile.mycircuits.circuitname',
          type: costanti.FieldType.select,
          value: props.filtrocircuito ? props.filtrocircuito : tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'circuits', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe',
          filteradv: true,
        },
        {
          visible: true,
          label: 'Categorie',
          table: 'catgrps',
          key: 'idCatGrp',
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'catgrps', costanti.FILTER_TUTTI),
          arrvalue: [],
          type: costanti.FieldType.select,
          filter: null,
          addall: true,
          useinput: false,
          icon: 'engineering',
          filteradv: true,
        },
        {
          visible: true,
          label: 'Visiblità',
          table: 'visibilGroup',
          key: 'visibility',
          type: costanti.FieldType.multiselect,
          value: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + props.table + costanti.FILTER_SEP + 'visibility', costanti.FILTER_TUTTI),
          addall: true,
          arrvalue: [],
          useinput: true,
          filter: null,
          tablesel: 'visibility',
          filteradv: true,
        },
        {
          visible: true,
          label: '',
          table: '',
          key: '',
          value: 0,
          type: costanti.FieldType.separator,
          arrvalue: [],
          addall: true,
          filter: null,
          showcount: true,
          useinput: false,
          notinsearch: true,
          icon: '',
        },
      ]

      filtercustom.value = []
    }



    function mySortFieldsAvailable() {
      if (props.table === toolsext.TABUSER) {
        return userStore.getSortFieldsAvailable()
      }

      return []
    }



    function doSearch() {
      //
    }

    function clickButtBar(item: any) {
      if (myGridRef.value) {
        myGridRef.value.clickButtBar(item)
      }

      // emit('clickButtBar', item)
    }

    onMounted(mounted)

    return {
      t,
      tools,
      costanti,
      arrfilterand,
      filtercustom,
      searchList,
      search,
      doSearch,
      myrecfiltertoggle,
      prop_colkey,
      col_title,
      col_footer,
      col_tabfooter,
      col,
      toolsext,
      mypagination,
      noMsgRecord,
      showType,
      visuType,
      hint,
      strextra,
      myoptions,
      mySortFieldsAvailable,
      clickButtBar,
      myGridRef,
    }
  },
})
