<template>
  <a-modal
    :class="$style.mainModal"
    :body-style="bodyStyle"
    :title="title"
    :destroy-on-close="true"
    :visible="visible"
    :width="modalWidth"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <audit-advanced-query
      :expand="expand"
      :search-data="formData"
      :ref-name="searchform"
      :search-style="{
        height: '130px',
        left: '0px',
        top: '120px !important',
        width: 'calc(100% - 40px) !important',
      }"
      :search-fun="handleSearch"
      @searchedClick="searchedClick"
    >
      <template>
        <a-col :span="12">
          <a-form-model-item :label="'模型编号'" prop="modelCode">
            <a-input v-model="formData.modelCode" allow-clear />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item :label="'模型名称'" prop="modelName">
            <a-input v-model="formData.modelName" allow-clear />
          </a-form-model-item>
        </a-col>
      </template>
    </audit-advanced-query>
    <!-- 列表 -->
    <sd-data-table-ex
      ref="SJMXDataTable"
      :show-advance-btn="true"
      form-id="iamModelMaintain"
      page-id="audit/maintain/iamModelMaintain"
      :search-fields="['modelCode', 'modelName']"
      :pagination="{ pageSize: 10 }"
      :filter-expressions="filterExpressions"
      :check-type="'checkbox'"
      :columns="columns"
      :show-advance-query="true"
      show-selection
      :process-res="processRes"
      @searchbtnClick="searchbtnClick"
      @onChange="onChange"
    >
    </sd-data-table-ex>
  </a-modal>
</template>

<script>
import moment from 'moment'
import auditAdvancedQueryMixins from '../../components/audit-advanced-query-mixins'
import auditAdvancedQuery from '../../components/audit-advanced-query.vue'
import components from './_import-components/audit-select-maintain-import'
const columns = [
  {
    title: '序号',
    dataIndex: 'sortNumber',
    customRender: (text, record, index) => `${index + 1}`,
    width: '80px',
  },
  {
    title: '模型编号',
    dataIndex: 'modelCode',
    width: '20%',
  },
  {
    title: '模型名称',
    dataIndex: 'modelName',
    width: '20%',
  },
  {
    title: '模型描述',
    dataIndex: 'modelDesc',
  },
]
export default {
  name: 'AuditSelectMaintain',
  metaInfo: {
    title: '审计模型选择器',
  },
  components: {
    ...components,
    auditAdvancedQuery,
  },
  mixins: [auditAdvancedQueryMixins],
  props: {
    // 弹出窗标题
    title: {
      type: String,
      default: '审计模型选择器',
    },
    // 弹出窗宽度
    modalWidth: {
      type: String,
      default: '1200px',
    },
    // 弹出窗显示参数
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      year: '',
      searchYear: '',
      yearShow: false,
      bodyStyle: {
        // padding: 0,
      },
      // 列表展示用
      columns,

      // 高级搜索
      searchform: 'searchform',
      expand: false,
      // 列表展示过滤条件
      filterExpressions: [],
      formData: {
        modelCode: '',
        modelName: '',
      },
      period: [],
    }
  },
  methods: {
    moment,
    processRes(data) {
      let index = 0
      data.data.forEach((item) => {
        item.id = this.$refs.SJMXDataTable.localPagination.current + '-' + index
        index++
      })
      return data
    },
    handleOk(e) {
      this.$parent.visible = !this.$parent.visible
      this.$parent.visibleY = !this.$parent.visibleY
      // 列表选择事件，返回选择的数据
      this.$emit(
        'listMxSelected',
        this.$refs.SJMXDataTable.getSelectedRowKeys(),
        this.$refs.SJMXDataTable.getSelectedRows()
      )
    },
    handleCancel(e) {
      this.$parent.visible = !this.$parent.visible
      this.$parent.visibleY = !this.$parent.visibleY
    },
    // 翻页操作
    onChange(pagination, filters, sorter) {
      this.pageSize = pagination.pageSize
      this.startPosition = (pagination.current - 1) * pagination.pageSize
    },
    // 开启关闭
    searchedClick() {
      this.expand = false
    },
    handleSearch() {
      const formData = this.formData
      // 获取当前表格过滤条件
      this.filterExpressions = []
      // 获取高级搜索的数据
      // 开始修改
      if (formData !== null) {
        // 模型编号
        if (this.formData.modelCode) {
          this.filterExpressions.push({
            dataType: 'str',
            name: 'modelCode',
            op: 'like',
            stringValue: `%${this.formData.modelCode}%`,
          })
        }
        // 模型名称
        if (this.formData.modelName) {
          this.filterExpressions.push({
            dataType: 'str',
            name: 'modelName',
            op: 'like',
            stringValue: `%${this.formData.modelName}%`,
          })
        }
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.main-modal {
  :global(.ant-calendar-picker) {
    width: 100%;
  }
}
</style>
