<template>
  <div class="admittance-detail">
    <cy-info-title>准入结果</cy-info-title>
    <cy-comm-table
      ref="commTable"
      style="margin-top: 20px;"
      :columns="resultColumns"
      :data="resultTableData"
    />
    <template v-if="hasConclusions">
      <cy-info-title>授信结论</cy-info-title>
      <cy-comm-table
        ref="commTable"
        style="margin-top: 20px;"
        :columns="creditColumns"
        :data="creditTableData"
      />
    </template>

  </div>
</template>

<script>
import { getProjectDetailInfo } from '@/api/myBusiness/projectInfo'
export default {
  name: 'MyBusinessAdmittanceDetail',
  data() {
    return {
      resultColumns: [
        {
          label: '准入类型',
          prop: 'type'
        },
        {
          label: '名称',
          prop: 'name'
        },
        {
          label: '审核状态',
          prop: 'status'
        },
        {
          label: '失败原因',
          prop: 'error'
        },
        {
          label: '操作',
          show: this.$route.query.type !== 'details',
          render: (h, row) => {
            const btnList = [
              { msg: '查看', icon: 'iconfont icon-orange icon-a-Group951', power: 'projectNumber:delete', category: 'see' }
            ]
            const options = btnList.map(item => {
              return (
                <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 200
        }
      ],
      resultTableData: [

      ],
      creditColumns: [
        {
          label: '序号',
          index: true,
          width: '150'
        },
        {
          label: '条件',
          prop: 'content',
          render: (h, row) => {
            if (row.fx) {
              return <div domPropsInnerHTML={this.formatCondition(row)}></div>
            } else {
              return <span>{ row.content}</span>
            }
          }
        }
      ],
      creditTableData: [

      ],
      hasConclusions: false
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      const { id } = this.$route.query
      getProjectDetailInfo({ id: id }).then(res => {
        this.resultTableData.push({
          type: '项目',
          name: res.data.projectName,
          status: res.data.projectStatusStr,
          error: res.data.errorsString
        })
        this.resultTableData.push({
          type: '核心企业',
          name: res.data.coreEnterpriseName,
          status: res.data.coreEnterpriseStatusStr,
          error: res.data.projectCoreEnterprise.errorsString
        })

        if (res.data.creditConclusions.length) {
          this.hasConclusions = true
          this.creditTableData = res.data.creditConclusions
        }
      })
    },
    handlerOperate(row) {
      const { id } = this.$route.query
      this.$router.push({
        path: '/myBusiness/projectView/child',
        query: {
          id: id,
          type: 'see'
        }
      })
    },
    formatCondition(row) {
      const { content, fx } = row
      // 正则表达式匹配
      const regex = fx.replace(/^\/(.*)\/g?$/, '$1')
      // 匹配结果数组
      const match = new RegExp(regex, 'g').exec(content)
      if (match) {
        const text1 = match[1]
        const text2 = match[2]
        let highlightedText = content.replace(text1, `<span style='color: red;'>${text1}</span>`)
        highlightedText = highlightedText.replace(text2, `<span style='color: red;'>${text2}</span>`)
        return highlightedText
      } else {
        console.log('未找到匹配的文本')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.admittance-detail {

}
</style>
