<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '当前志愿表',
  },
}
</route>

<template>
  <view class="h-screen flex flex-col">
    <web-view :src="webUrl" :update-title="false" @message="handleMessage" />
  </view>
</template>

<script setup lang="ts">
import { useUserStore } from '@/store/user'

import { putWishList, saveWishList } from '@/service/index/api'
const userStore = useUserStore()

const wishListName = ref('')

const editWishList = (_wishList: any[]) => {
  let _vTbDetails = _wishList.map((item, index) => {
    return {
      unId: item.unId || item.uId,
      unName: item.unName || item.universityName,
      unCode: item.unCode || item.collegeCode,
      type: item.type,
      sort: index + 1,
      percentAge: item.percentAge,
      vItems: item.vItems.map((vItem, vIndex) => {
        return {
          sort: vIndex + 1,
          _pId: vItem._pId || vItem.planId,
          major: vItem.major,
          majorCode: vItem.majorCode,
          majorGroup: vItem.majorGroup,
          type: vItem.type,
          percentAge: vItem.percentAge,
        }
      }),
    }
  })

  let params = {
    vId: vTbId.value,
    cId: userStore.userInfo.estimatedAchievement.wxId,
    tableName: wishListName.value,
    batchName: userStore.userInfo.batchName,
    type: typeName,
    subjectClaim: userStore.userInfo.estimatedAchievement.subjectGroup,
    score: +userStore.userInfo.estimatedAchievement.expectedScore,
    vTbDetails: _vTbDetails,
    LocationCode: userStore.userInfo.estimatedAchievement.provinceCode,
  }

  putWishList(params).then((res) => {
    if (res.code === 200) {
      vTbId.value = res.result as number
      if (!changeFlag) {
        userStore.clearWishList()
        userStore.clearWishListId()
      }
    } else {
      uni.showToast({
        title: res.message || '保存失败',
        icon: 'none',
      })
    }
  })
}

const addWishList = () => {
  let _wishList = userStore.userInfo.wishList
  let _vTbDetails = _wishList.map((item, index) => {
    return {
      unId: item.unId || item.uId,
      unName: item.unName || item.universityName,
      unCode: item.unCode || item.collegeCode,
      type: item.type,
      sort: index + 1,
      percentAge: item.percentAge,
      vItems: item.vItems.map((vItem, vIndex) => {
        return {
          sort: vIndex + 1,
          _pId: vItem.planId,
          major: vItem.major,
          majorCode: vItem.majorCode,
          majorGroup: vItem.majorGroup,
          type: vItem.type,
          percentAge: vItem.percentAge,
        }
      }),
    }
  })
  let params = {
    cId: userStore.userInfo.estimatedAchievement.wxId,
    tableName: wishListName.value,
    batchName: userStore.userInfo.batchName,
    type: typeName,
    subjectClaim: userStore.userInfo.estimatedAchievement.subjectGroup,
    score: +userStore.userInfo.estimatedAchievement.expectedScore,
    vTbDetails: _vTbDetails,
    LocationCode: userStore.userInfo.estimatedAchievement.provinceCode,
  }
  saveWishList(params).then((res) => {
    if (res.code === 200) {
      vTbId.value = res.result as number
      userStore.setWishListId({ id: vTbId.value })
    } else {
      uni.showToast({
        title: res.message || '保存失败',
        icon: 'none',
      })
    }
  })
}

let typeName = ''
let editType = ''
let changeFlag = false

let vTbId = ref(0)
let webUrl = computed(() => {
  if (vTbId.value !== 0) {
    //sort.ycymedu.com
    return `https://sort.ycymedu.com/sort-college?id=${vTbId.value}&token=${userStore.userInfo.token}&score=${userStore.userInfo.estimatedAchievement.expectedScore}&batchName=${userStore.userInfo.batchName}&subjectGroup=${userStore.userInfo.estimatedAchievement.subjectGroup}&location=${userStore.userInfo.estimatedAchievement.provinceCode}&requireSubject=${userStore.userInfo.estimatedAchievement.requireSubject.map((item) => item.name)}&timestamp=${new Date().getTime()}`
  }
  return ''
})

const handleMessage = (evt) => {
  const actionsData = evt.detail.data as any[]
  actionsData.forEach((item) => {
    if (item.action === 'delete' && item.deleteType === 'major') {
      let deleteObj = JSON.parse(item.deleteObj)
      let deleteCollege = JSON.parse(item.deleteCollege)
      userStore.deleteWishListMajor({ uId: deleteCollege.unId, planId: deleteObj._pId })
    } else if (item.action === 'delete' && item.deleteType === 'college') {
      let deleteObj = JSON.parse(item.deleteObj)
      userStore.deleteWishListCollege({ uId: deleteObj.unId })
    } else if (item.action === 'message') {
      let data = JSON.parse(item.message)
      changeFlag = false
      editWishList(data)
    }
  })
}

onLoad((options) => {
  typeName = options.typeName
  editType = options.editType
  changeFlag = options.change || false
  if (editType === 'edit') {
    vTbId.value = options.id
    if (changeFlag) {
      editWishList(userStore.userInfo.wishList)
    }
  } else {
    addWishList()
  }
})

onUnload(() => {
  if (typeName === '一键填报') {
    userStore.clearWishList()
    userStore.clearWishListId()
  }
})
</script>

<style lang="scss" scoped>
.button-group {
  box-shadow: 0rpx -8rpx 8rpx 0rpx rgba(225, 225, 225, 0.2);
}

.input-border {
  border: 1rpx solid #ccc;
  border-radius: 8rpx;
  padding: 20rpx;
}
</style>
