import { CMyFriends } from '@src/components/CMyFriends'
import { CGridTableRec } from '@src/components/CGridTableRec'
import { tools } from '@tools'
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useRoute, useRouter } from 'vue-router'
import { useGlobalStore } from '@store/globalStore'
import { useI18n } from 'vue-i18n'
import { colmyUserPeople } from '@store/Modules/fieldsTable'
import type { ISearchList } from 'model'
import { costanti } from '@costanti'
import { shared_consts } from '@src/common/shared_vuejs'
import { toolsext } from '@store/Modules/toolsext'

export default defineComponent({
  name: 'myfriends',
  components: { CMyFriends, CGridTableRec },
  props: {},
  setup() {
    const userStore = useUserStore()
    const $route = useRoute()
    const { t } = useI18n()

    const arrfilterand: any = ref([])
    const filtercustom: any = ref([])
    const searchList = ref(<ISearchList[]>[])

    const filter = ref(costanti.FIND_PEOPLE)

    function mounted() {

      searchList.value = [
        {
          visible: true,
          label: 'Filtra per',
          table: shared_consts.TABFILTRI_UTENTE,
          key: '',
          type: costanti.FieldType.multiselect,
          value: 0,
          keycookie: '_filu',
          addall: false,
          arrvalue: tools.getCookie(tools.COOK_SEARCH + costanti.FILTER_SEP + 'users' + costanti.FILTER_SEP + shared_consts.TABFILTRI_UTENTE + '_filu', []),
          filter: null,
          useinput: false,
          icon: 'fas fa-filter'
        },
        {
          visible: true,
          label: 'Regione',
          table: 'regions',
          key: 'idReg',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + 'regions_fr', costanti.FILTER_TUTTI),
          keycookie: '_fr',
          addall: true,
          arrvalue: [],
          filter: null,
          useinput: false,
          icon: 'fas fa-globe-europe'
        },
        {
          visible: true,
          label: 'Provincia',
          table: 'provinces',
          key: 'profile.resid_province',
          type: costanti.FieldType.select,
          value: tools.getCookie(tools.COOK_SEARCH + 'provinces_fr', costanti.FILTER_TUTTI),
          keycookie: '_fr',
          addall: true,
          arrvalue: [],
          filter: getFilterProvinceByRegion,
          useinput: true,
          icon: 'flag',
          tablesel: 'provinces',
        },
      ]

      filtercustom.value = [
        {
          $or: [
            { deleted: { $exists: false } },
            { deleted: { $exists: true, $eq: false } }],
        }
      ]
      arrfilterand.value = []

      const filt_loaded = tools.getCookie(tools.COOK_SEARCH + tools.FRIENDS_SEARCH, costanti.FIND_PEOPLE, true)
      filter.value = filt_loaded ? filt_loaded : costanti.FIND_PEOPLE
    }
    watch(() => filter.value, (newval: any, oldval) => {
      tools.setCookie(tools.COOK_SEARCH + tools.FRIENDS_SEARCH, newval)

    })

    function extraparams() {

      return {
        lookup1: {
          lk_tab: 'provinces',
          lk_LF: 'profile.resid_province',
          lk_FF: 'prov',
          lk_as: 'mycities',
          lk_proj: {
            username: 1,
            name: 1,
            surname: 1,
            perm: 1,
            verified_by_aportador: 1,
            'profile.handshake': 1,
            'profile.img': 1,
            'profile.mygroups': 1,
            'profile.qualifica': 1,
            'profile.note': 1,
            'profile.da_contattare': 1,
            'profile.resid_province': 1,
            'mycities.reg': 1,
          }
        },

      }
    }

    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
      }
    }



    onMounted(mounted)

    return {
      filter,
      costanti,
      shared_consts,
      arrfilterand,
      filtercustom,
      searchList,
      colmyUserPeople,
      extraparams,
      getFilterProvinceByRegion,
    }
  }
})
