<template>
  <div :class="$style.wrapHeight">
    <div>
      <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="businessCode">
                <a-input v-model="form.businessCode" placeholder="请输入业务编码" allow-clear />
              </a-form-model-item>
            </a-col>

            <a-col :span="8">
              <a-form-model-item :label="'事件标签分类'" prop="eventTagName">
                <a-input v-model="form.eventTagName" placeholder="事件标签分类" allow-clear />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <!-- todo 二级单位简称 -->
              <a-form-model-item :label="'二级单位简称'" prop="secondUnit">
                <a-input v-model="form.secondUnit" placeholder="请输入二级单位简称" allow-clear />
              </a-form-model-item>
            </a-col>
            <!-- 开始时间 ，结束时间  -->
            <a-col :span="8">
              <a-form-model-item label="业务时间">
                <a-range-picker v-model="time1" :format="'YYYY-MM-DD'" @change="changeTimeRange" />
              </a-form-model-item>
            </a-col>
            <!-- 单位 unit -->
            <a-col :span="8">
              <a-form-model-item :label="'单位'" prop="unit">
                <a-input v-model="form.unit" placeholder="请输入单位" allow-clear />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item :label="'模型类型'" prop="modelType">
                <a-select v-model="form.modelType" placeholder="请选择模型类型" allow-clear>
                  <!-- <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="问题推送时间" prop="timeRange">
                <a-range-picker
                  v-model="time2"
                  :format="'YYYY-MM-DD'"
                  @change="changeStartTimeChange"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <div :class="$style.buttonContent">
                <a-button type="primary" @click="handleAction('serach')">查询</a-button>
                <a-button @click="handleReset">重置</a-button>
                <!-- <a-button :loading="exportLoading" type="primary" @click="fnexport">导出</a-button> -->
                <!-- 下发 -->
                <a-button type="primary" @click="handleIssue">下发</a-button>
                <!-- 删除 -->
                <a-button type="primary" @click="handleDelete">删除</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"
            :show-selection="(record) => record.examineNum !== record.questionNum"
            :scroll="{ x: 1300 }"
            data-url="api/xcoa-mobile/v1/iamquestionreform/questionExamineSummaryQueryList"
          >
            <div slot="action" slot-scope="text, record">
              <a-button type="link" @click="detailClick(record)">明细</a-button>
            </div>
          </sd-data-table>
        </a-card>
        <audit-advanced-export
          ref="export"
          :v-if="false"
          :exclebxh="false"
          table-id="iamModelMaintain"
          :expressions="exportExpressions"
          :config-id="121"
        />
      </div>
      <a-modal v-model="issueVisible" title="下发提醒" :footer="null" @cancel="cancel">
        <div>
          <p v-for="(item, i) in issueMessageList" :key="i"
            >{{ item.label + item.value
            }}<span v-if="i === 3 && issueMessageList[3].value !== ''"
              >未绑定整改人与审核、复审人，请前往<a @click="toPath">模型领域</a
              >人员关联页面完善信息后重试
            </span>
          </p>
        </div>
      </a-modal>
    </div>
  </div>
</template>

<script>
import components from './_import-components/audit-shsum-list-import'
import auditAdvancedExport from '../../components/audit-advanced-export.vue'
import moment from 'moment'
import AuditRiskbraryService from '@product/iam/audit/riskLibrary/riskLibrary'
import { message, Modal } from 'ant-design-vue'

