<template>
  <!-- 负面行业策略 -->
  <el-table class="c-table"
            :data="form"
            :max-height="height"
            :cell-style="{background:'#fff'}"
            :span-method="span"
            border>
    <el-table-column label="序号"
                     width="80"
                     align="center">
      <template #default="{ row, $index }">
        <p v-if="isRule($index)"
           class="text-left font-semibold text-base text-[#333]">{{ row.name }}</p>
        <span v-else>{{ row.index }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="name"
                     label="禁入行业"
                     min-width="150"
                     align="center">
    </el-table-column>
    <el-table-column label="本人工作单位"
                     min-width="150"
                     align="center">
      <template #default="{ row, $index }">
        <el-checkbox v-model="row.myselfWorkplace"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="配偶工作单位"
                     min-width="150"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.spouseWorkplace"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="本人名下企业"
                     min-width="150"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.myselfEnterprise"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="配偶名下企业"
                     min-width="150"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.spouseEnterprise"></el-checkbox>
      </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, computed, watch, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { accessStrategyNegativeIndustryStrategyFind, accessStrategyNegativeIndustryStrategySave } from '@/api/model';
import { getProcessInformationBasedOnRoles, addOperation } from '@/api/judgment';
import type { TableColumnCtx } from 'element-plus';
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');
const form = ref<Record<string, any>[]>([]);
const info = ref<Record<string, any>[]>([]);
const height = window.innerHeight - 230;
// 配置项
const getConfig = async () => {
  const { process } = await getProcessInformationBasedOnRoles(156);
  const result = [];
  let index = 0;
  process.map((e) => {
    let temp = {
      checkpointId: levelId,
      projectId,
      name: e.name,
    };
    result.push(temp);
    e?.recordChildren.forEach((n, i) => {
      index++;
      temp = JSON.parse(JSON.stringify(temp));
      const cur = info.value.find((j) => j.stRecordId === n.id);
      temp.stRecordId = n.id;
      temp.subjectId = n.subjectId;
      temp.parentId = e.id;
      temp.name = n.name;
      temp.index = index;
      temp.myselfEnterprise = !!cur?.myselfEnterprise;
      temp.myselfWorkplace = !!cur?.myselfWorkplace;
      temp.spouseEnterprise = !!cur?.spouseEnterprise;
      temp.spouseWorkplace = !!cur?.spouseWorkplace;
      result.push(temp);
    });
  });
  form.value = result;
};
// 详情
const getDetail = async () => {
  try {
    const { data } = await accessStrategyNegativeIndustryStrategyFind(levelId, projectId);
    info.value = data;
    getConfig();
  } finally {
  }
};

watch(
  () => route.query,
  () => {
    getDetail();
  },
  {
    immediate: true,
  },
);

const isRule = (rule: number): boolean => {
  return !rule || rule === 43 || rule === 63 || rule === 89;
};
interface SpanMethodProps {
  row: Record<string, any>;
  column: TableColumnCtx<Record<string, any>>;
  rowIndex: number;
  columnIndex: number;
}
// 表格合并
const span = ({ row, column, rowIndex, columnIndex }: SpanMethodProps) => {
  if (isRule(rowIndex)) {
    if (!columnIndex) {
      return {
        rowspan: 1,
        colspan: 6,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};
// 提交
const submit = async () => {
  let param = [];
  form.value.map((e, i) => {
    if (e.index) {
      param.push({
        checkpointId: levelId,
        projectId,
        stRecordId: e.stRecordId,
        id: info.value.find((j) => j.stRecordId === e.stRecordId)?.id ?? '',
        myselfEnterprise: e.myselfEnterprise ? 1 : 0,
        myselfWorkplace: e.myselfWorkplace ? 1 : 0,
        spouseEnterprise: e.spouseEnterprise ? 1 : 0,
        spouseWorkplace: e.spouseWorkplace ? 1 : 0,
      });
    }
  });
  await accessStrategyNegativeIndustryStrategySave({ negativeIndustryStrategyList: param });
  addRecord(param);
  getDetail();
  ElMessage.success('提交成功!');
};
// 新增判分记录
const addRecord = async (data: Record<string, any>) => {
  const preIds = `1,${Cookies.get('sand-level')},42,67,147,156`; // 1,关卡id,角色(这个页面是风控经理策略)
  const rule: Array<Record<string, any>> = [];

  form.value.map((e) => {
    if (e.index) {
      const temp = [];
      e.myselfWorkplace && temp.push(383);
      e.spouseWorkplace && temp.push(384);
      e.myselfEnterprise && temp.push(385);
      e.spouseEnterprise && temp.push(386);
      temp.length && rule.push(handleId(e.stRecordId, e.subjectId, temp.join(), preIds + ',' + e.parentId + ',' + e.stRecordId, 1));
    }
  });

  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>