<template>
  <div :class="$style.searchdiv">
    <a-card :class="$style.searchWrap">
      <a-form-model
        ref="advancedSearchForm"
        :class="$style.antAdvancedSearchForm"
        :model="form"
        v-bind="formItemLayout"
        layout="horizontal"
      >
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item label="关联方名称">
              <a-input v-model="form.relatedPartyName"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="关联方类型">
              <a-select v-model="form.relatedPartyType" :allow-clear="true" :placeholder="'请选择'">
                <a-select-option value="0">
                  金融产品
                </a-select-option>
                <a-select-option value="1">
                  自然人
                </a-select-option>
                <a-select-option value="2">
                  法人
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="前一层关联方名称">
              <a-input v-model="form.afterRelatedPartyTypeName"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item :class="$style.titleLabel" :label="'与前一层关联方的关系'">
              <a-select
                v-model="form.afterRelatedPartyType"
                :allow-clear="true"
                :placeholder="'请选择'"
              >
                <a-select-option value="0">
                  总经理
                </a-select-option>
                <a-select-option value="1">
                  监事
                </a-select-option>
                <a-select-option value="2">
                  高级管理
                </a-select-option>
                <a-select-option value="4">
                  控制
                </a-select-option>
                <a-select-option value="5">
                  法人代表
                </a-select-option>
                <a-select-option value="6">
                  父亲
                </a-select-option>
                <a-select-option value="7">
                  董事
                </a-select-option>
                <a-select-option value="8">
                  夫妻关系
                </a-select-option>
                <a-select-option value="9">
                  妹妹
                </a-select-option>
                <a-select-option value="10">
                  持有
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="关联方状态">
              <a-select
                v-model="form.relatedPartyStatus"
                :allow-clear="true"
                :placeholder="'请选择'"
              >
                <a-select-option value="0">
                  有效
                </a-select-option>
                <a-select-option value="1">
                  无效
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="16"> </a-col>
          <a-col :class="$style.searchbutton" :span="8">
            <div class="reportbuttonContent" style="margin-right:15%">
              <a-button @click="handleReset">
                重置
              </a-button>
              <a-button type="primary" @click="handleSearch">
                查询
              </a-button>
              <a-button type="primary" @click="exportForm">知识图谱</a-button>
              <audit-advanced-export
                ref="export"
                :class-style="[$style.buttonSpacing]"
                :v-if="false"
                :config-id="91"
                :exclebxh="false"
                :expressions="expressions"
              ></audit-advanced-export>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- <findReportSearch :search="search" :expressions="expressions"> </findReportSearch> -->
    </a-card>
    <a-card class="reporttablecardxm">
      <span class="header_sd-header_common"
        ><span :class="['toptitle', $style.toptitle]">关联交易查询</span></span
      >
      <sd-data-table
        ref="sjzlTable"
        :row-key="(record, index) => index"
        :filter-expressions="expressions"
        :projectlist="true"
        :columns="columns"
        :data-url="'api/xcoa-mobile/v1/audit-report-find/find-report'"
        :show-advance-query="false"
        :scroll="{ x: 1500, y: 300 }"
        :defultpagination-pagesize="50"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="findType" slot-scope="text, record">
          {{ JSON.parse(text).text }}
        </template>
        <template slot="whetherOrderDuty" slot-scope="text, record">
          {{ text === 0 ? '是' : '否' }}
        </template>
      </sd-data-table>
    </a-card>
  </div>
</template>

