<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 || $index === 3 ? '' : '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 || $index === 3">--</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(704);
  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: 3,
    };
    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);
    });
  });
  form.value = result;
  loading.close();
};
// 详情
const getDetail = async (load?: number) => {
  if (load) loading = ElLoading.service();
  try {
    const { data } = await detailRick({
      checkpointId: levelId,
      projectId,
      type: 3,
    });
    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,67,149,704,747`; // 1，关卡id，角色（这个页面是风控经理策略）
  const rule: Array<Record<string, any>> = [
    handleId(750, 232, form.value[1].coefficient, `${preIds},748,750`, 3),
    handleId(751, 232, form.value[2].coefficient, `${preIds},748,751`, 3),

    handleId(752, 232, form.value[4].coefficient, `${preIds},749,752`, 3),
    handleId(753, 232, form.value[5].coefficient, `${preIds},749,753`, 3),
    handleId(754, 232, form.value[6].coefficient, `${preIds},749,754`, 3),
    handleId(755, 232, form.value[7].coefficient, `${preIds},749,755`, 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>
