<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="risk">
                <a-input v-model="form.risk" 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">
              <!-- <a-form-model-item :label="'管理归属二级单位'" prop="belongedSecondUnit">
                <a-input
                  v-model="form.belongedSecondUnit"
                  placeholder="请输入管理归属二级单位"
                  allow-clear
                />
              </a-form-model-item> -->
              <!-- 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">
                <!-- <AuditRangeDatePicker
                  :time-placeholder="['开始时间', '结束时间']"
                  :time-mode="['date', 'date']"
                  :time-format="'YYYY-MM-DD'"
                  :time-range.sync="form.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>
              </div>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <div>
        <a-card>
          <sd-data-table
            ref="SJMXDataTable"
            :key="dataKey"
            :row-key="(record, index) => index"
            :columns="columns"
            :process-req="processReq"
            form-id="iamModelMaintain"
            :scroll="{ x: 1300 }"
            data-url="api/xcoa-mobile/v1/iamquestionreform/iamQuestionReformSummaryQueryList"
          >
            <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>
    </div>
  </div>
</template>

<script>
import components from './_import-components/audit-view-sum-import'
import auditAdvancedExport from '../../components/audit-advanced-export.vue'
import { getUserInfo } from '@/common/store-mixin'
import AuditRiskbraryService from './riskLibrary'
import moment from 'moment'
export default {
  name: 'AuditViewSum',
  metaInfo: {
    title: '审计问题总汇',
  },
  components: {
    ...components,
    auditAdvancedExport,
  },
  data() {
    return {
      exportLoading: false,
      exportExpressions: [],
      form: {
        businessCode: null,
        eventTagName: null,
        secondUnit: null,
        risk: null,
        modelType: null,
        // timeRange: [],
        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',
        },

        // todo 事件标签
        {
          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',
        },
        {
          title: '问题数量',
          dataIndex: 'questionNum',
        },
        {
          title: '非问题数量',
          dataIndex: 'nonQuestionNum',
        },
        {
          title: '已整改数量',
          dataIndex: 'reformNum',
        },
        {
          title: '未整改数量',
          dataIndex: 'nonReformNum',
        },

        {
          title: '操作',
          dataIndex: 'sharedSetting',
          fixed: 'right',
          width: '100px',
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },

  methods: {
    // 开始时间 结束时间
    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')
      this.form.pushDateEd = moment(val[1]._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) {
      // this.reqData.pushDateSt = this.form.timeRange.length ? this.form.timeRange[0] : ''
      // this.reqData.pushDateEd = this.form.timeRange.length ? this.form.timeRange[1] : ''
      // 调用方法
      switch (type) {
        case 'serach':
          this.handleSearch()
          break
      }
    },
    handleSearch() {
      this.dataKey++
    },
    detailClick(record) {
      const unitCode = record.unitCode
      let url = '#/audit-view-change?'
      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-view-detail?'
      }
      for (const key in allQueryData) {
        if (allQueryData[key]) {
          url += `${key}=${allQueryData[key]}&`
        }
      }
      window.open(url)
    },
    fnexport() {
      this.exportExpressions = this.setExpressions()
      const account = getUserInfo().account
      AuditRiskbraryService.getUserInfo(account).then((res) => {
        this.exportExpressions.push({
          dataType: 'str',
          name: 'unitCode',
          op: 'eq',
          stringValue: res.data.parentCode,
        })
        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>