<script>
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import { getUserInfo } from '@/common/store-mixin'
import auditAdvancedExport from '@product/iam/components/audit-advanced-export'
import auditAdvancedQueryMixins from '@product/iam/components/audit-advanced-query-mixins'
import components from './_import-components/cube-audit-find-report-import'
export default {
  name: 'CubeConnectedTransaction',
  components: {
    ...components,
    auditAdvancedExport,
  },
  metaInfo: {
    title: '关联交易',
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      expressions: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '关联方名称',
          dataIndex: 'unitName',
          width: '120px',
        },
        {
          title: '关联方证件号码',
          dataIndex: 'projectTitle',
          width: '170px',
        },
        {
          title: '关联方类型',
          dataIndex: 'projectCode',
          width: '170px',
        },
        {
          title: '前一层关联方名称',
          dataIndex: 'groupLeaderName',
          width: '170px',
        },
        {
          title: '前一层关联方证件号码',
          dataIndex: 'auditedUnitName',
          width: '170px',
        },
        {
          title: '前一层关联方类型',
          dataIndex: 'creatFindOr',
          width: '180px',
        },
        {
          title: '与前一层之间是否有权益份额',
          dataIndex: 'findTitle',
          width: '170px',
        },
        {
          title: '持有比例',
          dataIndex: 'findType',
          scopedSlots: { customRender: 'findType' },
          width: '170px',
        },
        {
          title: '关联关系说明',
          dataIndex: 'findDesc',
          width: '170px',
        },

        {
          title: '关联方状态',
          dataIndex: 'suggestionType',
          scopedSlots: { customRender: 'suggestionType' },
          width: '170px',
        },
        {
          title: '生效日期',
          dataIndex: 'problemType',
          scopedSlots: { customRender: 'problemType' },
          width: '170px',
        },
      ],

      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      form: {},
      riskOptions: [],
      problemOptions: [],
      auditModeOptions: [],
      rootNode: {},
    }
  },
  created() {
    let userInfo = getUserInfo()
    const params = {
      orgId: userInfo.deptId,
    }
    axios({
      url: 'api/xcoa-mobile/v1/iamorg/getCurrentUserGroup',
      method: 'get',
    }).then((res) => {
      userInfo = res.data
      params.orgId = res.data.id
      axios({
        url: 'api/xcoa-mobile/v1/iamorg/findIamOrgId',
        method: 'post',
        params,
      }).then((res) => {
        this.id = res.data
        const deptCode = userInfo.id.toString()
        const deptName = userInfo.name
        this.rootNode = { code: deptCode, name: deptName, id: this.id }
      })
    })
  },
  mounted() {
    // 初始化数据字典信息
    this.initDictionaryInfo()
  },
  methods: {
    handleReset() {
      this.$refs.advancedSearchForm.resetFields()
      this.form = {}
    },
    initDictionaryInfo() {
      // 风险等级
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=RISK_RATE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.riskOptions = res.data
        }
      })
      // 问题类型
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PROBLEM_TYPE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.problemOptions = res.data
        }
      })
    },
    exportForm() {
      if (!this.$refs.sjzlTable.data || this.$refs.sjzlTable.data.length === 0) {
        message.warning('未查询出可展示图谱的数据', 1)
        return
      }
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          // 此处直接打开知识图谱地址
          window.open('http://kg.smartdot.com:3001/schema/3/knowledge-atlas')
        }
      })
    },
    handleSearch() {
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          const units = this.form.units
          if (units && units.length > 0) {
            var unitsIds = []
            for (var i = 0; i < units.length; i++) {
              unitsIds.push(units[i].id.toString())
            }
            this.form.unitIds = unitsIds
          }
          const auditedUnits = this.form.auditedUnits
          if (auditedUnits && auditedUnits.length > 0) {
            var auditedUnitIds = []
            for (var j = 0; j < auditedUnits.length; j++) {
              auditedUnitIds.push(auditedUnits[j].id.toString())
            }
            this.form.auditedUnitIds = auditedUnitIds
          }
          this.search(this.form)
        }
      })
    },
    // 查询
    search(form) {
      // 默认查询条件
      this.expressions = []
      // 审计发现标题

      if (form.year[0]) {
        var startYear = new Date(form.year[0])
        this.expressions.push({
          dataType: 'str',
          name: 'planYear',
          op: 'ge',
          stringValue: startYear.getFullYear(),
        })
      }
      if (form.year[1]) {
        var endYear = new Date(form.year[1])
        this.expressions.push({
          dataType: 'str',
          name: 'planYear',
          op: 'le',
          stringValue: endYear.getFullYear(),
        })
      }
      if (form.unitIds) {
        this.expressions.push({
          dataType: 'str',
          name: 'unitIds',
          op: 'in',
          stringValue: form.unitIds
            .map((item) => {
              return "'" + item + "'"
            })
            .join(','),
        })
      }
      if (form.projectName) {
        this.expressions.push({
          dataType: 'str',
          name: 'projectName',
          op: 'like',
          stringValue: form.projectName,
        })
      }
      if (form.auditedUnitIds) {
        this.expressions.push({
          dataType: 'str',
          name: 'auditedUnitIds',
          op: 'in',
          stringValue: form.auditedUnitIds
            .map((item) => {
              return "'" + item + "'"
            })
            .join(','),
        })
      }
      if (form.whetherOrderDuty) {
        this.expressions.push({
          dataType: 'str',
          name: 'whetherOrderDuty',
          op: 'ne',
          stringValue: form.whetherOrderDuty,
        })
      }
      if (form.findTitle) {
        this.expressions.push({
          dataType: 'str',
          name: 'findTitle',
          op: 'like',
          stringValue: form.findTitle,
        })
      }
      if (form.findType) {
        this.expressions.push({
          dataType: 'str',
          name: 'findType',
          op: 'like',
          stringValue: form.findType,
        })
      }
      if (form.problemType) {
        this.expressions.push({
          dataType: 'str',
          name: 'problemType',
          op: 'ne',
          stringValue: form.problemType,
        })
      }
      if (form.riskLevel) {
        this.expressions.push({
          dataType: 'str',
          name: 'riskLevel',
          op: 'ne',
          stringValue: form.riskLevel,
        })
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
// .statistics {
//   :global(.ant-table-body) {
//     overflow-x: auto;
//   }
// }
.toptitle {
  color: #404040;
  font-weight: bold;
  font-size: 22px;
  margin-left: calc(50% - 150px);
}

.search-wrap {
  margin-bottom: $padding-lg;
  :global(.ant-advanced-search-form) {
    padding: 0;
    background: transparent;
    border: none;
  }
  :global .ant-divider-horizontal {
    margin: 10px 0;
  }
}
.searchdiv {
  height: 100%;
  :global(.sd-has-table.ant-card) {
    min-height: 50%;
  }
  :global(.reporttablecardxm) {
    :global(.ant-table-body) {
      height: auto !important;
      overflow: auto;
      min-height: auto !important;
    }
  }
  :global(.ant-table-placeholder) {
    height: auto !important;
  }
}
.title-label {
  label {
    width: 6em !important;
    white-space: normal;
    display: inline-flex;
  }
}
</style>
