<template>
  <el-form class="form" ref="form" v-bind="{ labelWidth: 'auto', size: 'small' }" :model="formData" inline>
    <el-form-item label="关键字" prop="groupByName">
      <el-input v-model="formData.id" placeholder="请输入关键字" size="small" clearable />
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        size="small"
        :loading="loading.query"
        :disabled="loading.query"
        @click="$emit('query', { ...formData })"
        icon="el-icon-search"
      >
        查询
      </el-button>
      <el-button
        type="primary"
        size="small"
        :loading="loading.add"
        :disabled="loading.add"
        @click="$emit('add')"
        icon="el-icon-plus"
      >
        新增
      </el-button>
      <el-button
        type="primary"
        size="small"
        :loading="loading.update"
        :disabled="loading.update || selected.length !== 1"
        @click="$emit('update', selected[0])"
        icon="el-icon-edit"
      >
        修改
      </el-button>
      <el-button
        type="danger"
        size="small"
        :loading="loading.del"
        :disabled="loading.del || !selected.length"
        @click="$emit('del', ids)"
        icon="el-icon-delete"
      >
        删除
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
  import { Vue, Component, Prop } from 'vue-property-decorator'
  import { IFlaw } from '../api/common'

  export interface ILoading {
    query: boolean
    add: boolean
    update: boolean
    del: boolean
  }

  @Component({ name: 'QueryForm' })
  export default class QueryForm extends Vue {
    @Prop({ type: Object, default: () => ({}) }) loading!: ILoading
    @Prop({ type: Array, default: () => [] }) selected!: IFlaw[]

    formData: Partial<IFlaw> = {}

    get ids() {
      return this.selected.map((item) => item.id).join()
    }
  }
</script>
