<template>
  <audit-form-top-banner :form-data="'readonly'">
    <a-card :class="$style.conditions">
      <a-form-model
        ref="advancedSearchForm"
        class="ant-advanced-search-form"
        :model="form"
        v-bind="formItemLayout"
      >
        <a-row :gutter="24" :class="$style.antformitem">
          <a-col :span="8">
            <a-form-model-item :label="'状态'" prop="viewFlag">
              <a-select v-model="form.viewFlag" placeholder="请选择状态" allow-clear>
                <a-select-option value="1">已查看</a-select-option>
                <a-select-option value="0">未查看</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <div :class="$style.buttonContent">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button @click="handleReset">重置</a-button>
              <!-- 导出 -->
              <a-button @click="exportExcel">导出</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>
    <div>
      <a-card>
        <sd-data-table
          ref="SJMXDataTable"
          :key="dataKey"
          :columns="columns"
          :process-req="processReq"
          form-id="iamModelMaintain"
          :scroll="{ x: 1300 }"
          show-selection
          data-url="api/xcoa-mobile/v1/iamquestionreform/iamQuestionReformListNew"
          :filter-expressions="expressions"
        >
          <div slot="action" slot-scope="text, record">
            <!-- 如果 -->
            <a-button type="link" @click="showModal(record)">详情</a-button>
            <!-- <a-button
              type="link"
              :disabled="record.viewFlag !== 0"
              @click="viewFlagClick(record)"
              >{{ record.viewFlag === 0 ? '查看' : '已查看' }}</a-button
            > -->
          </div>
          <div slot="viewFlag" slot-scope="text, record">
            <span>{{ record.viewFlag === 1 ? '已查看' : '未查看' }}</span>
          </div>
        </sd-data-table>
      </a-card>
      <a-modal v-model="visible" title="问题详情" :footer="null" width="1200px" @cancel="cancel">
        <div :class="$style.modelDetail">
          <span>模型编码：{{ modelDetail.batchCode }}</span>
          <span>模型名称：{{ modelDetail.batchName }}</span>
          <span>业务领域：{{ modelDetail.modelDomainName }}</span>
        </div>
        <div :class="$style.detail">
          <a-table
            :columns="columnsDetail"
            :data-source="dataDetail"
            :row-key="(_, index) => index"
            :scroll="{ x: 1150, y: 500 }"
          >
          </a-table>
        </div>
      </a-modal>
    </div>
  </audit-form-top-banner>
</template>

