<template>
  <BasicDrawer v-bind="$attrs" title="详情" @register="registerDrawer" :width="pwidth">
    <!-- <template #title>
          <span v-html="pageTitle" style="text-align:center;font-weight: 600;font-size:17px;"> </span>
        </template> -->

    <a-card :bordered="false" style="width: 98%">
      <template #title>
        <a v-html="pageTitle" style="text-align: center; font-weight: 500; font-size: 14px" @click="toItemDetail"> </a>
        <span style="font: size 12px; color: #bebebe">（点击进入标准库管理）</span>
      </template>
      <a-list item-layout="vertical" :data-source="listData">
        <template #renderItem="{ item }">
          <a-list-item>
            <a-list-item-meta>
              <template #title>
                <span style="font-weight: bold; font-size: 15px; margin-bottom: 5px">{{ item.label }}</span>
              </template>
              <template #description>
                <div style="width: 100%; font-weight: 400; line-height: 25px; font-size: medium; color: black">
                  <a-button v-if="item.fpath" ghost type="link" @click="showFileContent(item)" style="margin-left: 0px; padding-left: 0px">
                    <span v-html="item.val"> </span>
                  </a-button>
                  <span v-else v-html="item.val"> </span>
                </div>
              </template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
      <a-divider v-if="lableNameList" />
      <Tag v-for="(lableName, i) in lableNameList" :key="i" color="warning">
        <span style="font-size: 14px; font-weight: 400" v-html="lableName"></span>
      </Tag>
      <a-button type="link" @click="closeDrawer" style="text-align: center; float: right">关闭</a-button>
    </a-card>
  </BasicDrawer>
  <ShowFileContentModal @register="registerShowFileContentModal" />
