<template>
  <div class="w-full box-border pb-10 px-1">
    <!-- style="height: calc(100vh - 105px);" -->
    <!-- 发布中 -->
    <ProfileNotesEmpty v-if="!noteList.length" />
    <div v-else-if="noteList.length">
      <van-list
        v-model:loading="loading"
        v-model:error="error"
        :finished="finished"
        :immediate-check="false"
        offset="80"
        error-text="请求失败，点击重新加载"
        finished-text=""
        @load="getloadList"
      >
        <ProfileNotesPublishedItem
          v-for="item in noteList"
          :key="item.id"
          :data="item"
          @on-note-down="handleNoteDown(item)"
          @on-share="changeShowShare(item)"
        />
      </van-list>
    </div>

    <van-share-sheet v-model:show="showShare" :options="options" @select="onSelectShare" />
  </div>
</template>

<script setup>
// import wx from 'weixin-js-sdk'
const showShare = ref(false)

const options = [
  // { name: '微信', icon: 'wechat' },
  // { name: '微博', icon: 'weibo' },
  { name: '复制链接', icon: 'link' }
  // { name: '分享海报', icon: 'poster' },
  // { name: '二维码', icon: 'qrcode' }
]

const yjId = ref(null)

const changeShowShare = (parmas) => {
  showShare.value = true
  yjId.value = parmas.id
}
// 分享
const onSelectShare = (option) => {
  if (option.name === '微信') {
    shareToWeChat('friend')
  } else if (option.name === '朋友圈') {
    shareToWeChat('timeline')
  } else if (option.name == '复制链接') {
    copyLink()
  }
  showShare.value = false
}

// 微信分享到好友
const shareToWeChat = (type) => {
  // const shareUrl = 'https://your-website.com/share-page' // 分享链接
  // const title = '分享标题' // 分享标题
  // const desc = '分享描述' // 分享描述
  // const imgUrl = 'https://your-website.com/share-image.jpg' // 分享图片
  // getWeChatConfig().then((configData) => {
  //   wx.config({
  //     debug: false, // 设置为 true 以便调试
  //     appId: configData.appId,
  //     timestamp: configData.timestamp,
  //     nonceStr: configData.nonceStr,
  //     signature: configData.signature,
  //     jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'] // 配置微信 JS-SDK 的接口
  //   })
  //   // 通过微信 JS-SDK 实现分享功能（已配置过微信 JS-SDK）
  //   if (typeof wx !== 'undefined') {
  //     wx.ready(function () {
  //       const shareData = {
  //         title: title,
  //         desc: desc,
  //         link: shareUrl,
  //         imgUrl: imgUrl
  //       }
  //       if (type === 'friend') {
  //         wx.updateAppMessageShareData(shareData) // 分享给朋友
  //       } else if (type === 'timeline') {
  //         wx.updateTimelineShareData(shareData) // 分享到朋友圈
  //       }
  //     })
  //   }
  //   wx.error((error) => {
  //     console.error('微信 JS-SDK 配置失败:', error)
  //   })
  // })
}

// 复制链接
const copyLink = () => {
  let origin = location.origin
  let url = `${origin}/yj/${yjId.value}`

  navigator.clipboard.writeText(url).then(
    () => {
      showNotify({ type: 'success', message: '链接已复制' })
    },
    () => {
      showNotify({ message: '链接复制失败' })
    }
  )
}

const finished = ref(false)
const error = ref(false)
const { loading, setLoading } = useLoading()
loading.value = true

const noteList = ref([])
const pageNum = ref(1)

// 列表下拉加载数据
const getloadList = () => {
  pageNum.value++
  getList()
}

// 获取数据
async function getList() {
  setLoading(true)
  try {
    let { data } = await request('/website/tourism/publishTravelNotes/getDraftList', {
      query: {
        pageNum: pageNum.value,
        pageSize: 10,
        type: 3
      }
    })

    if (data.dataList.length == 0) noteList.value = []

    // 去重处理
    if (noteList.value.length > data.totalCount)
      noteList.value = Array.from(new Set([...noteList.value, ...data.dataList]))
    // 去重处理合并处理
    if (noteList.value.length < data.totalCount)
      noteList.value = noteList.value.concat(data.dataList)

    loading.value = false
    if (noteList.value.length >= data.totalCount) {
      finished.value = true
    }
  } finally {
    setLoading(false)
  }
}

// 下架
async function handleNoteDown(item) {
  showConfirmDialog({
    title: '提示',
    message: '下架后游记将存入草稿，确定下架吗?',
    confirmButtonColor: '#FF9300'
  })
    .then(async () => {
      try {
        await request(`/website/tourism/publishTravelNotes/delist`, {
          method: 'post',
          body: {
            writeId: item.id
          }
        })

        showSuccessToast('下架成功')
        // 页面的渲染处理
        noteList.value = noteList.value.filter((note) => note.id !== item.id)

        getList()
      } catch (error) {}
    })
    .catch(() => {
      // on cancel
    })
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped></style>
