<template>
  <div style="font-size: 14px;margin-bottom: 20px" v-if="!details">
    当前总分:{{countEs}} <span style="margin:0 20px">/</span> 细项总分:{{countIz}}
  </div>
  <el-table
      :data="hlbData"
      :span-method="objectSpanMethod"
      border
      style="width: 100%;"
  >
    <el-table-column prop="bri" label="业务风险项" width="100" />
    <el-table-column prop="es" label="评价分值" width="80" align="center">
      <template #default="scope">
        <el-input v-model="scope.row.es" @change="changeEs(scope.row)" v-if="!details"/>
        <span v-else>{{scope.row.es}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="cs" label="实际得分" width="80" align="center" v-if="details"/>
    <el-table-column prop="eo" label="评价对象" width="120" />
    <el-table-column prop="ri" label="风险指标" width="180" />
    <el-table-column prop="pds" label="扣分标准" width="180" />
    <el-table-column prop="iz" label="细项分" width="100" align="center"/>
    <el-table-column label="当前健康评分标准" width="200">
      <template #default="scope">
        <div v-html="scope.row.chss" />
      </template>
    </el-table-column>
    <el-table-column label="日健康评分标准">
      <template #default="scope">
        <div v-html="scope.row.stand" />
      </template>
    </el-table-column>
  </el-table>
</template>
<script setup lang="ts">
import { watchEffect} from "vue";
import {hlScore} from "@/api/hl/hlScore"
import {hlbDetails} from "@/api/hl/hlb"
import {hlDay} from "@/api/hl/hlDay"
import {updateHld} from "@/api/hl/hld"
const props = defineProps({
  modelId:{
    type:Number,
    required:true
  },
  details:{
    type:Boolean,
    default:false
  },
  dialogType:{
    type:String,
    default:"history"
  }
})
const hlbData = ref([])
const countEs=ref(0)
const countIz=ref(0)
watchEffect(()=>{
  getHtlData()
})

function  calcCountEs(){
  let count = 0;
  hlbData.value.forEach(p=>{
    if(p.length === p.lowLength){
      count += Number(p.es)
    }
  })
  countEs.value = count
}

async function changeEs(row){
  const {hlDetailId,es} = row;
  await updateHld({hlDetailId,hlDetailScore:Number(es)})
  calcCountEs()
}

function  objectSpanMethod ({row,column,rowIndex,columnIndex,})  {
  if (columnIndex === 0||columnIndex === 1||columnIndex === 2||(props.details && columnIndex === 3)||(props.details?columnIndex===7:columnIndex===6)) {
    const f=row.length===row.lowLength
      if(f){
        return {
          rowspan: row.length,
          colspan: 1
        }
      }else{
        return {
          rowspan: 0,
          colspan: 0
        }
      }
  }
  if(props.details?columnIndex===8:columnIndex===7){
    if(rowIndex===0){
      return {
        rowspan: hlbData.value.length,
        colspan: 1
      }
    }else{
      return {
        rowspan: 0,
        colspan: 0
      }
    }
  }
}

function getHtlData(){
  const request = props.details ? (props.dialogType==="day"? hlDay:hlScore) : hlbDetails
  request(props.modelId).then(res=>{
    if(res.data){
      countEs.value = 0
      hlbData.value = []
      formattedData(res.data)
    }
  })
}

function formattedData(data) {
  countIz.value = 0
  data.hlClassList.forEach(item=>{
    if(item.hlObjList.length){
      item.hlObjList.forEach((cItem,cIndex)=>{
        if(cItem.hlDetailList.length){
          cItem.hlDetailList.forEach((ccItem,ccIndex)=>{
            hlbData.value.push({
              es:cItem.hlObjScore,
              eo:cItem.hlObjName,
              cs:ccItem.currScore || 0,
              length:cItem.hlDetailList.length,
              lowLength:cItem.hlDetailList.length-ccIndex,
              chss:cItem.hlObjStand.replace(/\n/g, '<br/>'),
              bri:item.className,
              data:item,
              ri:ccItem.hlDetailName,
              pds:ccItem.remark,
              iz:ccItem.hlDetailScore,
              stand:data.stand.replace(/\n/g, '<br/>'),
              hlDetailId:ccItem.hlDetailId
            })
            countIz.value+=ccItem.hlDetailScore
          })
        }else{
          hlbData.value.push({
            es:cItem.hlObjScore,
            bri:item.className,
            data:item,
            eo:cItem.hlObjName,
            length:cItem.hlDetailList.length,
            lowLength:cItem.hlDetailList.length-cIndex,
            chss:cItem.hlObjStand,
            stand:data.stand.replace(/\n/g, '<br/>')
          })
        }
      })
    }else{
      hlbData.value.push({
        bri:item.className,
        data:item
      })
    }
  })
  calcCountEs()
}

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