</template>
<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { useRouter } from 'vue-router';
  import { Tag } from 'ant-design-vue';
  import { useAppInject } from '/@/hooks/web/useAppInject';
  import { defHttp } from '/@/utils/http/axios';
  import { useModal } from '/@/components/Modal';
  import { BasicDrawer, useDrawerInner } from '/src/components/Drawer';
  import ShowFileContentModal from './ShowFileContentModal.vue';
  import { replaceAll } from '/@/utils';

  const pwidth = ref();
  const pageTitle = ref('');
  const lableNameList = ref([]);
  const listData = ref<{ label: string; val: string; fileId?: string; fpath?: string; fileName?: string }[]>([]);
  let strApi: any = '';
  let sctp = 0;
  const paramData = ref({});
  let queryParam = '';
  let id = '';
  const [registerShowFileContentModal, { openModal: openFileContentModel }] = useModal();

  const { getIsMobile } = useAppInject();
  let fileName = '';
  let filePath = '';
  let fileId = '';
  const router = useRouter();
  const [registerDrawer, { closeDrawer }] = useDrawerInner(async (param) => {
    queryParam = param.srh;
    let tag = param.data.tag;
    fileName = param.data.fileName;
    filePath = param.data.filePath;
    fileId = param.data.fileId;
    //let docNo = param.docNo;
    let bizNo = param.data.bizNo;
    pageTitle.value = param.data.name;
    let lableNames = param.data.lableNames;
    paramData.value = param.data;

    if (unref(getIsMobile)) {
      pwidth.value = '100%';
    } else {
      pwidth.value = '50%';
    }

    if (lableNames) {
      lableNameList.value = lableNames.split(',');
    }

    const data = param.data;
    id = data.id;
    sctp = data.sctype * 1;

    if (sctp == 1) {
      strApi = '/audit/auditComplianeItem/queryById';
    } else if (sctp == 2) {
      strApi = '/audit/auditInnerCtrl/queryById';
    } else if (sctp == 3) {
      strApi = '/audit/auditInnerOrder/queryById';
    } else if (sctp == 4) {
      strApi = '/audit/auditProblem/queryById';
    } else if (sctp == 5) {
      strApi = '/audit/auditCheckMethod/queryById';
    } else if (sctp == 6) {
      strApi = '/audit/auditRiskEvent/queryById';
    } else if (sctp == 7) {
      strApi = '/audit/auditBestPractice/queryById';
    } else if (sctp == 8) {
      strApi = '/audit/auditRiskScenario/queryById';
    } else if (sctp == 9) {
      strApi = '/audit/auditAct/queryById';
    } else if (sctp == 10) {
      strApi = '/compliane.item/stComplianePunish/queryById';
    }
    if (sctp == 10) {
      getPunishInfo(id);
    } else {
      let tParma = { id: id };
      getOne(tParma, handleSuccess);
    }
  });

  const getOne = (params, handleSuccess) => {
    listData.value = [];
    lableNameList.value = [];
    return defHttp.get({ url: strApi, params }, { joinParamsToUrl: true }).then((res) => {
      handleSuccess(res);
    });
  };

  function handleSuccess(data) {
    listData.value = [];
    if (sctp == 1) {
      listData.value.push({ label: '条目名称', val: data.item });
      let strTmp = handleHilight(data.content, queryParam);
      listData.value.push({ label: '描述', val: strTmp });
      listData.value.push({ label: '参考文件', val: fileName, fileId: fileId, fpath: filePath });
    } else if (sctp == 2) {
      listData.value.push({ label: '策略名称', val: data.strategy });
      let strTmp = handleHilight(data.claim, queryParam);
      listData.value.push({ label: '内控要求', val: strTmp });
      listData.value.push({ label: '参考文件', val: fileName, fileId: fileId, fpath: filePath });
    } else if (sctp == 3) {
      listData.value.push({ label: '执行规则', val: data.execRule });
      let strTmp = handleHilight(data.ctrlDesc, queryParam);
      listData.value.push({ label: '内控要求', val: strTmp });
      listData.value.push({ label: '参考文件', val: fileName, fileId: fileId, fpath: filePath });
    } else if (sctp == 4) {
      listData.value.push({ label: '问题项目', val: data.pbItem });
      let strTmp = handleHilight(data.pbExample, queryParam);
      listData.value.push({ label: '问题示例', val: strTmp });
      listData.value.push({ label: '问题建议', val: data.pbAdvise });
      listData.value.push({ label: '问题分析', val: data.pbAnalysis });
      listData.value.push({ label: '问题性质', val: data.pbNature });
      listData.value.push({ label: '参考文件', val: fileName, fileId: fileId, fpath: filePath });
    } else if (sctp == 5) {
      listData.value.push({ label: '检查方法', val: data.content });
      let strTmp = handleHilight(data.des, queryParam);
      listData.value.push({ label: '说明', val: strTmp });
      listData.value.push({ label: '参考文件', val: fileName, fileId: fileId, fpath: filePath });
    } else if (sctp == 6) {
      listData.value.push({ label: '风险事件', val: data.riskEventName });
      let strTmp = handleHilight(data.riskEventDesc, queryParam);
      listData.value.push({ label: '事件描述', val: strTmp });
    } else if (sctp == 7) {
      listData.value.push({ label: '行业实践', val: data.content });
      let strTmp = handleHilight(data.exeRule, queryParam);
      listData.value.push({ label: '执行规则', val: strTmp });
      listData.value.push({ label: '参考文件', val: fileName, fileId: fileId, fpath: filePath });
    } else if (sctp == 8) {
      listData.value.push({ label: '分类名称', val: data.scenarioName });
      let strTmp = handleHilight(data.scenarioDesc, queryParam);
      listData.value.push({ label: '分类描述', val: strTmp });
    } else if (sctp == 9) {
      listData.value.push({ label: '控制项名称', val: data.caName });
      let strTmp = handleHilight(data.caDesc, queryParam);
      listData.value.push({ label: '控制项说明', val: strTmp });
    }
  }

  const getPunishInfo = (id) => {
    listData.value = [];
    const params = { id: id };
    return defHttp.get({ url: strApi, params }, { joinParamsToUrl: true }).then((res) => {
      if (!res) return;

      let curData: object = res;
      listData.value.push({ label: '行政处罚决定书文号', val: curData.docNo });

      if (curData.punishedUsers) {
        listData.value.push({ label: '被处罚个人名称', val: curData.punishedUsers });
      }

      if (curData.punishedOrg) {
        if (curData.punishedOrg.trim().length > 0) {
          listData.value.push({ label: '被处罚单位名称', val: curData.punishedOrg });
        }
      }
      if (curData.punishedOrgHead) {
        if (curData.punishedOrgHead.trim().length > 0) {
          listData.value.push({ label: '被处罚单位负责人', val: curData.punishedOrgHead });
        }
      }

      if (curData.violationFact) {
        if (curData.violationFact.trim().length > 0) {
          listData.value.push({ label: '主要违法违规事实', val: curData.violationFact });
        }
      }

      if (curData.punishBasis) {
        if (curData.punishBasis.trim().length > 0) {
          listData.value.push({ label: '行政处罚依据', val: curData.punishBasis });
        }
      }

      if (curData.punishDecision) {
        listData.value.push({ label: '行政处罚决定', val: curData.punishDecision });
      }

      if (curData.punishCom) {
        listData.value.push({ label: '作出处罚决定的机关名称', val: curData.punishCom });
      }

      if (curData.punishDate) {
        listData.value.push({ label: '处罚日期', val: curData.punishDate });
      }
    });
  };

  // 处理描述：分析
  const handleDesAndAsys = (strDes) => {
    if (strDes && strDes.length > 0) {
      let pp = strDes.indexOf('分析:');
      pp = pp == -1 ? strDes.indexOf('分析：') : pp;
      let des1 = strDes;
      let des2 = '';
      if (pp > -1) {
        des1 = strDes.substring(0, pp);
        des2 = strDes.substring(pp + 3);
      }
      if (des1.substring(2, 3) == ':' || des1.substring(2, 3) == '：') {
        des1 = des1.substring(3);
      }
      listData.value.push({ label: '描述', val: des1 });
      if (des2 != '') {
        listData.value.push({ label: '分析', val: des2 });
      }
    }
  };

  const handleHilight = (strDes, srh) => {
    if (srh && srh.trim().length > 0 && strDes && strDes.trim().length > 0) {
      strDes = strDes.replaceAll(srh, "<em style='color:red'>" + srh + '</em>');
    }
    return strDes;
  };

  function toItemDetail() {
    closeDrawer();
    router.push({ path: '/newlib/index', query: { id: id, tp: sctp } });
  }
  function showFileContent(item) {
    if (item.fpath) {
      let ind = item.fpath.lastIndexOf('.');
      item['fileTp'] = item.fpath.substring(ind + 1);
      openFileContentModel(true, {
        record: item,
        isUpdate: false,
        showFooter: true,
      });
    }
  }
</script>
<style scoped>
  em {
    color: red;
  }

  .ant-list-vertical .ant-list-item-meta-title {
    margin-bottom: 5px;
    color: rgba(0, 0, 0, 0.85);
    font-size: 15px;
    line-height: 24px;
  }
</style>
