<template>
  <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-model-item label="项目名称">
            <a-input v-model="form.projectName"></a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="被审计单位">
            <sd-group-picker v-model="form.auditedUnits" :single="false" :read-only="false" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="是否问责">
            <a-select v-model="form.whetherOrderDuty" :llow-clear="true" :placeholder="'请选择'">
              <a-select-option value="0">
                是
              </a-select-option>
              <a-select-option value="1">
                否
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="审计发现标题">
            <a-input v-model="form.findTitle"></a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="审计发现类别">
            <a-input v-model="form.findType"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="问题类型">
            <a-select v-model="form.problemType" :placeholder="'请选择'">
              <a-select-option
                v-for="(item, index) in problemOptions"
                :key="index"
                :value="item.id"
              >
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="风险评级">
            <a-select v-model="form.riskLevel" :llow-clear="true" :placeholder="'请选择'">
              <a-select-option v-for="(risk, index) in riskOptions" :key="index" :value="risk.id">{{
                risk.name
              }}</a-select-option>
            </a-select>
          </a-form-model-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="91"
              :exclebxh="false"
              :expressions="expressions"
            ></audit-advanced-export>
          </div>
        </a-col>
      </a-row>
    </a-form-model>
  </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 '../report/_import-components/find-report-search-import'

export default {
  name: 'FindReportSearch',

  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: [],
      riskOptions: [],
      problemOptions: [],
      auditModeOptions: [],
    }
  },
  computed: {},
  mounted() {
    // 初始化数据字典信息
    this.initDictionaryInfo()
  },
  methods: {
    moment,
    initDictionaryInfo() {
      // 风险等级
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=RISK_RATE',
        method: 'get',
      }).then((res) => {
        if (res.status === 200) {
          this.riskOptions = res.data
        }
      })
      // 问题类型
      axios({
        url: 'api/xcoa-mobile/v1/iam-law/dictionary?key=PROBLEM_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>
