<template>
  <el-table class="c-table"
            :data="form"
            :max-height="height"
            :span-method="span"
            border>
    <el-table-column prop="name"
                     label="指标"
                     min-width="150"
                     align="center">
    </el-table-column>
    <el-table-column label="规则"
                     min-width="250"
                     align="center">
      <template #default="{ row }">
        <!-- 大病报销 || 贫困户 -->
        <div v-if="row.stRecordId == 161 || row.stRecordId == 164"
             class="flex items-center">
          <span class="whitespace-nowrap">{{ row?.recordChildren[row.span ? 1 : 0]?.name }}</span>
          <div class="w-[90px] ">
            <el-select class="mx-2"
                       v-model="row.symbol">
              <el-option v-for="item in symbols"
                         :key="item"
                         :label="item.name"
                         :value="item.name" />
            </el-select>
          </div>
          <el-input class="w-[80px]"
                    v-model="row.num"></el-input>
          <span class="ml-2 whitespace-nowrap">万元</span>
        </div>
        <!-- 大龄未婚 -->
        <div v-else-if="row.stRecordId == 167"
             class="flex items-center">
          <template v-if="row.span">
            <span class="whitespace-nowrap">且近一年</span>
            <div class="w-[90px] ">
              <el-select class="mx-2"
                         v-model="row.had">
                <el-option label="有"
                           value="1" />
                <el-option label="无"
                           value="2" />
              </el-select>
            </div>
            <span class="ml-2 whitespace-nowrap">缴纳过社保或公积金</span>
          </template>
          <template v-else>
            <span class="whitespace-nowrap">未婚，且年龄</span>
            <div class="w-[90px] ">
              <el-select class="mx-2"
                         v-model="row.symbol">
                <el-option v-for="item in symbols"
                           :key="item"
                           :label="item.name"
                           :value="item.name" />
              </el-select>
            </div>
            <el-input class="w-[80px]"
                      v-model="row.num"></el-input>
            <span class="ml-2 whitespace-nowrap">,</span>
            <el-select class="w-[80px] ml-2"
                       v-model="row.had">
              <el-option label="有"
                         value="1" />
              <el-option label="无"
                         value="2" />
            </el-select>
            <span class="ml-2 whitespace-nowrap">固定资产</span>
          </template>
        </div>
        <span v-else>{{ '命中' + row.name }}</span>
      </template>
    </el-table-column>
    <el-table-column label="本人命中进黑名单"
                     width="140"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.personalHitBlacklist"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="配偶命中拒入"
                     width="140"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.mateHitRejected"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="父母/子女命中拒入"
                     width="150"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.parentsHitRejected"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="其它家庭成员命中拒入"
                     width="170"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.otherFamilyMembersHitRejected"></el-checkbox>
      </template>
    </el-table-column>
    <el-table-column label="企业大股东命中拒入"
                     width="160"
                     align="center">
      <template #default="{ row }">
        <el-checkbox v-model="row.corporateMajorityHitRejected"></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, reactive, computed, watch, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { accessStrategyGovernmentBlacklistFind, accessStrategyGovernmentBlacklist } from '@/api/bank';
import { getProcessInformationBasedOnRoles, addOperation } from '@/api/judgment';
import type { TableColumnCtx } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
import { handleId } from '@/utils/common';

const emit = defineEmits(['getList']);

const router = useRouter();
const route = useRoute();
const id = computed(() => +route.query.id);
const form = ref<Record<string, any>[]>([]);
const height = window.innerHeight - 270;
const symbols = <Record<string, any>[]>[
  {
    name: '>=',
  },
  {
    name: '<',
  },
  {
    name: '>',
  },
  {
    name: '==',
  },
  {
    name: '<=',
  },
];
// 配置项
const getConfig = async () => {
  const { process } = await getProcessInformationBasedOnRoles(150);
  const result = [];
  process.map((e) => {
    let temp = {
      checkpointId: 1,
      projectId: 1,
      name: e.name,
      recordChildren: e.recordChildren,
      isRule: isRule(e.id) ? 1 : 0,
      corporateMajorityHitRejected: false,
      mateHitRejected: false,
      otherFamilyMembersHitRejected: false,
      parentsHitRejected: false,
      personalHitBlacklist: false,
      symbol: '>=',
      had: '',
      num: '',
      ruleOne: '',
      ruleThree: '',
      ruleTwo: '',
      stRecordId: e.id,
    };
    result.push(temp);
    if (isRule(e.id)) {
      temp = JSON.parse(JSON.stringify(temp));
      temp.span = 1;
      result.push(temp);
    }
  });
  form.value = result;
  console.log('🚀 ~ file: CardList.vue:68 ~ getList ~ list.value:', form.value);
};
// 详情
const getDetail = async () => {
  if (id.value) {
    try {
      const { data } = await accessStrategyGovernmentBlacklistFind(1, 1);
      // info.value = data;
      getConfig();
    } finally {
    }
  }
};
watch(
  () => route.query,
  () => {
    // getDetail();
    getConfig();
  },
  {
    immediate: true,
  },
);