export default {
  name: 'AuditShsumList',
  metaInfo: {
    title: '审计问题总汇审核',
  },
  components: {
    ...components,
    auditAdvancedExport,
  },
  data() {
    return {
      exportLoading: false,
      exportExpressions: [],
      // 下发弹窗
      issueVisible: false,
      issueMessageList: [
        { label: '下发总数：', value: 0 },
        { label: '下发成功：', value: 0 },
        { label: '下发失败：', value: 0 },
        { label: '下发失败原因：', value: '' },
      ],
      form: {
        businessCode: null,
        eventTagName: null,
        secondUnit: null,
        risk: null,
        modelType: null,
        belongedSecondUnit: null,
        unit: null,
        // buss_time
        bussTime: null,
        abbreviation: null,
        //  bussTimeSt bussTimeEd 业务时间
        bussTimeSt: null,
        bussTimeEd: null,
        pushDateSt: null,
        pushDateEd: null,
      },
      time1: [],
      time2: [],
      reqData: {
        pushDateSt: '',
        pushDateEd: '',
      },
      formItemLayout: {
        labelCol: { span: 10 },
        wrapperCol: { span: 14 },
      },
      dataKey: 0,
      columns: [
        {
          title: '序号',
          dataIndex: 'sortNumber',
          width: '70px',
          customRender: (text, record, index) => `${index + 1}`,
        },
        {
          title: '业务编码',
          dataIndex: 'businessCode',
        },

        {
          title: '事件标签',
          dataIndex: 'jmEventType',
        },
        {
          title: '事件标签分类',
          dataIndex: 'eventTagName',
        },
        {
          title: '模型类型',
          dataIndex: 'modelTypeName',
        },
        {
          dataIndex: 'modelType',
          sdHidden: true,
        },
        {
          title: '问题推送日期',
          dataIndex: 'pushDate',
        },

        {
          title: '二级单位简称',
          dataIndex: 'secondUnit',
        },
        {
          dataIndex: 'secondUnitCode',
          sdHidden: true,
        },
        {
          dataIndex: 'unitCode',
          sdHidden: true,
        },
        {
          title: '单位',
          dataIndex: 'unit',
        },
        //         /**
        //  * 问题数量
        //  */
        // private Integer questionNum;
        // /**
        //  * 未审核数量
        //  */
        // private Integer nonExamineNum;
        // /**
        //  * 已审核数量
        //  */
        // private Integer examineNum;

        {
          title: '问题数量',
          dataIndex: 'questionNum',
        },
        // 已审核数量 ，待审核数量
        {
          title: '已下发数量',
          dataIndex: 'examineNum',
        },
        {
          title: '待下发数量',
          dataIndex: 'nonExamineNum',
        },
        // 下发失败数量
        {
          title: '下发失败数量',
          dataIndex: 'examineFailNum',
        },
        {
          title: '操作',
          dataIndex: 'sharedSetting',
          fixed: 'right',
          width: '100px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  methods: {
    toPath() {
      window.open('#/sd-frame/audit-area-list')
    },
    setMessageList(data) {
      this.issueMessageList = [
        { label: '下发总数：', value: data.questionNum },
        { label: '下发成功：', value: data.examineNum },
        { label: '下发失败：', value: data.examineFailNum },
        { label: '下发失败原因：', value: data.failReason },
      ]
      this.issueVisible = true
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    cancel() {
      this.issueVisible = false
      this.selectedRowKeys = []
      this.selectedRows = []
    },
    // 审核
    handleIssue() {
      const selectedRows = this.$refs.SJMXDataTable.getSelectedRows()
      const params = selectedRows.map((item) => {
        return {
          businessCode: item.businessCode,
          jmEventType: item.jmEventType,
          eventTagName: item.eventTagName,
          modelTypeName: item.modelTypeName,
          pushDate: item.pushDate,
          unitCode: item.unitCode,
        }
      })
      AuditRiskbraryService.questionSummaryExamine(params).then((res) => {
        this.setMessageList(res.data)
        this.handleSearch()
      })
    },
    handleDelete() {
      Modal.confirm({
        title: '提示',
        content: '仅支持删除待下发及下发有误的数据，是否确认删除？',
        onOk: () => {
          const selectedRows = this.$refs.SJMXDataTable.getSelectedRows()
          const params = selectedRows.map((item) => {
            return {
              businessCode: item.businessCode,
              jmEventType: item.jmEventType,
              eventTagName: item.eventTagName,
              modelTypeName: item.modelTypeName,
              pushDate: item.pushDate,
              unitCode: item.unitCode,
            }
          })
          AuditRiskbraryService.questionSummaryDelete(params).then((res) => {
            message.success('删除成功')
            this.handleSearch()
          })
        },
      })
    },
    // 开始时间 结束时间
    changeTimeRange(val) {
      // 开始天的 00:00:00 结束天的 23:59:59 时间戳
      const timeList = val.map((item) => {
        // 转化成时间戳 并忽略时分秒
        return moment(item._d)
          .startOf('day')
          .valueOf()
      })
      // 结束时间加一天的时间戳
      this.form.bussTimeSt = timeList[0]
      this.form.bussTimeEd = timeList[1] + 60 * 60 * 24 * 1000
    },
    changeStartTimeChange(val) {
      // "stringValue": "2023-12-16" 转换成这个格式
      this.form.pushDateSt = moment(val[0]._d).format('YYYY-MM-DD')
    },
    // 处理请求参数
    setExpressions() {
      const expressions = []

      const filter = ['bussTimeSt', 'bussTimeEd', 'pushDateSt', 'pushDateEd', 'modelType']

      for (const key in this.form) {
        if (this.form[key] && !filter.includes(key)) {
          expressions.push({
            dataType: 'str',
            name: key,
            op: 'like',
            stringValue: '%' + this.form[key] + '%',
          })
        }
        if (this.form[key] && filter.includes(key)) {
          expressions.push({
            dataType: 'str',
            name: key,
            op: 'eq',
            stringValue: this.form[key],
          })
        }
      }
      return expressions
    },
    processReq(req) {
      req.data.expressions = []
      req.data.expressions = this.setExpressions()
      return req
    },
    handleReset() {
      this.$refs.advancedSearchForm.resetFields()
      this.form.bussTimeEd = null
      this.form.bussTimeSt = null
      this.form.pushDateSt = null
      this.form.pushDateEd = null
      this.time1 = []
      this.time2 = []
      this.handleAction('serach')
    },
    // 查询
    handleAction(type) {
      // 调用方法
      switch (type) {
        case 'serach':
          this.handleSearch()
          break
      }
    },
    handleSearch() {
      this.dataKey++
    },
    detailClick(record) {
      const unitCode = record.unitCode
      let url = '#/audit-shchange-list?'
      const allQueryData = {
        businessCode: record.businessCode,
        jmEventType: record.jmEventType,
        eventTagName: record.eventTagName,
        modelTypeName: record.modelTypeName,
        pushDate: record.pushDate,
        unitCode: unitCode,
        modelId: record.modelId,
      }
      if (record.modelTypeName !== '整改类' && record.modelTypeName !== '定位型-整改类') {
        url = '#/audit-shdetail-list?'
      }
      for (const key in allQueryData) {
        if (allQueryData[key]) {
          url += `${key}=${allQueryData[key]}&`
        }
      }
      window.open(url)
    },
    fnexport() {
      this.exportExpressions = this.setExpressions()
      this.exportExpressions.push({
        dataType: 'str',
        name: 'maxResults',
        op: 'eq',
        stringValue: -1,
      })
      setTimeout(() => {
        this.$refs.export.exportdataRisk()
      }, 200)
    },
  },
}
</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;
}
</style>
