<template>
  <div class="w-full h-full pb-10">
    <!-- style="height: calc(100vh - 105px);" -->
    <!-- 审核中 -->
    <ProfileNotesEmpty v-if="!loading && !draftList.length" />

    <div v-else-if="draftList.length">
      <van-list
        v-model:loading="loading"
        v-model:error="error"
        :finished="finished"
        :immediate-check="false"
        offset="80"
        error-text="请求失败，点击重新加载"
        finished-text=""
        @load="getloadList"
      >
        <ProfileNotesAuditingItem
          v-for="item in draftList"
          :key="item.id"
          :data="item"
          @on-revoke="handleRevoke(item)"
        />
      </van-list>
    </div>
  </div>
</template>

<script setup>
const { loading, setLoading } = useLoading()
loading.value = true
const finished = ref(false)
const error = ref(false)
const pageNum = ref(1)
const draftList = ref([])

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

async function getNotesList() {
  setLoading(true)

  try {
    const { data } = await request('/website/tourism/publishTravelNotes/getDraftList', {
      query: {
        pageNum: pageNum.value,
        pageSize: 10,
        type: 1
      }
    })

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

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

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

async function handleRevoke(item) {
  showConfirmDialog({
    title: '提示',
    message: '确定撤销审核吗?',
    confirmButtonColor: '#FF9300'
  })
    .then(async () => {
      try {
        await request(`/website/tourism/publishTravelNotes/withdraw`, {
          method: 'post',
          body: {
            writeId: item.id
          }
        })
        showSuccessToast('撤销成功')
        // 页面的渲染处理
        draftList.value = draftList.value.filter((note) => note.id !== item.id)

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

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

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