<script name="safeInfo" setup>
defineProps({
  detailData: Object
})
const isJson = (str) => {
  if (typeof str === 'string') {
    try {
      var obj = JSON.parse(str)
      if (typeof obj === 'object' && obj) {
        return true
      } else {
        return false
      }
    } catch (e) {
      return false
    }
  }
}
</script>

<template>
  <template v-if="isJson(detailData.safetyInputDescribe)">
    <el-descriptions
      v-for="(item,index) in JSON.parse(detailData.safetyInputDescribe)"
      :key="index"
      :column="1"
      border
      class="mb-16"
    >
      <el-descriptions-item label="投入项">
        {{ item.safetyInputTitle||'-' }}
      </el-descriptions-item>
      <el-descriptions-item label="内容描述：">
        {{ item.safetyInputDescribe ||'-' }}
      </el-descriptions-item>
      <el-descriptions-item label="金额（万元）：">
        {{ item.safetyInputAmount }}
      </el-descriptions-item>
    </el-descriptions>
  </template>
  <template v-else>
    <el-descriptions
      :column="1"
      border
      class="mb-16"
    >
      <el-descriptions-item label="投入项">
        {{ detailData.safetyInputTitle||'-' }}
      </el-descriptions-item>
      <el-descriptions-item label="内容描述：">
        {{ detailData.safetyInputDescribe ||'-' }}
      </el-descriptions-item>
      <el-descriptions-item label="金额（万元）：">
        {{ detailData.safetyInputAmount }}
      </el-descriptions-item>
    </el-descriptions>
  </template>
  <el-descriptions
    :column="1"
    border
    class="mb-16"
  >
    <el-descriptions-item label="安全投入占比：">
      {{ ((JSON.parse(detailData.safetyInputDescribe)?.reduce((acc, cur) => acc + cur.safetyInputAmount, 0)/detailData.declareAmount)*100).toFixed(2) }}%
    </el-descriptions-item>
  </el-descriptions>
</template>
