<template>
  <view class="ss-p-b-100">
    <view v-if="bioList.length > 0">
			<uni-card v-for="(item, index) in bioList" :key="index" :is-shadow="true" :border='false' shadow="0px 0px 3px 1px rgba(0,0,0,0.1)">
				<view class="d-flex align-center">
          <view @click="preview(item.url)"  style="flex: 1;">
            <view style="font-weight: bold;">{{ item.title }}</view>
				    <view>上传时间：{{ timesTampChange(item.createTime, 'Y-M-D') }}</view>
          </view>
          <view class="ss-m-l-30" style="width: 60rpx;">
            <uni-icons @click="handleOpenPopup(item)" type="more-filled" size="20"></uni-icons>
          </view>
        </view>
			</uni-card>
		</view>
		<view v-else class="nodata-img-parent">
			<image src="https://minio.citupro.com/dev/static/nodata.png" mode="widthFix" style="width: 100vw;height: 100vh;"></image>
		</view>
    <view class="bottom-sticky">
      <button class="recomm-button" @click="handleUpload">微信聊天文件上传</button>
    </view>

    <uni-popup ref="popup" type="bottom">
      <button class="big-cancel-button" style="color: #333 !important;" @click="handleDelete">删除附件</button>
      <button class="big-cancel-button" @click="popup.close(); currentId = ''">取消</button>
    </uni-popup>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { getPersonResumeCv, saveResume, deleteResume } from '@/api/user'
import { uploadFileTest } from '@/api/file'
import { timesTampChange } from '@/utils/date'
import { preview } from '@/utils/preview'

// 获取附件
const bioList = ref([])
const getList = async () => {
  const { data } = await getPersonResumeCv()
  bioList.value = data
}
getList()

// 更多
const currentId = ref('')
const popup = ref()
const handleOpenPopup = (item) => {
  currentId.value = item.id
  popup.value.open()
}

// 删除附件
const handleDelete = async () => {
  if (!currentId.value) return
  await deleteResume(currentId.value)
  uni.showToast({
    title: '删除成功',
    icon: 'success',
    duration: 2000
  })
  currentId.value = ''
  popup.value.close()
  getList()
}

// 上传附件
const handleUpload = () => {
  wx.chooseMessageFile({
    count: 1,
    type: 'file',
    success (res) {
      const title = res.tempFiles[0].name
      const path = res.tempFiles[0].path
      //效验是否为支持的文件格式
      if(/\.(pdf|docx|doc)$/.test(title)){
        uploadFileTest(path).then(async (res) => {
          if (!res.data) {
            uni.showToast({
              title: '上传失败',
              icon: 'none'
            })
            return
          }
          await saveResume({ title, url: res.data })
          uni.showToast({
            title: '上传成功',
            icon: 'success'
          })
          getList()
        })
      }else{
        uni.showToast({
          icon: 'none',
          title: '请上传pdf、word类型的文件',
          duration: 2000
        })
        return
      }
    }
  })
}
</script>

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