<template>
  <view class="p-20 min-height bg-gray-light">
    <view class="card text-25 m-20">
      <view class="flex justify-between items-center">
        <text>考生姓名: {{ realnameVal }}</text>
        <text>考生身份证: {{ idCardVal }}</text>
      </view>
      <view class="flex justify-between items-center my-10">
        <text>打分人: {{ scoreUserNameVal }}</text>
      </view>
      <view class="text-main my-15">总分: {{ totalScore }}</view>
      <view class="text-main my-15">分数: {{ totalGetScore }}</view>
      <view class="flex justify-center items-center">
        <view class="flex-1 mx-15">
          <uv-button type="primary" shape="circle" @click="submit" :disabled="disabled">确认</uv-button>
        </view>
        <view class="flex-1 mx-15">
          <uv-button type="error" shape="circle" @click="refuse" :disabled="disabled">退回</uv-button>
        </view>
      </view>
    </view>
    <view style="height: calc(100vh - 360rpx)" class="overflow-y">
      <view class="card m-20 text-25 flex flex-col justify-start items-start"
            v-for="(item, index) in list" :key="index">
        <view class="flex justify-start items-center mb-10" @click="item.showAll = !item.showAll">
          <text class="text-main mr-10" style="flex: 0 0 100rpx ">
            {{ item.showAll ? '折叠' : '展开详情' }}
          </text>
          <text class="font-bold text-28">{{ item.title }}</text>
        </view>
        <view class="flex flex-col justify-start items-start py-15" v-if="item.showAll">
          <text class="pb-15 font-bold">考试内容</text>
          <text class="text-25 mt-15 pb-15 border-bottom" v-html="item.content"></text>
          <text class="pb-15 font-bold">评分表</text>
          <view v-for="(content, contentIndex) in item.examContent" :key="contentIndex"
                class="p-15 text-25 bg-main-light rounded mb-10 w-90p">
            <text class="font-bold pb-15 border-bottom w-100p">{{ content.title }}(分数: {{
                content.score
              }}/{{ content.totalScore }})
            </text>
            <view class="py-10 flex flex-col justify-start items-start"
                  v-for="(contentItem, contentItemIndex) in content.list" :key="contentItemIndex">
              <text class="mb-10">{{ contentItem.content }}</text>
              <view class="p-10 my-10 w-90p bg-white flex justify-start items-center">
                <text>分数: {{ list[index].examContent[contentIndex].list[contentItemIndex].rating }}</text>
              </view>
              <text class="mb-10 text-gray">配分:{{ contentItem.score }}</text>
              <text class="mb-10 text-gray">最大扣分:{{ contentItem.maxReduce }}</text>
              <text class="mb-10 text-gray">扣分步长:{{ contentItem.step }}</text>
              <text class="mb-10 text-gray">评分标准:{{ contentItem.standard }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import {ref} from 'vue'
import {
  operatePaperDetailReq,
  operateScoreActionReq,
  operateScoreCheckReq,
  operateScoreListReq,
  operateScoreRefuseReq
} from "@/api/exam";
import {onLoad} from "@dcloudio/uni-app";
import {$toast} from "@/utils";

const list = ref([])
const getPaperDetailList = async (paperId) => {
  const {data} = await operatePaperDetailReq({
    paperId
  })
  list.value = data.map(item => {
    item.showAll = true
    item.score = ''
    item.examContent = JSON.parse(item.examContent).map(content => {
      content.score = 0
      content.totalScore = 0
      content.list.map(contentItem => {
        contentItem.rating = parseInt(contentItem.score)
        contentItem.score = parseInt(contentItem.score)
        contentItem.maxReduce = parseInt(contentItem.maxReduce)
        content.score += contentItem.rating
        content.totalScore += contentItem.rating
        totalScore.value += contentItem.rating
        return contentItem
      })
      return content
    })
    return item
  })
}

const getScoreList = async (paperId, userId) => {
  const {data} = await operateScoreListReq({
    paperId, userId
  })
  data.forEach(score => {
    list.value.forEach(item => {
      item.examContent.forEach(content => {
        content.list.map(contentItem => {
          if (contentItem.content === score.title && item.id === score.detailId) {
            contentItem.rating = score.score
          }
          return contentItem
        })
      })
    })
  })
  calTotalScore()
}

const totalScore = ref(0)
const totalGetScore = ref(0)

const calTotalScore = () => {
  totalGetScore.value = 0
  list.value.forEach(item => {
    item.examContent.forEach(content => {
      content.list.forEach(contentItem => {
        totalGetScore.value += contentItem.rating
      })
    })
  })
}

const disabled = ref(false)
const submit = async () => {
  uni.showModal({
    title: '提示',
    content: `确认核分吗?`,
    success: async ({confirm}) => {
      if (confirm) {
        disabled.value = true
        uni.showLoading({title: '提交中'})
        await operateScoreCheckReq({
          examId: examVal,
          paperId: paperIdVal,
          userId: userIdVal,
          scoreUserId: scoreUserIdVal,
        }).catch(() => {
          disabled.value = false
        })
        disabled.value = false
        uni.hideLoading()
        $toast('操作成功')
        setTimeout(async () => {
          await uni.navigateBack()
        }, 1500)
      }
    }
  })
}

const refuse = async () => {
  uni.showModal({
    title: '提示',
    content: `确认退回吗?`,
    success: async ({confirm}) => {
      if (confirm) {
        disabled.value = true
        uni.showLoading({title: '提交中'})
        await operateScoreRefuseReq({
          examId: examVal,
          paperId: paperIdVal,
          userId: userIdVal,
          scoreUserId: scoreUserIdVal,
        }).catch(() => {
          disabled.value = false
        })
        disabled.value = false
        uni.hideLoading()
        $toast('操作成功')
        setTimeout(async () => {
          await uni.navigateBack()
        }, 1500)
      }
    }
  })
}


const idCardVal = ref('')
const realnameVal = ref('')
const scoreUserNameVal = ref('')
let examVal = null, paperIdVal = null, userIdVal = null, scoreUserIdVal = null
onLoad(async ({paperId, idCard, realname, examId, userId, scoreUserId, scoreUserName}) => {
  await getPaperDetailList(paperId)
  await getScoreList(paperId, userId)
  idCardVal.value = idCard
  realnameVal.value = realname
  examVal = examId
  paperIdVal = paperId
  userIdVal = userId
  scoreUserIdVal = scoreUserId
  scoreUserNameVal.value = scoreUserName
})
</script>