<!--  -->
<template>
  <view>
    <resume v-if="showResumeList" resumeAnalysis @submit="handleResumeAnalysis"></resume>
    <view v-if="formLIst?.length">
      <uni-card v-for="item of formLIst" :key="item.id" :id="item.id">
		    <uni-section :title="item.text" type="line">
          <template v-slot:right>
            <view v-if="item.path !== 'baseInfoEdit'" style="color: #e64340;" @click="del(item)">删除</view>
          </template>
          <avatarEdit v-if="item.path === 'avatarEdit'" ref="componentRef" :id="item.id" :data="item.data" />
          <baseInfoEdit v-if="item.path === 'baseInfoEdit'" ref="componentRef" :id="item.id" :data="item.data" />
        </uni-section>
      </uni-card>
    </view>
    <view v-else>
      <view v-if="loading">加载中...</view>
      <view v-else>加载失败</view>
    </view>
    <!-- 确认框 -->
    <uni-popup ref="confirmRef" type="dialog">
      <uni-popup-dialog
        type="warn"
        cancelText="取消"
        confirmText="确认" 
        title="系统提示"
        :content="dialogContent"
        @confirm="handleConfirm"
        @close="null"
      ></uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script setup>
defineOptions({name: 'resumeAnalysis-index'})
// import { saveResumeInfo, resumeParser2 } from '@/api/recruit/personal/resume'
// import { resumeParser2 } from '@/api/recruit/personal/resume'
// import { envObj, baseUrl } from '@/utils/config'
import resume from '../resume/index.vue'
import { ref, shallowRef } from 'vue'
import baseInfoEdit from './components/baseInfoEdit.vue'
import avatarEdit from './components/avatarEdit.vue'
import { data } from './testData.js'

const showResumeList = ref(true)

const handleResumeAnalysis = (url) => {
  if (!url) {
    return uni.showToast({ icon: 'none', title: '请选择要解析的简历' })
  }
  showResumeList.value = false
  // handleAnalysis(url)
}

const exampleList = {
  avatar: { text: '头像', id: 'avatar', path: 'avatarEdit' },
  person: { text: '基础信息', id: 'person', path: 'baseInfoEdit' },
  advantage: { text: '个人优势', id: 'advantage', path: 'advantage' },
  eduList: { text: '教育经历', id: 'eduList', path: '' },
  workList: { text: '工作经历', id: 'workList', path: '' },
  trainList: { text: '培训经历', id: 'trainList', path: '' },
}

const resumeTxt = ref([]) // 查看文本信息
const formLIst = shallowRef([])
const transformToLIst = async (result) => {
  formLIst.value = []
  if (result && Object.keys(result)) {
    if (result.resume?.rawText) resumeTxt.value = result.resume.rawText.split('\n') || []
    if (result.person?.advantage) result.advantage = result.person.advantage
    if (result.person?.avatar) result.avatar = result.person.avatar
    // obj
    const dealObjKeys = ['avatar', 'person', 'advantage']
    dealObjKeys.forEach(key => {
      if (result[key]) {
        const obj = {...exampleList[key]}
        obj.data = result[key]
        formLIst.value.push(obj)
      }
    })
    // arr
    const dealArrKeys = ['eduList', 'workList', 'trainList']
    dealArrKeys.forEach(key => {
      if (result[key]?.length) {
        for (let index = 0; index < result[key].length; index++) {
          const obj = {...exampleList[key]}
          obj.id = obj.id + '_' + index
          obj.text = result[key].length > 1 ? obj.text + (index+1) : obj.text
          obj.data = result[key][index]
          formLIst.value.push(obj)
        }
      }
    })
  }
  // console.log('formLIst:', formLIst.value)
}

const confirmRef = ref()
const dialogContent = ref('')
let delId = null
const del = (item) => {
  dialogContent.value =  `是否确认删除${item.text}？`
  delId = item.id
  confirmRef.value.open()
}
const handleConfirm = () => {
  formLIst.value = formLIst.value.filter(e => e.id !== delId)
}

const result = ref(JSON.parse(JSON.stringify(data)))
const loading = ref(false)
transformToLIst(result.value)
// const handleAnalysis = async (url) => {
//   url = decodeURIComponent(url)
//   if (!url) return
//   showSelect.value = false
//   loading.value = true
//   // const baseUrl = envObj.previewUrl
//   // fileUrl.value = !url.includes('.pdf') ?  `${baseUrl}/onlinePreview?url=${encodeURIComponent(Base64.encode(url))}` : url
//   try {
//     const data = await resumeParser2({ fileUrl: url })
//     result.value = data || {}
//     await transformToLIst(result.value)
//   } catch (error) {
//     console.log(error)
//   } finally {
//     loading.value = false
//   }
// }
</script>
<style lang="scss" scoped>
</style>
