<template>
  <div id="wrap">
    <div class="flex items-center">
      <span class="mr-2 text-sm font-semibold text-[#333] whitespace-nowrap">评级卡名称</span>
      <el-input class="w-[220px]"
                maxlength="20"
                v-model="form.scoreCardName"></el-input>
      <span class="ml-10 mr-2 text-sm font-semibold text-[#333] whitespace-nowrap">评分对象</span>
      <div class="w-[220px] ">
        <el-select v-model="form.scoringObject">
          <el-option v-for="item in scoringObjects"
                     :key="item"
                     :label="item.name"
                     :value="item.id" />
        </el-select>
      </div>
    </div>
    <h6 class="mt-5 mb-3 text-sm text-[#006BFF] font-semibold">评分表</h6>
    <el-table v-if="form.scoringObject"
              class="c-table"
              :key="1"
              :data="list1"
              :span-method="span1"
              border>
      <el-table-column prop="name"
                       label="选用"
                       width="100">
        <template #default="{ row }">
          <div class="items-center">
            <el-checkbox class="mr-2"
                         v-model="row.isChoose"></el-checkbox>
            <span v-if="row.top">{{row.name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="sub"
                       label="维度"
                       width="150"></el-table-column>
      <el-table-column prop="remark"
                       label="计算公式">
        <template #default="{ row }">
          <span v-if="isSelect(row.answerId1)">{{ row.remark }}</span>
          <el-select v-else
                     v-model="row.calculationFormula">
            <el-option v-for="item in row?.recordChildren[1]?.subject?.itemList"
                       :key="item"
                       :value="item.options" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="index"
                       label="指标"
                       width="110"
                       align="center">
        <template #default="{ row }">
          <span>{{ isSelect(row.answerId1) ? row.index : row?.recordChildren[2]?.recordChildren[0]?.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分值"
                       align="center">
        <template #default="{ row }">
          <el-select v-if="isSelect(row.answerId1)"
                     v-model="row.score">
            <el-option v-for="item in row?.subject?.itemList"
                       :key="item"
                       :value="item.options" />
          </el-select>
          <div v-else
               :class="'flex items-center'">
            <span v-if="row.answerId2 === 643"
                  class="mr-2 whitespace-nowrap">全部为正常贷款满分，</span>
            <span class="whitespace-nowrap">
              {{ row.answerId2 === 627 ?
                '每少1倍扣' :
               (row.answerId1 === 590 || row.answerId1 === 591 || row.answerId1 === 592) ?
                    row?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.name.replace('分','') :
                    row.answerId1 === 595 ?
                      '每降1%扣' :
                      '' }}
            </span>
            <div class="w-[100px] mx-2">
              <el-select v-model="row.score">
                <el-option v-for="item in row?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.subject?.itemList"
                           :key="item"
                           :value="item.options" />
              </el-select>
            </div>
            <span>分</span>
            <span v-if="row.answerId2 !== 627 && row.answerId2 !== 646"
                  class="mx-2 whitespace-nowrap">
              {{
                row.answerId2 === 607 || row.answerId2 === 609 ?
                  '，大于等于' :
                  row.answerId2 === 667 || row.answerId2 === 668 || row.answerId2 === 670 || row.answerId2 === 694 || row.answerId2 === 695 ?
                    '，负增长不计分' :
                    '，小于等于'
              }}
            </span>
            <template v-if="row.answerId2 !== 627 && row.answerId2 !== 646 && row.answerId2 !== 667 && row.answerId2 !== 668 && row.answerId2 !== 670 && row.answerId2 !== 694 && row.answerId2 !== 695">
              <div class="w-[120px] mx-2">
                <el-select placeholder=""
                           v-model="row.percentage">
                  <el-option v-for="item in row.answerId2 === 669 || row.answerId2 === 671 ? row?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.subject?.itemList : row?.recordChildren[2]?.recordChildren[0]?.recordChildren[1]?.subject?.itemList"
                             :key="item"
                             :value="item.options" />
                </el-select>
              </div>
              <span class="whitespace-nowrap">不计分</span>
            </template>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-else
              class="c-table"
              :key="2"
              :data="list"
              :span-method="span"
              border>
      <el-table-column prop="name"
                       label="选用"
                       min-width="150"
                       align="center">
        <template #default="{ row }">
          <el-checkbox v-model="row.isChoose"></el-checkbox>
        </template>
      </el-table-column>
      <el-table-column prop="name"
                       label="维度"
                       min-width="150"
                       align="center"></el-table-column>
      <el-table-column prop="sub"
                       label="指标"
                       min-width="150"
                       align="center"></el-table-column>
      <el-table-column label="分值"
                       min-width="150"
                       align="center">
        <template #default="{ row }">
          <el-select v-model="row.score">
            <el-option v-for="item in row?.itemList"
                       :key="item"
                       :value="item.options" />
          </el-select>
        </template>
      </el-table-column>
    </el-table>
    <p class="mt-4 mb-2 text-sm font-semibold text-[#333] whitespace-nowrap">描述</p>
    <el-input placeholder="请输入"
              maxlength="100"
              type="textarea"
              v-model="form.description"></el-input>
    <div class="flex justify-center">
      <div class="submit"
           @click="submit">完成配置</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue';
import { ElLoading, ElMessage } from 'element-plus';
import { saveCredit, findCredit } from '@/api/model';
import { getProcessInformationBasedOnRoles, addOperation } from '@/api/judgment';
import { useRouter, useRoute } from 'vue-router';
import type { TableColumnCtx } from 'element-plus';
import { handleId } from '@/utils/common';
import Cookies from 'js-cookie';

const emit = defineEmits(['getList']);

const router = useRouter();
const route = useRoute();
const id = computed(() => +route.query.creditId);
const projectId = +Cookies.get('sand-projectId');
const levelId = +Cookies.get('sand-level');
let loading: boolean = true;
const list = ref<Record<string, any>[]>([]);
const list1 = ref<Record<string, any>[]>([]);
const detail = ref<Record<string, any>>({});
const scoringObjects = ref<Array<any>>([
  {
    id: 0,
    name: '个人',
  },
  {
    id: 1,
    name: '企业',
  },
]);
const form = reactive({
  checkpointId: levelId,
  projectId,
  description: '',
  scoreCardName: '',
  scoringObject: 0,
  id: computed(() => route.query.creditId || ''),
});

// 配置项
const getConfig = async () => {
  const { process } = await getProcessInformationBasedOnRoles(148);
  // 个人
  const result = [];
  process[0]?.recordChildren.slice(3).forEach((e, j) => {
    e?.recordChildren.forEach((n, i) => {
      if (i) {
        const cur = !form.scoringObject && detail.value[j];
        result.push({
          answerId1: e.id,
          answerId2: n.id,
          subjectId: n.subjectId,
          id: cur ? cur?.dimensionIndexList[i - 1]?.id : '',
          dimensionId: cur ? cur?.dimensionId : '',
          strategyId: cur ? cur?.strategyId : '',
          name: e.name,
          sub: n.name,
          isChoose: cur ? !!cur?.isChoose : false,
          score: cur ? cur?.dimensionIndexList[i - 1]?.score + '' : '0',
          span: i === 1 ? 1 : 0,
          parent: i === 1 ? '' : j,
          itemList: n?.subject?.itemList || [],
        });
      }
    });
  });
  list.value = result;

  // 企业
  const result1 = [];
  process[1]?.recordChildren.slice(3).forEach((e, j) => {
    const cur = form.scoringObject && detail.value[j];
    result1.push({
      answerId1: e.id,
      dimensionId: '',
      name: e.name,
      isChoose: false,
      top: 1,
    });
    e?.recordChildren.forEach((n, i) => {
      if (i) {
        let temp = {
          answerId1: e.id,
          answerId2: n.id,
          id: '',
          dimensionId: '',
          name: e.name,
          sub: n.name,
          index: n?.recordChildren[1]?.name,
          subject: n?.recordChildren[1]?.subject,
          remark: n.remark,
          isChoose: false,
          calculationFormula: '',
          score: '',
          percentage: '',
          span: i === 1 ? 1 : 0,
          middle: 1, // 第二层的标识
          recordChildren: n?.recordChildren,
        };
        result1.push(temp);
        if (e.id === 589 || e.id === 594) {
          n?.recordChildren.forEach((m, o) => {
            if (o > 1) {
              temp = JSON.parse(JSON.stringify(temp));
              temp.parent = i;
              temp.index = m.name;
              temp.subject = m.subject;
              delete temp.middle;
              result1.push(temp);
            }
          });
        }
      }
    });
  });

  if (form.scoringObject && detail.value.length) {
    result1[0].dimensionId = detail.value[0]?.dimensionId;
    result1[0].isChoose = !!detail.value[0]?.isChoose;

    // 企业素质下的4大块有下拉的
    result1[1].dimensionId = detail.value[1]?.dimensionId;
    result1[1].isChoose = !!detail.value[1]?.isChoose;

    result1[1].id = detail.value[1].dimensionIndexList[0]?.id;
    result1[1].dimensionId = detail.value[1].dimensionIndexList[0]?.dimensionId;
    result1[1].score = detail.value[1].dimensionIndexList[0]?.score;

    result1[2].id = detail.value[1].dimensionIndexList[1]?.id;
    result1[2].dimensionId = detail.value[1].dimensionIndexList[1]?.dimensionId;
    result1[2].score = detail.value[1].dimensionIndexList[1]?.score;

    result1[3].id = detail.value[1].dimensionIndexList[2]?.id;
    result1[3].dimensionId = detail.value[1].dimensionIndexList[2]?.dimensionId;
    result1[3].score = detail.value[1].dimensionIndexList[2]?.score;

    result1[4].dimensionId = detail.value[2]?.dimensionId;
    result1[4].isChoose = !!detail.value[2]?.isChoose;

    result1[4].id = detail.value[2].dimensionIndexList[0]?.id;
    result1[4].dimensionId = detail.value[2].dimensionIndexList[0]?.dimensionId;
    result1[4].score = detail.value[2].dimensionIndexList[0]?.score;

    result1[5].id = detail.value[2].dimensionIndexList[1]?.id;
    result1[5].dimensionId = detail.value[2].dimensionIndexList[1]?.dimensionId;
    result1[5].score = detail.value[2].dimensionIndexList[1]?.score;

    result1[6].id = detail.value[2].dimensionIndexList[2]?.id;
    result1[6].dimensionId = detail.value[2].dimensionIndexList[2]?.dimensionId;
    result1[6].score = detail.value[2].dimensionIndexList[2]?.score;

    result1[7].dimensionId = detail.value[3]?.dimensionId;
    result1[7].isChoose = !!detail.value[3]?.isChoose;

    result1[7].id = detail.value[3].dimensionIndexList[0]?.id;
    result1[7].dimensionId = detail.value[3].dimensionIndexList[0]?.dimensionId;
    result1[7].score = detail.value[3].dimensionIndexList[0]?.score;

    result1[8].id = detail.value[3].dimensionIndexList[1]?.id;
    result1[8].dimensionId = detail.value[3].dimensionIndexList[1]?.dimensionId;
    result1[8].score = detail.value[3].dimensionIndexList[1]?.score;

    result1[9].id = detail.value[3].dimensionIndexList[2]?.id;
    result1[9].dimensionId = detail.value[3].dimensionIndexList[2]?.dimensionId;
    result1[9].score = detail.value[3].dimensionIndexList[2]?.score;

    result1[10].dimensionId = detail.value[4]?.dimensionId;
    result1[10].isChoose = !!detail.value[4]?.isChoose;

    result1[10].id = detail.value[4].dimensionIndexList[0]?.id;
    result1[10].dimensionId = detail.value[4].dimensionIndexList[0]?.dimensionId;
    result1[10].score = detail.value[4].dimensionIndexList[0]?.score;

    result1[11].id = detail.value[4].dimensionIndexList[1]?.id;
    result1[11].dimensionId = detail.value[4].dimensionIndexList[1]?.dimensionId;
    result1[11].score = detail.value[4].dimensionIndexList[1]?.score;

    result1[12].id = detail.value[4].dimensionIndexList[2]?.id;
    result1[12].dimensionId = detail.value[4].dimensionIndexList[2]?.dimensionId;
    result1[12].score = detail.value[4].dimensionIndexList[2]?.score;

    // <经济实力>下面4行
    result1[13].dimensionId = detail.value[5]?.dimensionId;
    result1[13].isChoose = !!detail.value[5]?.isChoose;

    result1[14].dimensionId = detail.value[6]?.dimensionId;
    result1[14].isChoose = !!detail.value[6]?.isChoose;
    result1[14].calculationFormula = detail.value[6]?.calculationFormula;
    result1[14].dimensionId = detail.value[6]?.dimensionIndexList[0]?.dimensionId;
    result1[14].id = detail.value[6]?.dimensionIndexList[0]?.id;
    result1[14].percentage = detail.value[6]?.dimensionIndexList[0]?.percentage;
    result1[14].score = detail.value[6]?.dimensionIndexList[0]?.score;

    result1[15].dimensionId = detail.value[7]?.dimensionId;
    result1[15].isChoose = !!detail.value[7]?.isChoose;
    result1[15].calculationFormula = detail.value[7]?.calculationFormula;
    result1[15].dimensionId = detail.value[7]?.dimensionIndexList[0]?.dimensionId;
    result1[15].id = detail.value[7]?.dimensionIndexList[0]?.id;
    result1[15].percentage = detail.value[7]?.dimensionIndexList[0]?.percentage;
    result1[15].score = detail.value[7]?.dimensionIndexList[0]?.score;

    result1[16].dimensionId = detail.value[8]?.dimensionId;
    result1[16].isChoose = !!detail.value[8]?.isChoose;
    result1[16].calculationFormula = detail.value[8]?.calculationFormula;
    result1[16].dimensionId = detail.value[8]?.dimensionIndexList[0]?.dimensionId;
    result1[16].id = detail.value[8]?.dimensionIndexList[0]?.id;
    result1[16].percentage = detail.value[8]?.dimensionIndexList[0]?.percentage;
    result1[16].score = detail.value[8]?.dimensionIndexList[0]?.score;

    result1[17].dimensionId = detail.value[9]?.dimensionId;
    result1[17].isChoose = !!detail.value[9]?.isChoose;
    result1[17].calculationFormula = detail.value[9]?.calculationFormula;
    result1[17].dimensionId = detail.value[9]?.dimensionIndexList[0]?.dimensionId;
    result1[17].id = detail.value[9]?.dimensionIndexList[0]?.id;
    result1[17].percentage = detail.value[9]?.dimensionIndexList[0]?.percentage;
    result1[17].score = detail.value[9]?.dimensionIndexList[0]?.score;

    // <偿债能力>下面7行
    result1[18].dimensionId = detail.value[10]?.dimensionId;
    result1[18].isChoose = !!detail.value[10]?.isChoose;

    result1[19].dimensionId = detail.value[11]?.dimensionId;
    result1[19].isChoose = !!detail.value[11]?.isChoose;
    result1[19].calculationFormula = detail.value[11]?.calculationFormula;
    result1[19].dimensionId = detail.value[11]?.dimensionIndexList[0]?.dimensionId;
    result1[19].id = detail.value[11]?.dimensionIndexList[0]?.id;
    result1[19].percentage = detail.value[11]?.dimensionIndexList[0]?.percentage;
    result1[19].score = detail.value[11]?.dimensionIndexList[0]?.score;

    result1[20].dimensionId = detail.value[12]?.dimensionId;
    result1[20].isChoose = !!detail.value[12]?.isChoose;
    result1[20].calculationFormula = detail.value[12]?.calculationFormula;
    result1[20].dimensionId = detail.value[12]?.dimensionIndexList[0]?.dimensionId;
    result1[20].id = detail.value[12]?.dimensionIndexList[0]?.id;
    result1[20].percentage = detail.value[12]?.dimensionIndexList[0]?.percentage;
    result1[20].score = detail.value[12]?.dimensionIndexList[0]?.score;

    result1[21].dimensionId = detail.value[13]?.dimensionId;
    result1[21].isChoose = !!detail.value[13]?.isChoose;
    result1[21].calculationFormula = detail.value[13]?.calculationFormula;
    result1[21].dimensionId = detail.value[13]?.dimensionIndexList[0]?.dimensionId;
    result1[21].id = detail.value[13]?.dimensionIndexList[0]?.id;
    result1[21].percentage = detail.value[13]?.dimensionIndexList[0]?.percentage;
    result1[21].score = detail.value[13]?.dimensionIndexList[0]?.score;

    result1[22].dimensionId = detail.value[14]?.dimensionId;
    result1[22].isChoose = !!detail.value[14]?.isChoose;
    result1[22].calculationFormula = detail.value[14]?.calculationFormula;
    result1[22].dimensionId = detail.value[14]?.dimensionIndexList[0]?.dimensionId;
    result1[22].id = detail.value[14]?.dimensionIndexList[0]?.id;
    result1[22].percentage = detail.value[14]?.dimensionIndexList[0]?.percentage;
    result1[22].score = detail.value[14]?.dimensionIndexList[0]?.score;

    result1[23].dimensionId = detail.value[15]?.dimensionId;
    result1[23].isChoose = !!detail.value[15]?.isChoose;
    result1[23].calculationFormula = detail.value[15]?.calculationFormula;
    result1[23].dimensionId = detail.value[15]?.dimensionIndexList[0]?.dimensionId;
    result1[23].id = detail.value[15]?.dimensionIndexList[0]?.id;
    result1[23].percentage = detail.value[15]?.dimensionIndexList[0]?.percentage;
    result1[23].score = detail.value[15]?.dimensionIndexList[0]?.score;

    result1[24].dimensionId = detail.value[16]?.dimensionId;
    result1[24].isChoose = detail.value[16]?.isChoose;
    result1[24].calculationFormula = detail.value[16]?.calculationFormula;
    result1[24].dimensionId = detail.value[16]?.dimensionIndexList[0]?.dimensionId;
    result1[24].id = detail.value[16]?.dimensionIndexList[0]?.id;
    result1[24].percentage = detail.value[16]?.dimensionIndexList[0]?.percentage;
    result1[24].score = detail.value[16]?.dimensionIndexList[0]?.score;

    result1[25].dimensionId = detail.value[17]?.dimensionId;
    result1[25].isChoose = !!detail.value[17]?.isChoose;
    result1[25].calculationFormula = detail.value[17]?.calculationFormula;
    result1[25].dimensionId = detail.value[17]?.dimensionIndexList[0]?.dimensionId;
    result1[25].id = detail.value[17]?.dimensionIndexList[0]?.id;
    result1[25].percentage = detail.value[17]?.dimensionIndexList[0]?.percentage;
    result1[25].score = detail.value[17]?.dimensionIndexList[0]?.score;

    // <经营能力>下面6行
    result1[26].dimensionId = detail.value[18]?.dimensionId;
    result1[26].isChoose = !!detail.value[18]?.isChoose;

    result1[27].dimensionId = detail.value[19]?.dimensionId;
    result1[27].isChoose = !!detail.value[19]?.isChoose;
    result1[27].calculationFormula = detail.value[19]?.calculationFormula;
    result1[27].dimensionId = detail.value[19]?.dimensionIndexList[0]?.dimensionId;
    result1[27].id = detail.value[19]?.dimensionIndexList[0]?.id;
    result1[27].percentage = detail.value[19]?.dimensionIndexList[0]?.percentage;
    result1[27].score = detail.value[19]?.dimensionIndexList[0]?.score;

    result1[28].dimensionId = detail.value[20]?.dimensionId;
    result1[28].isChoose = !!detail.value[20]?.isChoose;
    result1[28].calculationFormula = detail.value[20]?.calculationFormula;
    result1[28].dimensionId = detail.value[20]?.dimensionIndexList[0]?.dimensionId;
    result1[28].id = detail.value[20]?.dimensionIndexList[0]?.id;
    result1[28].percentage = detail.value[20]?.dimensionIndexList[0]?.percentage;
    result1[28].score = detail.value[20]?.dimensionIndexList[0]?.score;

    result1[29].dimensionId = detail.value[21]?.dimensionId;
    result1[29].isChoose = !!detail.value[21]?.isChoose;
    result1[29].calculationFormula = detail.value[21]?.calculationFormula;
    result1[29].dimensionId = detail.value[21]?.dimensionIndexList[0]?.dimensionId;
    result1[29].id = detail.value[21]?.dimensionIndexList[0]?.id;
    result1[29].percentage = detail.value[21]?.dimensionIndexList[0]?.percentage;
    result1[29].score = detail.value[21]?.dimensionIndexList[0]?.score;

    result1[30].dimensionId = detail.value[22]?.dimensionId;
    result1[30].isChoose = !!detail.value[22]?.isChoose;
    result1[30].calculationFormula = detail.value[22]?.calculationFormula;
    result1[30].dimensionId = detail.value[22]?.dimensionIndexList[0]?.dimensionId;
    result1[30].id = detail.value[22]?.dimensionIndexList[0]?.id;
    result1[30].percentage = detail.value[22]?.dimensionIndexList[0]?.percentage;
    result1[30].score = detail.value[22]?.dimensionIndexList[0]?.score;

    result1[31].dimensionId = detail.value[23]?.dimensionId;
    result1[31].isChoose = !!detail.value[23]?.isChoose;
    result1[31].calculationFormula = detail.value[23]?.calculationFormula;
    result1[31].dimensionId = detail.value[23]?.dimensionIndexList[0]?.dimensionId;
    result1[31].id = detail.value[23]?.dimensionIndexList[0]?.id;
    result1[31].percentage = detail.value[23]?.dimensionIndexList[0]?.percentage;
    result1[31].score = detail.value[23]?.dimensionIndexList[0]?.score;

    result1[32].dimensionId = detail.value[24]?.dimensionId;
    result1[32].isChoose = !!detail.value[24]?.isChoose;
    result1[32].calculationFormula = detail.value[24]?.calculationFormula;
    result1[32].dimensionId = detail.value[24]?.dimensionIndexList[0]?.dimensionId;
    result1[32].id = detail.value[24]?.dimensionIndexList[0]?.id;
    result1[32].percentage = detail.value[24]?.dimensionIndexList[0]?.percentage;
    result1[32].score = detail.value[24]?.dimensionIndexList[0]?.score;

    // <经营效益>下面5行
    result1[33].dimensionId = detail.value[25]?.dimensionId;
    result1[33].isChoose = !!detail.value[25]?.isChoose;

    result1[34].dimensionId = detail.value[26]?.dimensionId;
    result1[34].isChoose = !!detail.value[26]?.isChoose;
    result1[34].calculationFormula = detail.value[26]?.calculationFormula;
    result1[34].dimensionId = detail.value[26]?.dimensionIndexList[0]?.dimensionId;
    result1[34].id = detail.value[26]?.dimensionIndexList[0]?.id;
    result1[34].percentage = detail.value[26]?.dimensionIndexList[0]?.percentage;
    result1[34].score = detail.value[26]?.dimensionIndexList[0]?.score;

    result1[35].dimensionId = detail.value[27]?.dimensionId;
    result1[35].isChoose = !!detail.value[27]?.isChoose;
    result1[35].calculationFormula = detail.value[27]?.calculationFormula;
    result1[35].dimensionId = detail.value[27]?.dimensionIndexList[0]?.dimensionId;
    result1[35].id = detail.value[27]?.dimensionIndexList[0]?.id;
    result1[35].percentage = detail.value[27]?.dimensionIndexList[0]?.percentage;
    result1[35].score = detail.value[27]?.dimensionIndexList[0]?.score;

    result1[36].dimensionId = detail.value[28]?.dimensionId;
    result1[36].isChoose = !!detail.value[28]?.isChoose;
    result1[36].calculationFormula = detail.value[28]?.calculationFormula;
    result1[36].dimensionId = detail.value[28]?.dimensionIndexList[0]?.dimensionId;
    result1[36].id = detail.value[28]?.dimensionIndexList[0]?.id;
    result1[36].percentage = detail.value[28]?.dimensionIndexList[0]?.percentage;
    result1[36].score = detail.value[28]?.dimensionIndexList[0]?.score;

    result1[37].dimensionId = detail.value[29]?.dimensionId;
    result1[37].isChoose = !!detail.value[29]?.isChoose;
    result1[37].calculationFormula = detail.value[29]?.calculationFormula;
    result1[37].dimensionId = detail.value[29]?.dimensionIndexList[0]?.dimensionId;
    result1[37].id = detail.value[29]?.dimensionIndexList[0]?.id;
    result1[37].percentage = detail.value[29]?.dimensionIndexList[0]?.percentage;
    result1[37].score = detail.value[29]?.dimensionIndexList[0]?.score;

    result1[38].dimensionId = detail.value[30]?.dimensionId;
    result1[38].isChoose = !!detail.value[30]?.isChoose;
    result1[38].calculationFormula = detail.value[30]?.calculationFormula;
    result1[38].dimensionId = detail.value[30]?.dimensionIndexList[0]?.dimensionId;
    result1[38].id = detail.value[30]?.dimensionIndexList[0]?.id;
    result1[38].percentage = detail.value[30]?.dimensionIndexList[0]?.percentage;
    result1[38].score = detail.value[30]?.dimensionIndexList[0]?.score;

    // <发展前景>下面3大块
    result1[39].dimensionId = detail.value[31]?.dimensionId;
    result1[39].isChoose = !!detail.value[31]?.isChoose;

    result1[40].dimensionId = detail.value[32]?.dimensionId;
    result1[40].isChoose = !!detail.value[32]?.isChoose;

    result1[40].id = detail.value[32].dimensionIndexList[0]?.id;
    result1[40].dimensionId = detail.value[32].dimensionIndexList[0]?.dimensionId;
    result1[40].score = detail.value[32].dimensionIndexList[0]?.score;

    result1[41].id = detail.value[32].dimensionIndexList[1]?.id;
    result1[41].dimensionId = detail.value[32].dimensionIndexList[1]?.dimensionId;
    result1[41].score = detail.value[32].dimensionIndexList[1]?.score;

    result1[42].id = detail.value[32].dimensionIndexList[2]?.id;
    result1[42].dimensionId = detail.value[32].dimensionIndexList[2]?.dimensionId;
    result1[42].score = detail.value[32].dimensionIndexList[2]?.score;

    result1[43].dimensionId = detail.value[33]?.dimensionId;
    result1[43].isChoose = !!detail.value[33]?.isChoose;

    result1[43].id = detail.value[33].dimensionIndexList[0]?.id;
    result1[43].dimensionId = detail.value[33].dimensionIndexList[0]?.dimensionId;
    result1[43].score = detail.value[33].dimensionIndexList[0]?.score;

    result1[44].id = detail.value[33].dimensionIndexList[1]?.id;
    result1[44].dimensionId = detail.value[33].dimensionIndexList[1]?.dimensionId;
    result1[44].score = detail.value[33].dimensionIndexList[1]?.score;

    result1[45].id = detail.value[33].dimensionIndexList[2]?.id;
    result1[45].dimensionId = detail.value[33].dimensionIndexList[2]?.dimensionId;
    result1[45].score = detail.value[33].dimensionIndexList[2]?.score;

    result1[46].dimensionId = detail.value[34]?.dimensionId;
    result1[46].isChoose = !!detail.value[34]?.isChoose;

    result1[46].id = detail.value[34].dimensionIndexList[0]?.id;
    result1[46].dimensionId = detail.value[34].dimensionIndexList[0]?.dimensionId;
    result1[46].score = detail.value[34].dimensionIndexList[0]?.score;

    result1[47].id = detail.value[34].dimensionIndexList[1]?.id;
    result1[47].dimensionId = detail.value[34].dimensionIndexList[1]?.dimensionId;
    result1[47].score = detail.value[34].dimensionIndexList[1]?.score;

    result1[48].id = detail.value[34].dimensionIndexList[2]?.id;
    result1[48].dimensionId = detail.value[34].dimensionIndexList[2]?.dimensionId;
    result1[48].score = detail.value[34].dimensionIndexList[2]?.score;

    // <经营效益预测（未来一年）>下面2行
    result1[49].dimensionId = detail.value[35]?.dimensionId;
    result1[49].isChoose = !!detail.value[35]?.isChoose;

    result1[50].dimensionId = detail.value[36]?.dimensionId;
    result1[50].isChoose = !!detail.value[36]?.isChoose;
    result1[50].calculationFormula = detail.value[36]?.calculationFormula;
    result1[50].dimensionId = detail.value[36]?.dimensionIndexList[0]?.dimensionId;
    result1[50].id = detail.value[36]?.dimensionIndexList[0]?.id;
    result1[50].percentage = detail.value[36]?.dimensionIndexList[0]?.percentage;
    result1[50].score = detail.value[36]?.dimensionIndexList[0]?.score;

    result1[51].dimensionId = detail.value[37]?.dimensionId;
    result1[51].isChoose = !!detail.value[37]?.isChoose;
    result1[51].calculationFormula = detail.value[37]?.calculationFormula;
    result1[51].dimensionId = detail.value[37]?.dimensionIndexList[0]?.dimensionId;
    result1[51].id = detail.value[37]?.dimensionIndexList[0]?.id;
    result1[51].percentage = detail.value[37]?.dimensionIndexList[0]?.percentage;
    result1[51].score = detail.value[37]?.dimensionIndexList[0]?.score;
  }
  list1.value = result1;
  loading.close();
};
// 详情
const getDetail = async () => {
  loading = ElLoading.service();
  if (id.value) {
    try {
      const res = await findCredit(id.value);
      const info = res.scoringStrategy;
      if (info) {
        detail.value = info.dimensionOfTheRatingTableList;
        form.checkpointId = info.checkpointId;
        form.description = info.description;
        form.projectId = info.projectId;
        form.scoreCardName = info.scoreCardName;
        form.scoringObject = info.scoringObject;
      }
      getConfig();
    } finally {
    }
  } else {
    getConfig();
    detail.value = [];
    form.checkpointId = levelId;
    form.description = '';
    form.projectId = projectId;
    form.scoreCardName = '';
    form.scoringObject = 0;
  }
};
watch(
  () => route.query,
  () => {
    getDetail();
  },
  {
    immediate: true,
  },
);
const isSelect = (rule: number): boolean => {
  return rule === 589 || rule === 594;
};
interface SpanMethodProps {
  row: Record<string, any>;
  column: TableColumnCtx<Record<string, any>>;
  rowIndex: number;
  columnIndex: number;
}

const rowMerge1 = [1, 4, 7, 10, 40, 43, 46];
const rowMerge2 = [2, 3, 5, 6, 8, 9, 11, 12, 41, 42, 44, 45, 47, 48];
const rowMerge3 = [0, 13, 18, 26, 33, 39, 49];
// 表格合并
const span1 = ({ row, column, rowIndex, columnIndex }: SpanMethodProps) => {
  if (rowMerge3.includes(rowIndex)) {
    if (!columnIndex) {
      return {
        rowspan: 1,
        colspan: 5,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }

  if (!columnIndex || columnIndex === 1 || columnIndex === 2) {
    if (rowMerge1.includes(rowIndex)) {
      return {
        rowspan: 3,
        colspan: 1,
      };
    } else if (rowMerge2.includes(rowIndex)) {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};
const span = ({ row, column, rowIndex, columnIndex }: SpanMethodProps) => {
  if (!columnIndex || columnIndex === 1) {
    if (!rowIndex || rowIndex === 31) {
      return {
        rowspan: 5,
        colspan: 1,
      };
    } else if (rowIndex === 5 || rowIndex === 11 || rowIndex === 39 || rowIndex === 53) {
      return {
        rowspan: 2,
        colspan: 1,
      };
    } else if (rowIndex === 7 || rowIndex === 13 || rowIndex === 17 || rowIndex === 27 || rowIndex === 41 || rowIndex === 45 || rowIndex === 49) {
      return {
        rowspan: 4,
        colspan: 1,
      };
    } else if (rowIndex === 21 || rowIndex === 24 || rowIndex === 36) {
      return {
        rowspan: 3,
        colspan: 1,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};
// 提交
const submit = async () => {
  if (!form.scoreCardName) return ElMessage.error('请输入评分卡名称');
  loading = ElLoading.service();
  const param = [];
  const isEnterprise = form.scoringObject;
  // 企业
  if (form.scoringObject) {
    list1.value.forEach((e, i) => {
      const child = {
        id: e?.id || '',
        dimensionId: e.dimensionId ?? '',
        recordName: e.index ?? '',
        score: e.score,
        percentage: e.percentage,
      };
      const temp = {
        calculationFormula: e.calculationFormula ?? '',
        dimensionId: e.dimensionId ?? '',
        strategyId: id.value || '',
        recordName: e.name,
        isChoose: +e.isChoose,
      };
      if (e.middle) {
        temp.dimensionIndexList = [child];
        if ((e.answerId2 > 595 && e.answerId2 < 600) || (e.answerId2 > 687 && e.answerId2 < 691))
          temp.dimensionIndexList.push(
            {
              id: list1.value[i + 1].id ?? '',
              dimensionId: list1.value[i + 1].dimensionId ?? '',
              recordName: list1.value[i + 1].index ?? '',
              score: list1.value[i + 1].score,
              percentage: list1.value[i + 1].percentage,
            },
            {
              id: list1.value[i + 2].id ?? '',
              dimensionId: list1.value[i + 2].dimensionId ?? '',
              recordName: list1.value[i + 2].index ?? '',
              score: list1.value[i + 2].score,
              percentage: list1.value[i + 2].percentage,
            },
          );
        temp.recordName = e.sub;
        param.push(temp);
      } else if (e.top) {
        param.push(temp);
      }
    });
  } else {
    // 个人
    list.value.forEach((e) => {
      const child = {
        id: e.id,
        dimensionId: e.dimensionId,
        recordName: e.sub,
        score: e.score,
        recordName: e.index,
        percentage: e.percentage,
      };
      const temp = {
        dimensionId: e.dimensionId,
        strategyId: e.strategyId,
        recordName: e.name,
        isChoose: +e.isChoose,
        dimensionIndexList: [child],
      };
      if (e.span) {
        param.push(temp);
      } else {
        param[e.parent].dimensionIndexList.push(child);
      }
    });
  }

  // debugger;
  await saveCredit({
    ...form,
    dimensionOfTheRatingTableList: param,
  });
  addRecord();

  ElMessage.success('提交成功！');
  emit('getList', 1);
  id.value && getDetail();
};
// 新增判分记录
const addRecord = async () => {
  const isEnterprise = form.scoringObject;
  const preIds = `1,${Cookies.get('sand-level')},42,148,${form.scoringObject ? 513 : 512}`; // 1，关卡id，角色（这个页面是风控经理），个人/企业（512/513）
  const lcRule: Array<Record<string, any>> = [handleId(514, 190, form.scoreCardName, preIds + ',514', 3), handleId(515, 191, form.scoringObject ? 392 : 391, preIds + ',515', 1)];
  form.description && lcRule.push(handleId(516, 192, '94,' + form.description, preIds + ',516', 3));

  // 企业
  if (isEnterprise) {
    list1.value.forEach((e) => {
      // 多选框选择了才需要给
      if (e.isChoose) {
        e.middle ? lcRule.push(handleId(518, '', '', `${preIds},${e.answerId1},${e.answerId2},518`, '')) : lcRule.push(handleId(518, '', '', `${preIds},${e.answerId1},518`, ''));
      }
      // 分值（同上）
      if (isSelect(e.answerId1)) {
        e.score &&
          lcRule.push(
            handleId(e.answerId2, e?.subject?.subjectId, e?.subject?.itemList?.find((n) => n.options == e.score)?.itemId || '', `${preIds},${e.answerId1},${e.answerId2}`, 1),
          );
      } else if (e.recordChildren) {
        // 计算公式
        e.calculationFormula &&
          lcRule.push(
            handleId(
              e?.recordChildren[1]?.id,
              e?.subject?.subjectId,
              e?.subject?.itemList?.find((n) => n.options === e.calculationFormula)?.itemId || '',
              `${preIds},${e.answerId1},${e.answerId2},${e?.recordChildren[1]?.id}`,
              1,
            ),
          );

        // 分值里的下拉选择分数
        e.score &&
          lcRule.push(
            handleId(
              e?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.id,
              e?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.subject?.subjectId,
              e?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.subject?.itemList?.find((n) => n.options === e.score)?.itemId || '',
              `${preIds},${e.answerId1},${e.answerId2},${e?.recordChildren[2]?.id},${e?.recordChildren[2]?.recordChildren[0]?.id},${e?.recordChildren[2]?.recordChildren[0]?.recordChildren[0]?.id}`,
              1,
            ),
          );

        // 分值里的下拉选择百分比
        e.percentage &&
          lcRule.push(
            handleId(
              e?.recordChildren[2]?.recordChildren[0]?.recordChildren[1]?.id,
              e?.recordChildren[2]?.recordChildren[0]?.recordChildren[1]?.subject?.subjectId,
              e?.recordChildren[2]?.recordChildren[0]?.recordChildren[1]?.subject?.itemList?.find((n) => n.options === e.percentage)?.itemId || '',
              `${preIds},${e.answerId1},${e.answerId2},${e?.recordChildren[2]?.id},${e?.recordChildren[2]?.recordChildren[0]?.id},${e?.recordChildren[2]?.recordChildren[0]?.recordChildren[1]?.id}`,
              1,
            ),
          );
      }
    });
  } else {
    // 个人
    list.value.forEach((e) => {
      e.span && e.isChoose && lcRule.push(handleId(518, '', '', preIds + ',' + e.answerId1 + ',518', ''));
      lcRule.push(handleId(e.answerId2, e.subjectId, e?.itemList?.find((n) => n.options == e.score)?.itemId || '', preIds + ',' + e.answerId1 + ',' + e.answerId2, 1));
    });
  }
  await addOperation({
    checkpointId: levelId,
    parentId: preIds,
    lcJudgmentRuleReq: lcRule,
    projectId,
  });
};
</script>

<style lang="scss" scoped>
@import url(../../../styles/form.scss);
</style>
