<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="'年度'"
              prop="year"
              :rules="[{ required: true, message: '请选择年度' }]"
            >
              <!-- <a-space direction="vertical" :size="12"> -->
              <AudiitRangePicker :time-range.sync="form.year"></AudiitRangePicker>
              <!-- </a-space> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item
              :label="'审计机构'"
              prop="units"
              :rules="[{ required: true, message: '请选择审计机构' }]"
            >
              <AuditGroupPicker
                v-model="form.units"
                :single="false"
                :read-only="false"
                :root-node="rootNode"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="被审计单位">
              <sd-group-picker v-model="form.auditedUnits" :single="false" :read-only="false" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item label="审计类型">
              <a-select v-model="form.auditType" :allow-clear="true" :placeholder="'请选择'">
                <a-select-option
                  v-for="(item, index) in problemOptions"
                  :key="index"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="报告名称">
              <a-input v-model="form.fileTitle"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="项目名称">
              <a-input v-model="form.projectTitle"></a-input>
            </a-form-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="101"
                :exclebxh="false"
                :expressions="expressions"
              ></audit-advanced-export>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- <auditReportSearch :search="search" :expressions="expressions"> </auditReportSearch> -->
    </a-card>
    <a-card class="reporttablecardxm">
      <span class="header_sd-header_common"
        ><span :class="['toptitle', $style.toptitle]">审计报告查询</span></span
      >
      <sd-data-table
        ref="sjzlTable"
        :form-id="'IAM_AUDIT_REPORT'"
        :filter-expressions="expressions"
        :columns="columns"
        :data-url="'api/xcoa-mobile/v1/iam-audit-report/auditReport'"
        :show-advance-query="false"
        :defultpagination-pagesize="50"
        @dataLoaded="dataLoaded"
        @searchbtnClick="searchbtnClick"
      >
        <template slot="fileTitle" slot-scope="text, record">
          <a :title="text" @click="clickRow(record)">{{ text }}</a>
        </template>
        <template slot="findType" slot-scope="text, record">
          {{ JSON.parse(text).text }}
        </template>
        <template slot="whetherOrderDuty" slot-scope="text, record">
          {{ text === 0 ? '是' : '否' }}
        </template>
        <template slot="suggestionType" slot-scope="text, record">
          {{ JSON.parse(text).text }}
        </template>
        <template slot="problemType" slot-scope="text, record">
          {{ JSON.parse(text).text }}
        </template>
      </sd-data-table>
    </a-card>
  </div>
</template>

<script>
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import moment from 'moment'
import { getUserInfo } from '@/common/store-mixin'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import auditAdvancedExport from '../../../components/audit-advanced-export'
import AudiitRangePicker from '../../../components/picker/audit-range-picker'
import AuditGroupPicker from '../../../components/picker/audit-group-picker.vue'
import auditAdvancedQueryMixins from '../../../components/audit-advanced-query-mixins'
import components from './_import-components/audit-report-import'
export default {
  name: 'AuditReport',
  metaInfo: {
    title: '审计报告查询',
  },
  components: {
    ...components,
    AudiitRangePicker,
    auditAdvancedExport,
    AuditGroupPicker,
  },
  mixins: [auditAdvancedQueryMixins],
  data() {
    return {
      expressions: [],
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => `${index + 1}`,
          width: '80px',
        },
        {
          title: '报告标题',
          dataIndex: 'fileTitle',
          scopedSlots: { customRender: 'fileTitle' },
        },
        {
          title: '审计机构',
          dataIndex: 'unitName',
        },
        {
          title: '被审计单位',
          dataIndex: 'auditedUnitNames',
        },
        {
          title: '审计类型',
          dataIndex: 'auditType',
          scopedSlots: { customRender: 'auditType' },
        },
        {
          title: '项目名称',
          dataIndex: 'projectTitle',
        },
        {
          title: '项目编号',
          dataIndex: 'projectCode',
        },
        {
          title: '报告文号',
          dataIndex: 'reportNumber',
        },
        {
          dataIndex: 'instId',
          sdHidden: true,
        },
      ],
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      form: {},
      problemOptions: [],
      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: {
    moment,
    initDictionaryInfo() {
      // 问题类型
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PRODUCT_IAM_AUDIT_TYPE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.problemOptions = res.data
        }
      })
    },
    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].code.toString())
            }
            this.form.auditedUnitIds = auditedUnitIds
          } else {
            this.form.auditedUnitIds = null
          }
          this.search(this.form)
        }
      })
    },
    handleReset() {
      // this.formRef.resetFields()
      this.$refs.advancedSearchForm.resetFields()
      this.form = {}
    },
    exportForm() {
      if (!this.$refs.sjzlTable.data || this.$refs.sjzlTable.data.length === 0) {
        message.warning('未查询出可导出数据', 1)
        return
      }

      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          this.$refs.export.exportdata()
        }
      })
    },
    dataLoaded(eventData) {
      if (!eventData.data) {
        return
      }
      eventData.data.forEach((item, index) => {
        item.id = index
      })
      return eventData
    },
    // 刷新列表
    refresh() {
      this.$refs.sjzlTable.refresh()
    },
    // 查询
    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.projectTitle) {
        this.expressions.push({
          dataType: 'str',
          name: 'projectTitle',
          op: 'like',
          stringValue: form.projectTitle,
        })
      }
      if (form.auditedUnitIds) {
        this.expressions.push({
          dataType: 'str',
          name: 'auditedUnitIds',
          op: 'in',
          stringValue: form.auditedUnitIds
            .map((item) => {
              return "'" + item + "'"
            })
            .join(','),
        })
      }
      if (form.auditType) {
        this.expressions.push({
          dataType: 'str',
          name: 'auditType',
          op: 'ne',
          stringValue: form.auditType,
        })
      }

      if (form.fileTitle) {
        this.expressions.push({
          dataType: 'str',
          name: 'fileTitle',
          op: 'like',
          stringValue: form.fileTitle,
        })
      }
    },
    clickRow(record) {
      const url = '/sd-webflow/done-pages/' + record.instId
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          // 这里写或者调刷新的方法
          this.refreshDataTable()
        }
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.toptitle {
  margin-left: calc(50% - 150px);
  font-size: 22px;
  font-weight: bold;
  color: #404040;
}
.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;
      min-height: auto !important;
      overflow: auto;
    }
  }
  :global(.ant-table-placeholder) {
    height: auto !important;
  }
}
</style>
