<template>
  <!-- 五级分类 -->
  <el-table class="c-table"
            :data="form"
            :span-method="span"
            :cell-style="{background:'#fff'}"
            border>
    <el-table-column prop="recordName"
                     label="产品类别"
                     min-width="150"
                     align="center">
    </el-table-column>
    <el-table-column label="逾期时间"
                     align="center">
      <el-table-column label="未逾期"
                       align="center">
        <template #default="{ row }">
          <el-select clearable
                     v-model="row.notOverdue">
            <el-option v-for="item in row.recordChildren[0].subject.itemList"
                       :key="item"
                       :label="item.options"
                       :value="item.itemId" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="1~30天"
                       align="center">
        <template #default="{ row }">
          <el-select clearable
                     v-model="row.thirtyDays">
            <el-option v-for="item in row.recordChildren[0].subject.itemList"
                       :key="item"
                       :label="item.options"
                       :value="item.itemId" />
          </el-select>
        </template></el-table-column>
      <el-table-column label="31~90天"
                       align="center">
        <template #default="{ row }">
          <el-select clearable
                     v-model="row.ninetyDays">
            <el-option v-for="item in row.recordChildren[0].subject.itemList"
                       :key="item"
                       :label="item.options"
                       :value="item.itemId" />
          </el-select>
        </template></el-table-column>
      <el-table-column label="91~180天"
                       align="center">
        <template #default="{ row }">
          <el-select clearable
                     v-model="row.oneHundredAndEightyDays">
            <el-option v-for="item in row.recordChildren[0].subject.itemList"
                       :key="item"
                       :label="item.options"
                       :value="item.itemId" />
          </el-select>
        </template></el-table-column>
      <el-table-column label="181~360天"
                       align="center">
        <template #default="{ row }">
          <el-select clearable
                     v-model="row.threeHundredAndSixtyDays">
            <el-option v-for="item in row.recordChildren[0].subject.itemList"
                       :key="item"
                       :label="item.options"
                       :value="item.itemId" />
          </el-select>
        </template></el-table-column>
      <el-table-column label="360天以上"
                       align="center">
        <template #default="{ row }">
          <el-select clearable
                     v-model="row.threeHundredAndSixtyDaysAbove">
            <el-option v-for="item in row.recordChildren[0].subject.itemList"
                       :key="item"
                       :label="item.options"
                       :value="item.itemId" />
          </el-select>
        </template></el-table-column>
    </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 } from 'element-plus';
import { fiveLevelClassificationDetails, fiveLevelClassificationSave } from '@/api/model';
import { getProcessInformationBasedOnRoles, addOperation } from '@/api/judgment';
import { handleId, getIds } from '@/utils/common';
import Cookies from 'js-cookie';

const projectId = +Cookies.get('sand-projectId');
const levelId = +Cookies.get('sand-level');
const form = ref<Record<string, any>[]>([]);
const info = ref<Record<string, any>[]>([]);
// 配置项
const getConfig = async () => {
  const { process } = await getProcessInformationBasedOnRoles(1029);
  const result = [];
  process.map((e, i) => {
    const cur = info.value.length ? info.value[i] : {};
    result.push({
      checkpointId: levelId,
      projectId,
      recordName: e.name,
      recordChildren: e.recordChildren,
      ninetyDays: +cur.ninetyDays || '',
      notOverdue: +cur.notOverdue || '',
      oneHundredAndEightyDays: +cur.oneHundredAndEightyDays || '',
      thirtyDays: +cur.thirtyDays || '',
      threeHundredAndSixtyDays: +cur.threeHundredAndSixtyDays || '',
      threeHundredAndSixtyDaysAbove: +cur.threeHundredAndSixtyDaysAbove || '',
      id: cur.id || '',
      stRecordId: e.id,
    });
  });
  form.value = result;
};
// 详情
const getDetail = async () => {
  try {
    const { data } = await fiveLevelClassificationDetails(levelId, projectId);
    info.value = data;
    getConfig();
  } finally {
  }
};

const fieldKeys = ['notOverdue', 'thirtyDays', 'ninetyDays', 'oneHundredAndEightyDays', 'threeHundredAndSixtyDays', 'threeHundredAndSixtyDaysAbove'];
// 新增判分记录
const addRecord = async (data: Record<string, any>) => {
  const preIds = `1,${levelId},42,69,1029`; // 1，关卡id，角色（这个页面是风控经理策略）
  const rule = [];

  data.map((e) => {
    e.recordChildren.forEach((n, i) => {
      rule.push(handleId(n.id, n.subjectId, e[fieldKeys[i]], `${preIds},${e.stRecordId},${n.id}`, 1));
    });
  });

  await addOperation({
    ...getIds(),
    parentId: preIds,
    lcJudgmentRuleReq: rule,
  });
};
// 提交
const submit = async () => {
  let param = JSON.parse(JSON.stringify(form.value));

  const recordParam = JSON.parse(JSON.stringify(param));
  param.map((e) => {
    delete e.recordChildren;
  });
  await fiveLevelClassificationSave({ fiveLevelClassificationList: param });
  addRecord(recordParam);
  getDetail();
  ElMessage.success('提交成功！');
};
onMounted(() => {
  getDetail();
});
</script>

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