<template>
  <!-- 企业 -->
  <el-table class="c-table"
            :data="form"
            :max-height="height"
            border>
    <el-table-column prop="name"
                     label="指标"
                     min-width="180"
                     align="center">
    </el-table-column>
    <el-table-column label="规则"
                     min-width="250">
      <template #default="{ row,$index }">
        <div class="flex items-center">
          <template v-if="!$index">
            <span class="whitespace-nowrap">跨行业经营，行业门类数</span>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] ">
              <el-select v-model="row.num">
                <el-option v-for="item in nums"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">。</span>
          </template>
          <template v-else-if="$index === 1">
            <span class="whitespace-nowrap">名下企业</span>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.had">
                <el-option v-for="item in have"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">房产查封。</span>
          </template>
          <template v-else-if="$index === 2">
            <span class="whitespace-nowrap">名下企业</span>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.had">
                <el-option v-for="item in have"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">股权冻结。</span>
          </template>
          <template v-else-if="$index === 3">
            <span class="whitespace-nowrap">行内企业</span>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.had">
                <el-option v-for="item in have"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">不良贷款客户。</span>
          </template>
          <template v-else-if="$index === 4">
            <el-checkbox-group v-model="row.check">
              <el-checkbox v-for="item in row?.subject?.itemList"
                           :key="item"
                           :label="item.options" />
            </el-checkbox-group>
          </template>
          <template v-else-if="$index === 5">
            <div class="w-[90px]">
              <el-select v-model="row.had"
                         clearable>
                <el-option value="不是" />
                <el-option value="是" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">被列为非正常户或注销户。</span>
          </template>
          <template v-else-if="$index === 6">
            <div class="w-[90px]">
              <el-select v-model="row.had"
                         clearable>
                <el-option value="不是" />
                <el-option value="是" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">非正常户或注销户。</span>
          </template>
          <template v-else-if="$index === 7">
            <span class="whitespace-nowrap">经营年限</span>
            <div class="w-[90px] ml-2">
              <el-select v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.num">
                <el-option v-for="item in nums"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] ">
              <el-select v-model="row.unit">
                <el-option v-for="item in units"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">。</span>
          </template>
          <template v-else-if="$index === 8">
            <span class="whitespace-nowrap mr-2">企业性质涉及</span>
            <el-checkbox-group v-model="row.check">
              <el-checkbox v-for="item in row?.subject?.itemList"
                           :key="item"
                           :label="item.options" />
            </el-checkbox-group>
          </template>
          <template v-else-if="$index === 9">
            <div class="w-[90px]">
              <el-select v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.num">
                <el-option v-for="item in nums"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px]">
              <el-select v-model="row.unit">
                <el-option v-for="item in units"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">注册资本减少。</span>
          </template>
          <template v-else-if="$index === 10">
            <div class="w-[90px]">
              <el-select v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.num">
                <el-option v-for="item in nums"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px]">
              <el-select v-model="row.unit">
                <el-option v-for="item in units"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">内法定代表人变更。</span>
          </template>
          <template v-else-if="$index === 11">
            <div class="w-[90px]">
              <el-select v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.num">
                <el-option v-for="item in nums"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px]">
              <el-select v-model="row.unit">
                <el-option v-for="item in units"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">内大股东（股权占比>=50%）变更。</span>
          </template>
          <template v-else-if="$index === 12">
            命中禁入行业名单中企业拒绝规则
          </template>
          <template v-else-if="$index === 13">
            <span class="whitespace-nowrap">申请人在该企业的股权占比</span>
            <div class="w-[90px] mx-2">
              <el-select v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <div class="w-[90px] ">
              <el-select v-model="row.num">
                <el-option v-for="item in nums"
                           :key="item"
                           :label="item"
                           :value="item" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">%。</span>
          </template>
          <template v-else-if="$index === 14">
            <div class="w-[90px] mx-2">
              <el-select v-model="row.had"
                         clearable>
                <el-option value="不在" />
                <el-option value="在" />
              </el-select>
            </div>
            <span class="whitespace-nowrap ml-2">工商局或农专经营异常目录。</span>
          </template>
        </div>
      </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, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { accessStrategyEnterpriseBlacklistFind, accessStrategyEnterpriseBlacklistSave } from '@/api/model';
