<template>
  <div>
    <div>
      <a-form-model
        ref="advancedSearchForm"
        :class="$style.antAdvancedSearchForm"
        :model="form"
        v-bind="formItemLayout"
        layout="horizontal"
        @submit="handleSearch"
      >
        <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: '请选择审计机构' }]"
            >
              <sd-group-picker v-model="form.units" :single="false" :read-only="false" />
            </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" :placeholder="'请选择'">
                <a-select-option
                  v-for="(item, index) in problemOptions"
                  :key="index"
                  :value="item.id"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
              <!-- <a-select v-model="form.reportType" :llow-clear="true" :placeholder="'请选择'"> -->

              <!-- <a-select-option value="1">
                  设计问题
                </a-select-option>
                <a-select-option value="2">
                  执行问题
                </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" html-type="submit">
                查询
              </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>
    </div>
  </div>
</template>

<script>
import axios from '@/common/services/axios-instance'
import moment from 'moment'
import auditAdvancedExport from '../../../components/audit-advanced-export'
import AudiitRangePicker from '../../../components/picker/audit-range-picker'
import components from './_import-components/audit-report-search-import'
export default {
  name: 'AuditReportSearch',

  components: {
    auditAdvancedExport,
    ...components,
    AudiitRangePicker,
  },
  props: {
    expressions: {
      type: Array,
      default: null,
    },
    search: {
      type: Function,
      default: null,
    },
  },
  metaInfo: {
    title: 'findReportSearch',
  },
  data() {
    return {
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 },
      },
      form: {},
      formRef: this.$form.createForm(this, { name: 'searchForm' }),
      rules: [],
      problemOptions: [],
    }
  },
  computed: {},
  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(e) {
      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)
        }
      })
    },
    exportData(e) {
      this.$emit('export-data', this.searchs)
    },
    handleReset() {
      this.formRef.resetFields()
      this.form = {}
    },
    exportForm() {
      this.$refs.advancedSearchForm.validate((valid, values) => {
        if (valid) {
          this.$refs.export.exportdata()
        }
      })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
// .ant-advanced-search-form button {
//   margin: 0 5px 10px;
// }
.searchbutton {
  text-align: right;
}
</style>
