<script setup lang="ts">

import { useRoute,useRouter } from 'vue-router'
import { ref } from 'vue'
import { fieldMap } from '@/views/companyScoreDetail/map'
import request from '@/utils/request'

const route = useRoute()
const router = useRouter()
const unifiedSocialCreditCode = ref<string>(route.query.unifiedSocialCreditCode as string)
const enterpriseName = ref<string>(route.query.enterpriseName as string)
const grade = ref<string>(route.query.grade as string)
const total = ref<string>()
if (route.query.total) {
  total.value = Number(route.query.total).toFixed(2)
}
const getMostDetailByUniCode = (code: string) => {
  return request.get('/crmHighTechZoneEnterprises/getCreditScoreMostDetailsByUniCode', {
    params: {
      unifiedSocialCreditCode: code
    }
  })
}
const data = ref<Map<string, string>>(new Map)
const tempData1 = ref<any>([])
const tempData2 = ref<any>([])
const tempData3 = ref<any>([])
const tempData4 = ref<any>([])
const tempData5 = ref<any>([])
const tempData6 = ref<any>([])
const tableData1 = ref([])
const tableData2 = ref([])
const tableData3 = ref([])
const tableData4 = ref([])
const tableData5 = ref([])
const tableData6 = ref([])
const sum1 = ref(0)
const sum2 = ref(0)
const sum3 = ref(10)
const sum4 = ref(20)
const sum5 = ref(20)
const sum6 = ref(0)
const init = async () => {
  const res: any = await getMostDetailByUniCode(unifiedSocialCreditCode.value)
  if (res.code == 200) {
    for (const resKey in res.data) {
      data.value.set(fieldMap[resKey], res.data[resKey])
    }
  }
  let count = 0
  for (const entry of data.value) {
    if (count <= 17) {
      tempData1.value.push(entry)
      count++
    } else if (count <= 29) {
      tempData2.value.push(entry)
      count++
    } else if (count <= 34) {
      tempData3.value.push(entry)
      count++
    } else if (count <= 74) {
      tempData4.value.push(entry)
      count++
    } else if (count <= 83) {
      tempData5.value.push(entry)
      count++
    } else if (count <= 91) {
      tempData6.value.push(entry)
      count++
    } else {
      break
    }
  }
  setTableDataValueAndSum(tableData1, tempData1, sum1)
  setTableDataValueAndSum(tableData2, tempData2, sum2)
  setTableDataValueAndSum(tableData3, tempData3, sum3)
  setTableDataValueAndSum(tableData4, tempData4, sum4)
  setTableDataValueAndSum(tableData5, tempData5, sum5)
  setTableDataValueAndSum(tableData6, tempData6, sum6)
}
const setTableDataValueAndSum = (tableData: any, tempData: any, sum: any) => {
  tableData.value = tempData.value.reduce((acc: any, cur: any) => {
    acc[0][cur[0]] = cur[1]
    return acc
  }, [{}])
  for (const k in tableData.value[0]) {
    sum.value += tableData.value[0][k]
  }
}
init()
const backToLastPage = ()=>{
  router.back();
}
</script>

<template>
  <div class="conpanyScoreDetailContainer">
    <div class="w-1400px m-auto pt-20px pb-20px">
      <el-card class="box-card">
        <div class="clearfix flex justify-start items-center">
          <el-icon @click="backToLastPage" class="cursor-pointer" size="25">
            <ArrowLeft />
          </el-icon>
          <span class="text-2xl font-bold">&nbsp;企业评分详情</span>
        </div>
        <el-row :gutter="20" class="mt-4">
          <el-col :span="12">
            <div class="grid-content">
              <div class="flex items-center mb-2">
                <span class="text-lg font-medium mr-2">统一社会信用代码:</span>
                <span>{{ unifiedSocialCreditCode }}</span>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content">
              <div class="flex items-center mb-2">
                <span class="text-lg font-medium mr-2">企业名称:</span>
                <span>{{ enterpriseName }}</span>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="mt-4">
          <el-col :span="12">
            <div class="grid-content">
              <div class="flex items-center mb-2">
                <span class="text-lg font-medium mr-2">等级:</span>
                <span>{{ grade }}</span>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content">
              <div class="flex items-center mb-2">
                <span class="text-lg font-medium mr-2">总分:</span>
                <span>{{ total }}</span>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-table :data="tableData1" style="width: 100%">
          <el-table-column :label="'企业竞争力得分详细(满分30分)此项得分为：'+sum1.toFixed(2)">
            <el-table-column width="150" v-for="(item,index) in tempData1" :key="index" :label="item[0]">
              <template #default="scope">
                {{ scope.row[item[0]] }}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <el-table :data="tableData2" style="width: 100%">
          <el-table-column :label="'财务得分详细(满分10分)此项得分为：'+(sum2<=4?4:sum2).toFixed(2)">
            <el-table-column width="150" v-for="(item,index) in tempData2" :key="index" :label="item[0]">
              <template #default="scope">
                {{ scope.row[item[0]] }}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <el-table :data="tableData3" style="width: 100%">
          <el-table-column :label="'司法裁判得分详细(满分10分)此项得分为：'+sum3.toFixed(2)">
            <el-table-column width="150" v-for="(item,index) in tempData3" :key="index" :label="item[0]">
              <template #default="scope">
                {{ scope.row[item[0]] ? scope.row[item[0]] : 0 }}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <el-table :data="tableData4" style="width: 100%">
          <el-table-column :label="'行政处罚得分详细(满分20分)此项得分为：'+sum4.toFixed(2)">
            <el-table-column width="150" v-for="(item,index) in tempData4" :key="index" :label="item[0]">
              <template #default="scope">
                {{ scope.row[item[0]] }}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <el-table :data="tableData5" style="width: 100%">
          <el-table-column :label="'行政监管、践诺履约得分详细(满分20分)此项得分为：'+sum5.toFixed(2)">
            <el-table-column width="150" v-for="(item,index) in tempData5" :key="index" :label="item[0]">
              <template #default="scope">
                {{ scope.row[item[0]] }}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <el-table :data="tableData6" style="width: 100%">
          <el-table-column :label="'参考信用评价得分详细(满分10分)此项得分为：'+sum6.toFixed(2)">
            <el-table-column width="150" v-for="(item,index) in tempData6" :key="index" :label="item[0]">
              <template #default="scope">
                {{ scope.row[item[0]] }}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.conpanyScoreDetailContainer {
  background-color: #ecf0f9;
}
</style>