// 判断是否是3个有输入框的指标
const isRule = (rule: number): boolean => {
  return rule === 161 || rule === 164 || rule === 167;
};
interface SpanMethodProps {
  row: Record<string, any>;
  column: TableColumnCtx<Record<string, any>>;
  rowIndex: number;
  columnIndex: number;
}
// 表格合并
const span = ({ row, column, rowIndex, columnIndex }: SpanMethodProps) => {
  if (columnIndex === 0 || columnIndex === 2 || columnIndex === 3 || columnIndex === 4 || columnIndex === 5 || columnIndex === 6) {
    if (rowIndex === 4 || rowIndex === 6 || rowIndex === 8) {
      return {
        rowspan: 2,
        colspan: 1,
      };
    } else if (rowIndex === 5 || rowIndex === 7 || rowIndex === 9) {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};
onMounted(() => {});
// 提交
const submit = async () => {
  const param = JSON.parse(JSON.stringify(form.value));
  param.map((e) => {
    e.corporateMajorityHitRejected = +e.corporateMajorityHitRejected;
    e.mateHitRejected = +e.mateHitRejected;
    e.otherFamilyMembersHitRejected = +e.otherFamilyMembersHitRejected;
    e.parentsHitRejected = +e.parentsHitRejected;
    e.personalHitBlacklist = +e.personalHitBlacklist;
    if (e.stRecordId == 161 || e.stRecordId == 164) {
      if (e.span) {
      } else {
        e.ruleOne = e.symbol + e.num;
      }
    }
  });
  const { message } = await accessStrategyGovernmentBlacklist(param);
  // addRecord(param, message);

  ElMessage.success('提交成功！');
  emit('getList', 1);
};
// 新增判分记录
const addRecord = async (data: Record<string, any>, newId: number) => {
  const isEnterprise = info.value.productType === 1;
  const preIds = `1,2,42,${newId}`; // 1，关卡id，角色（这个页面是风控经理策略）
  const lcRule = <Record<string, any>[]>[];

  // 企业
  if (isEnterprise) {
    lcRule.push(
      handleId(120, 44, data.accountMaterials, preIds + ',112,120', 1),
      handleId(121, 45, 162, preIds + ',112,121', 1),
      handleId(122, 46, data.loanApplicationMethod, preIds + ',113,122', 1),
      handleId(123, 47, data.borrowerMaterial, preIds + ',113,123', 1),
      handleId(124, 48, data.collateral, preIds + ',113,124', 1),
      handleId(125, 49, data.businessMaterials, preIds + ',113,125', 1),
      handleId(126, 50, data.supplementaryMaterials, preIds + ',113,126', 1),

      handleId(127, 51, data.runBatchObject, preIds + ',114,127', 1),
      handleId(128, 52, data.accessStrategy, preIds + ',114,128', 1),
    );
    data.personalCreditScoringStrategiesCheck && lcRule.push(handleId(129, 53, '240,' + data.personalCreditScoringStrategies, preIds + ',114,129', 1));
    data.corporateCreditScoringStrategiesCheck && lcRule.push(handleId(129, 53, '241,' + data.corporateCreditScoringStrategies, preIds + ',114,129', 1));

    lcRule.push(handleId(130, 54, data.riskDegreeStrategy, preIds + ',114,130', 1));
    data.interestRatePricingModelCheck && lcRule.push(handleId(131, 55, '247,' + data.interestRatePricingModel, preIds + ',114,131', 1));
    data.individualInterestRateModel && lcRule.push(handleId(131, 55, data.individualInterestRateModel, preIds + ',114,131', 1));

    lcRule.push(
      handleId(132, 56, data.dueDiligenceMode, preIds + ',115,132', 1),
      handleId(133, 57, data.dueDiligenceContent, preIds + ',115,133', 1),
      handleId(134, 58, data.reviewContent, preIds + ',116,134', 1),
      handleId(135, 59, data.reviewSignature, preIds + ',116,135', 1),
      handleId(136, 60, data.reviewApproveContent, preIds + ',117,136', 1),
      handleId(137, 61, data.approvalSignature, preIds + ',117,137', 1),
      handleId(138, 62, data.contractMaterials, preIds + ',118,138', 1),
    );
    data.loanContract && lcRule.push(handleId(139, 63, data.loanContract, preIds + ',118,139', 1));
    data.mortgageContract && lcRule.push(handleId(139, 63, data.mortgageContract, preIds + ',118,139', 1));
    data.pledgeContract && lcRule.push(handleId(139, 63, data.pledgeContract, preIds + ',118,139', 1));
    data.guaranteeContract && lcRule.push(handleId(139, 63, data.guaranteeContract, preIds + ',118,139', 1));
    lcRule.push(handleId(140, 64, data.selectionStrategy, preIds + ',119,140', 1));
  } else {
    lcRule.push(
      handleId(75, 20, data.accountMaterials, preIds + ',72,75', 1),
      handleId(76, 21, 46, preIds + ',72,76', 1),
      handleId(77, 22, data.loanApplicationMethod, preIds + ',73,77', 1),
      handleId(78, 23, data.borrowerMaterial, preIds + ',73,78', 1),
      handleId(79, 24, data.mateMaterial, preIds + ',73,79', 1),
      handleId(80, 25, data.businessMaterials, preIds + ',73,80', 1),
      handleId(81, 26, data.supplementaryMaterials, preIds + ',73,81', 1),
      handleId(82, 27, data.runBatchObject, preIds + ',74,82', 1),
      handleId(83, 28, data.accessStrategy, preIds + ',74,83', 1),
    );
    data.personalCreditScoringStrategiesCheck && lcRule.push(handleId(84, 29, '94,' + data.personalCreditScoringStrategies, preIds + ',74,84', 1));
    data.corporateCreditScoringStrategiesCheck && lcRule.push(handleId(84, 29, '95,' + data.corporateCreditScoringStrategies, preIds + ',74,84', 1));

    lcRule.push(handleId(85, 30, data.riskDegreeStrategy, preIds + ',74,85', 1));
    data.interestRatePricingModelCheck && lcRule.push(handleId(86, 31, '102,' + data.interestRatePricingModel, preIds + ',74,86', 1));
    data.individualInterestRateModel && lcRule.push(handleId(86, 31, data.individualInterestRateModel, preIds + ',74,86', 1));

    lcRule.push(
      handleId(88, 32, data.dueDiligenceMode, preIds + ',87,88', 1),
      handleId(89, 33, data.dueDiligenceContent, preIds + ',87,89', 1),
      handleId(91, 34, data.reviewContent, preIds + ',90,91', 1),
      handleId(92, 35, data.reviewSignature, preIds + ',90,92', 1),
      handleId(94, 36, data.reviewApproveContent, preIds + ',93,94', 1),
      handleId(95, 37, data.approvalSignature, preIds + ',93,95', 1),
      handleId(97, 38, data.contractMaterials, preIds + ',96,97', 1),
    );
    data.loanContract && lcRule.push(handleId(98, 39, data.loanContract, preIds + ',96,98', 1));
    data.mortgageContract && lcRule.push(handleId(98, 39, data.mortgageContract, preIds + ',96,98', 1));
    data.pledgeContract && lcRule.push(handleId(98, 39, data.pledgeContract, preIds + ',96,98', 1));
    data.guaranteeContract && lcRule.push(handleId(98, 39, data.guaranteeContract, preIds + ',96,98', 1));
    lcRule.push(handleId(99, 40, data.selectionStrategy, preIds + ',99', 1));
  }

  await addOperation({
    parentId: preIds,
    lcJudgmentRuleReq: lcRule,
    projectId: 1,
  });
};
</script>

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