<script>
import components from './_import-components/audit-view-detail-import'
import AuditRiskbraryService from '@product/iam/audit/riskLibrary/riskLibrary'
import { message, Modal } from 'ant-design-vue'
import auditFormTopBanner from './audit-top-banner'
import download from '@/common/services/download'
export default {
  name: 'AuditViewDetail',
  metaInfo: {
    title: '审计问题明细-关注类',
  },
  components: {
    ...components,
    auditFormTopBanner,
  },
  data() {
    return {
      modelDetail: {},
      form: {
        viewFlag: null,
      },
      visible: false,
      ModalquestionDetail: '',
      dataKey: 0,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '70px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        // {
        //   title: '批次号',
        //   dataIndex: 'batchCode',
        // },
        // {
        //   title: '批次名称',
        //   dataIndex: 'batchCode',
        // },
        {
          title: '模型编码',
          dataIndex: 'batchCode',
        },
        {
          title: '模型名称',
          dataIndex: 'batchName',
        },
        {
          title: '业务领域',
          dataIndex: 'modelDomainName',
        },
        {
          title: '业务阶段',
          dataIndex: 'modelPhaseName',
        },
        {
          title: '风险描述',
          dataIndex: 'risk',
        },
        {
          title: '问题推送日期',
          dataIndex: 'pushDate',
        },
        {
          title: '状态',
          dataIndex: 'viewFlag',
          scopedSlots: { customRender: 'viewFlag' },
        },
        {
          title: '查看人',
          dataIndex: 'modifyEmpAccount',
        },
        {
          title: '操作',
          dataIndex: 'sharedSetting',
          fixed: 'right',
          width: '150px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      expressions: [
        {
          dataType: 'str',
          name: 'modelType',
          op: 'eq',
          stringValue: '0',
        },
      ],
      queryData: {},
      columnsDetail: [],
      dataDetail: [],
    }
  },
  created() {
    this.initQueryData()
  },
  methods: {
    //
    // 初始化initQueryData
    initQueryData() {
      this.queryData = this.$route.query
      this.queryData.modelType = '2'
    },
    // 导出
    // 导出
    exportExcel() {
      // 判断选中的数据，至少选中一条数据
      const selectedRows = this.$refs.SJMXDataTable.getSelectedRows()
      if (selectedRows.length === 0) {
        message.warning('请至少选择一条数据')
        return
      }
      // 调用导出
      // 导出参数
      const params = selectedRows.map((item) => {
        return {
          modelId: item.modelId,
          detailGroupId: item.detailGroupId,
          businessCode: item.businessCode,
          jmEventType: item.jmEventType,
          eventTagName: item.eventTagName,
          modelTypeName: item.modelTypeName,
          pushDate: item.pushDate,
          unitCode: item.unitCode,
          modelType: item.modelType,
          bussKey: item.bussKey || null,
        }
      })
      // exportQuestionDetail
      AuditRiskbraryService.exportQuestionDetail(params).then((res) => {
        if (res.status === 200) {
          //
          const url = URL.createObjectURL(res.data)
          const filename = res.headers['content-disposition']
          const fname = filename.substring(filename.indexOf('filename=') + 9, filename.length)
          download(url, decodeURI(fname))
          // 导出成功
          message.success('导出成功')
        } else {
          Modal.warning({
            title: '提示',
            content: '导出报错，请联系管理员!',
          })
          return false
        }
      })
    },
    processReq(req) {
      req.data.expressions = [
        { dataType: 'str', name: 'pushDate', op: 'eq', stringValue: this.$route.query.pushDate },
        { dataType: 'str', name: 'modelType', op: 'eq', stringValue: '2' },
        { dataType: 'str', name: 'viewFlag', op: 'eq', stringValue: this.form.viewFlag },
      ]
      Object.keys(this.queryData).map((key) => {
        req.data.expressions.push({
          dataType: 'str',
          name: key,
          op: 'eq',
          stringValue: this.queryData[key],
        })
      })
      return req
    },
    handleSearch() {
      this.dataKey++
    },
    handleReset() {
      this.$refs.advancedSearchForm.resetFields()
    },
    viewFlagClick(row) {
      const params = {
        ...this.queryData,
        modelId: row.modelId,
        detailGroupId: row.detailGroupId,
        viewFlag: 1,
        reformStatus: 1,
      }
      AuditRiskbraryService.saveIamQuestionReform(params).then((res) => {
        if (res) {
          message.success('提交成功')
          this.handleSearch()
        }
      })
    },
    // showModal(row) {
    //   this.ModalquestionDetail = JSON.parse(row.questionDetail)
    //   this.visible = true
    // },
    showModal(row) {
      // this.ModalquestionDetail = JSON.parse(row.questionDetail)
      const params = {
        modelId: row.modelId,
        detailGroupId: row.detailGroupId,
        ...this.queryData,
        bussKey: row.bussKey || null,
      }
      const formData = new FormData()
      Object.keys(params).forEach((key) => {
        formData.append(key, params[key])
      })
      this.modelDetail = {
        modelDomainName: row.modelDomainName,
        batchName: row.batchName,
        batchCode: row.batchCode,
        riskData: row.risk,
      }
      const detailJson = JSON.stringify(this.modelDetail)
      const queryJson = JSON.stringify(params)
      window.open(
        `#/audit-risk-detail?queryJson=${queryJson}&modelDetail=${detailJson}&detailStatus=${this.detailStatus}`
      )
    },
    cancel() {
      this.visible = false
      this.dataDetail = []
      this.columnsDetail = []
      this.modelDetail = {}
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.wrap-height {
  height: 100%;
  .row-height {
    display: flex;
    flex: auto;
    height: 100%;
    .rightcard {
      flex: 1;
      width: calc(100% - 20%);
      height: 100%;
    }
  }
}

.button-content {
  button {
    margin: 0 4px;
  }
}
.no-action {
  padding: 0 15px;
  color: $text-color-secondary;
}
.detail {
  width: 100%;
  overflow-x: auto;
}
.detail-delete {
  display: flex;
  justify-content: flex-end;
}
.model-detail {
  display: flex;
  margin-bottom: 10px;
  span {
    margin-right: 20px;
  }
}
</style>
