import {
  computed,
  defineComponent, onMounted, ref, toRef, watch,
} from 'vue'

import type { IMyElem, IMyPage } from '@src/model'
import { useGlobalStore } from '@store/globalStore'
import { LandingFooter } from '@src/components/LandingFooter'
import { CMyElem } from '@src/components/CMyElem'
import { CMyEditElem } from '@src/components/CMyEditElem'

import { CImgTitle } from '../CImgTitle/index'
import { CTitle } from '../CTitle/index'
import { tools } from '@tools'
import { useQuasar } from 'quasar'
import { useI18n } from 'vue-i18n'
import { shared_consts } from '@src/common/shared_vuejs'
import objectId from '@src/js/objectId'

import { useRouter } from 'vue-router'

export default defineComponent({
  name: 'CMyPageElem2',
  components: { LandingFooter, CImgTitle, CTitle, CMyElem, CMyEditElem },
  props: {
    title: String,
    mypath: {
      type: String,
      required: true,
    },
    myidPage: {
      type: String,
      required: false,
      default: ''
    },
    img: {
      type: String,
      required: false,
      default: '',
    },
    imgbackground: {
      type: String,
      required: false,
      default: '',
    },
    sizes: {
      type: String,
      required: false,
      default: '',
    },
    styleadd: {
      type: String,
      required: false,
      default: '',
    },
    nofooter: {
      type: Boolean,
      required: false,
      default: false,
    },
  },

  setup(props) {
    const rec = ref<IMyPage | null>(null)
    const mypathin = toRef(props, 'mypath')

    const $q = useQuasar()

    const { t } = useI18n()
    const globalStore = useGlobalStore()
    const $router = useRouter()

    const visuEditor = ref(false)
    const addOn = ref(false)
    const myelemVoid = ref({ _id: objectId(), active: true, type: shared_consts.ELEMTYPE.TEXT, container: '...', path: mypathin.value } as IMyElem)


    const selElem = ref(globalStore.selElem)
    const editOn = computed(() => globalStore.editOn)

    const myelems = computed(() => {
      if (myidPage.value)
        return globalStore.getMyElemsByIdPage(myidPage.value)
      else if (mypathin.value)
        return globalStore.getMyElems(mypathin.value)
      else
        return null
    })

    function load() {
      // console.log('load', mypathin.value)

      if (mypathin.value !== '') {
        globalStore.loadPage('/' + mypathin.value, 'cmypageelem').then(ris => {
          rec.value = ris
        })
      }

    }

    watch(() => props.mypath, (to: string, from: string) => {
      console.log('... load', mypathin.value, props.mypath)
      selElem.value = {}
      load()
    })

    watch(
      () => editOn.value,
      () => {
        if (!editOn.value) {
          selElem.value = {}
        }
      })

    function selElemClick(myelem: IMyElem) {
      // console.log('mypageelem selElemClick', myelem)
      selElem.value = {}
      selElem.value = myelem
      visuEditor.value = !!myelem
    }

    function mounted() {
      load()
    }

    function saveElem(myelem: IMyElem) {
      //
    }

    function changeVisuDrawer(path: string, edit: boolean) {
      globalStore.changeVisuDrawer(path, edit)
    }

    onMounted(mounted)

    return {
      rec, myelems,
      mypathin,
      editOn,
      visuEditor,
      addOn,
      tools,
      shared_consts,
      myelemVoid,
      selElemClick,
      selElem,
      saveElem,
      changeVisuDrawer,
    }
  },

})
