<template>
  <!-- 风险度策略-担保方式基础系数 -->
  <div class="mb-3 text-sm font-semibold">
    单笔贷款风险度公式：
    <span class="text-[#0034ad]">单笔贷款风险度=</span>
    <span class="text-[#8f24ff]">信用等级基础系数</span>
    <span class="text-[#0034ad]"> * </span>
    <span class="text-[#5cc12b]">担保方式基础系数</span>
    <span class="text-[#0034ad]"> * </span>
    <span class="text-[#ed8269]">贷款期限基础系数</span>
  </div>
  <el-table class="c-table"
            :data="form"
            border>
    <el-table-column prop="recordName"
                     label="信用等级"
                     min-width="150">
      <template #default="{ row, $index }">
        <p :class="$index === 5 || $index === 6 || $index === 7 ? 'pl-20' : !$index || $index === 1 || $index === 10 || $index === 23 ? '' : 'pl-10'">{{ row.recordName }}</p>
      </template>
    </el-table-column>
    <el-table-column label="系数"
                     min-width="150"
                     align="center">
      <template #default="{ row, $index }">
        <p v-if="$index === 1 || $index === 4 || $index === 10 || $index === 23">--</p>
        <el-input v-else
                  placeholder="输入系数"
                  v-model="row.coefficient"></el-input>
      </template>
    </el-table-column>
  </el-table>

  <div class="flex justify-end">
    <div class="submit"
         @click="submit">完成配置</div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage, ElLoading } from 'element-plus';
import { detailRick, saveRick } from '@/api/model';
import { getProcessInformationBasedOnRoles, addOperation } from '@/api/judgment';
import { useRouter, useRoute } from 'vue-router';
import { handleId } from '@/utils/common';
import Cookies from 'js-cookie';

const router = useRouter();
const route = useRoute();
const projectId = +Cookies.get('sand-projectId');
const levelId = +Cookies.get('sand-level');
let loading = null;
const form = ref<Record<string, any>[]>([]);
const info = ref<Record<string, any>[]>([]);
// 配置项
const getConfig = async () => {
  const { process } = await getProcessInformationBasedOnRoles(703);
  const result = [];
  process[0]?.recordChildren?.forEach((e, i) => {
    const cur = {};
    let temp = {
      checkpointId: levelId,
      projectId,
      coefficient: cur?.coefficient || '',
      id: cur?.id || '',
      recordName: e.name,
      type: 2,
    };
    result.push(temp);

    e?.recordChildren?.forEach((n, j) => {
      temp = JSON.parse(JSON.stringify(temp));
      temp.recordName = n.name;
      temp.coefficient = n.coefficient || '';
      result.push(temp);

      n?.recordChildren?.forEach((m, o) => {
        temp = JSON.parse(JSON.stringify(temp));
        temp.recordName = m.name;
        temp.coefficient = m.coefficient || '';
        result.push(temp);
      });
    });
  });
  form.value = result;
  loading.close();
};
// 详情
const getDetail = async (load?: number) => {
  if (load) loading = ElLoading.service();
  try {
    const { data } = await detailRick({
      checkpointId: levelId,
      projectId,
      type: 2,
    });
    info.value = data;
    if (data.length) {
      form.value = data;
      loading.close();
    } else {
      getConfig();
    }
  } finally {
  }
};
onMounted(() => {
  getDetail(1);
});

// 提交
const submit = async () => {
  loading = ElLoading.service();
  await saveRick({ riskDegreeStrategyList: form.value });
  addRecord();
  getDetail();
  ElMessage.success('配置成功！');
};
// 新增判分记录
const addRecord = async () => {
  const preIds = `1,${Cookies.get('sand-level')},42,149,703,718`; // 1，关卡id，角色（这个页面是风控经理策略）
  const rule: Array<Record<string, any>> = [
    handleId(719, 232, form.value[0].coefficient, `${preIds},719`, 3),
    handleId(723, 232, form.value[2].coefficient, `${preIds},720,723`, 3),
    handleId(724, 232, form.value[3].coefficient, `${preIds},720,724`, 3),
    handleId(728, 232, form.value[5].coefficient, `${preIds},720,725,728`, 3),
    handleId(729, 232, form.value[6].coefficient, `${preIds},720,725,729`, 3),
    handleId(730, 232, form.value[7].coefficient, `${preIds},720,725,730`, 3),
    handleId(726, 232, form.value[8].coefficient, `${preIds},720,726`, 3),
    handleId(727, 232, form.value[9].coefficient, `${preIds},720,727`, 3),
    handleId(731, 232, form.value[11].coefficient, `${preIds},721,731`, 3),
    handleId(732, 232, form.value[12].coefficient, `${preIds},721,732`, 3),
    handleId(733, 232, form.value[13].coefficient, `${preIds},721,733`, 3),
    handleId(734, 232, form.value[14].coefficient, `${preIds},721,734`, 3),
    handleId(735, 232, form.value[15].coefficient, `${preIds},721,735`, 3),
    handleId(736, 232, form.value[16].coefficient, `${preIds},721,736`, 3),
    handleId(737, 232, form.value[17].coefficient, `${preIds},721,737`, 3),
    handleId(738, 232, form.value[18].coefficient, `${preIds},721,738`, 3),
    handleId(739, 232, form.value[19].coefficient, `${preIds},721,739`, 3),
    handleId(740, 232, form.value[20].coefficient, `${preIds},721,740`, 3),
    handleId(741, 232, form.value[21].coefficient, `${preIds},721,741`, 3),
    handleId(742, 232, form.value[22].coefficient, `${preIds},721,742`, 3),

    handleId(743, 232, form.value[24].coefficient, `${preIds},722,743`, 3),
    handleId(744, 232, form.value[25].coefficient, `${preIds},722,744`, 3),
    handleId(745, 232, form.value[26].coefficient, `${preIds},722,745`, 3),
    handleId(746, 232, form.value[27].coefficient, `${preIds},722,746`, 3),
  ];

  await addOperation({
    checkpointId: levelId,
    parentId: preIds,
    lcJudgmentRuleReq: rule,
    projectId,
  });
};
</script>

<style lang="scss" scoped>
@import url(../../../styles/form.scss);
.c-table {
  :deep(.el-input__inner) {
    @apply px-2;
  }
}
</style>
