<template>
  <el-form class="form" ref="form" v-bind="{ labelWidth: '6em', size: 'small' }" :model="formData" inline>
    <el-form-item label="站点名称" prop="siteName">
      <el-input v-model="formData.siteName" placeholder="请输入站点名称" clearable />
    </el-form-item>
    <el-form-item label="站点地址" prop="siteAddress">
      <el-input v-model="formData.siteAddress" placeholder="请输入站点地址" clearable />
    </el-form-item>
    <el-form-item label="SN序列号" prop="deviceSn">
      <el-input v-model="formData.deviceSn" placeholder="请输入SN序列号" clearable />
    </el-form-item>
    <el-form-item label="设备类型" prop="deviceType">
      <el-select v-model="formData.deviceType" filterable placeholder="请选择设备类型" clearable>
        <el-option v-for="item in types" :key="item.id" :label="item.deviceTypeName" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="排水分区" prop="partitionId">
      <el-select v-model="formData.partitionId" placeholder="请选择排水分区" filterable clearable>
        <el-option v-for="item in sections" :key="item.id" :label="item.partitionName" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="监测分组" prop="groupById">
      <el-select v-model="formData.groupById" placeholder="请选择监测分组" filterable clearable>
        <el-option v-for="item in teams" :key="item.id" :label="item.groupByName" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        :loading="loading.query"
        :disabled="loading.query"
        @click="$emit('query', { ...formData })"
        icon="el-icon-search"
      >
        查询
      </el-button>
      <el-button
        type="primary"
        :loading="loading.add"
        :disabled="loading.add"
        @click="$emit('add')"
        icon="el-icon-plus"
      >
        新增
      </el-button>
      <el-button
        type="primary"
        :loading="loading.update"
        :disabled="loading.update || ids.length !== 1"
        @click="$emit('update', selected[0])"
        icon="el-icon-edit"
      >
        修改
      </el-button>
      <el-button
        type="danger"
        :loading="loading.delete"
        :disabled="loading.delete || !ids.length"
        @click="$emit('delete', ids.join())"
        icon="el-icon-delete"
      >
        删除
      </el-button>
      <el-button
        :type="`${on ? 'success' : 'danger'}`"
        :loading="loading.enable"
        :disabled="loading.enable || !ids.length || !valid.length"
        @click="$emit('enable', on, ids.join())"
        :icon="`${on ? 'el-icon-circle-check' : 'el-icon-remove-outline'}`"
      >
        {{ `${on ? '启' : '停'}` }}用
      </el-button>
      <el-button
        type="primary"
        :loading="loading.dismount"
        :disabled="loading.dismount || ids.length !== 1"
        @click="$emit('dismount')"
        icon="el-icon-scissors"
      >
        拆卸
      </el-button>
      <el-button
        type="primary"
        :loading="loading.export"
        :disabled="loading.export || !ids.length"
        @click="$emit('export', ids)"
        icon="el-icon-download"
      >
        导出
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
import { IPoint, ISection, ITeam, IDeviceType } from '../api/common'
export interface ILoading {
  query: boolean
  add: boolean
  update: boolean
  delete: boolean
  enable: boolean
  dismount: boolean
  export: boolean
}
export type IQuery = Partial<IPoint> & { partitionId?: number; groupById?: number }

@Component({ name: 'QueryForm' })
export default class QueryForm extends Vue {
  @Prop({ type: Object, default: () => ({}) }) loading!: ILoading
  @Prop({ type: Array, default: () => [] }) selected!: IPoint[]
  @Prop({ type: Array, default: () => [] }) types!: IDeviceType[]
  @Prop({ type: Array, default: () => [] }) teams!: ITeam[]
  @Prop({ type: Array, default: () => [] }) sections!: ISection[]

  get valid() {
    return this.selected.filter((item) => !!item.deviceId)
  }

  get on() {
    return this.valid.some((item) => item.siteState !== 1)
  }

  formData: IQuery = {}

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