import { getProcessInformationBasedOnRoles, addOperation } from '@/api/judgment';
import { useRouter, useRoute } from 'vue-router';
import { handleId, getNum, getSymbol, getChinese } 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 - 270;
const symbols: Array<string> = ['>=', '<', '>', '==', '<='];
const nums: Array<number> = [1, 3, 5, 6, 16, 33, 40];
const units: Array<string> = ['年', '月', '天'];
const have: Array<string> = ['无', '有'];
// 配置项
const getConfig = async () => {
  const { process } = await getProcessInformationBasedOnRoles(154);
  const result = [];
  process.map((e, i) => {
    const cur = info.value.find((n) => n.stRecordId === e.id);
    let num = getNum(cur?.rule);
    let symbol = getSymbol(cur?.rule);
    let had = getChinese(cur?.rule);
    let temp = {
      name: e.name,
      subject: e.subject,
      stRecordId: e.id,
      symbol: symbol || '>=',
      unit: had || '年',
      num: num || 1,
      had: (((i > 0 && i < 4) || i === 5 || i === 6 || i === 14) && cur?.rule) || '',
      check: (i === 4 || i === 8) && cur ? cur?.rule.split(',') : [],
    };

    result.push(temp);
  });
  form.value = result;
};
// 详情
const getDetail = async () => {
  try {
    const { data } = await accessStrategyEnterpriseBlacklistFind(levelId, projectId);
    info.value = data;
    getConfig();
  } finally {
  }
};

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

// 提交
const submit = async () => {
  let param = [];
  form.value.map((e, i) => {
    const temp = {
      id: info.value.find((n) => n.stRecordId === e.stRecordId)?.id ?? '',
      stRecordId: e.stRecordId,
      checkpointId: levelId,
      projectId,
    };
    let rule;
    if (!i || i === 13) {
      rule = e.symbol + e.num;
    } else if ((i > 0 && i < 4) || i === 5 || i === 6 || i === 14) {
      rule = e.had;
    } else if (i === 4 || i === 8) {
      rule = e.check.join();
    } else if (i === 7 || i === 9 || i === 10 || i === 11) {
      rule = e.symbol + e.num + e.unit;
    }
    temp.rule = rule;
    param.push(temp);
  });
  await accessStrategyEnterpriseBlacklistSave({ enterpriseBlacklistList: param });
  addRecord(param);
  getDetail();
  ElMessage.success('提交成功！');
};
// 新增判分记录
const addRecord = async (data: Record<string, any>) => {
  const preIds = `1,${Cookies.get('sand-level')},42,67,147,154`; // 1，关卡id，角色（这个页面是风控经理策略）
  const rule: Array<Record<string, any>> = [
    handleId(369, 168, data[0].rule, preIds + ',369', 5),
    handleId(382, 179, data[13].rule, preIds + ',382', 5),
    handleId(376, 174, data[7].rule, preIds + ',376', 5),
    handleId(378, 176, data[9].rule, preIds + ',378', 5),
    handleId(379, 177, data[10].rule, preIds + ',379', 5),
    handleId(380, 178, data[11].rule, preIds + ',380', 5),
  ];
  form.value.map((e, i) => {
    if (((i > 0 && i < 4) || i === 5 || i === 6 || i === 14) && e.had) {
      rule.push(handleId(e.stRecordId, e?.subject?.subjectId, e?.subject?.itemList?.find((n) => n.options === e.had)?.itemId, preIds + ',' + e.stRecordId, 1));
    } else if ((i === 4 || i === 8) && e.check.length) {
      const ids = [];
      e.check.map((n) => {
        const cur = e.subject?.itemList?.find((e) => e.options === n);
        cur && ids.push(cur.itemId);
      });
      ids.length && rule.push(handleId(e.stRecordId, e?.subject?.subjectId, ids.join(), preIds + ',' + 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>
