<template>
  <view class="ss-p-b-100" style="height: 100vh; background-color: #f2f4f7;">
    <uni-notice-bar text="温馨提示：最多可以上传5份附件简历。请在手机上打开此小程序进行文件上传，暂不支持在桌面版小程序中上传文件。" />
    <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 v-if="props.resumeAnalysis" class="ss-m-r-15">
            <radio :value="index" :checked="index === checkedIndex" @tap="radioChange(index)" />
          </view>
          <view @click="preview(item.url)"  style="flex: 1;">
            <view class="font-size-14" 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 flex-column">
      <!-- <button class="recomm-button" style="margin-bottom: 0;" :loading="analysisLoading" @click="handleResumeAnalysis">开始解析</button> -->
      <button class="recomm-button" style="margin-bottom: 0;" @click="handleUpload">微信聊天文件上传</button>
      <view class="color-primary font-size-14 ss-m-b-25 ss-m-t-10" style="text-align: center;">上传文件大小不能超过20MB</view>
    </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 { uploadFile } from '@/api/file'
import { timesTampChange } from '@/utils/date'
import { preview } from '@/utils/preview'

const emit = defineEmits(['submit'])
const props = defineProps({
  resumeAnalysis: { type: Boolean, default: false }
})

// 获取附件
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 = () => {
  if (bioList.value.length >= 5) {
    uni.showToast({
      icon: 'none',
      title: '最多可上传5份简历'
    })
    return
  }
  wx.chooseMessageFile({
    count: 1,
    type: 'file',
    success (res) {
      // 限制文件上传大小
      const size = res.tempFiles[0].size
      if (size / (1024*1024) > 20) {
        uni.showToast({ icon: 'none', title: '文件大小不能超过20M' })
        return
      }

      const title = res.tempFiles[0].name
      const path = res.tempFiles[0].path
      //效验是否为支持的文件格式
      if(/\.(pdf|docx|doc)$/.test(title)){
        uploadFile(path, 'attachment').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、doc、docx类型的文件',
          duration: 2000
        })
        return
      }
    }
  })
}

const fileUrl = ref('')
const checkedIndex = ref()
const radioChange = (index) => {
  if (!props.resumeAnalysis) return
  checkedIndex.value = index
  if (bioList.value[index]?.url) fileUrl.value = encodeURIComponent(bioList.value[index].url)
}

const analysisLoading = ref(false)
const handleResumeAnalysis = () => {
  if (!fileUrl.value) return uni.showToast({ icon: 'none', title: '请选择要解析的简历' })
  analysisLoading.value = true
  emit('submit', fileUrl.value)
	// uni.navigateTo({
	// 	url: `/pagesA/resumeAnalysis/index?fileUrl=${fileUrl.value}`
	// })
}


</script>

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