<template>
  <el-form
    inline
    :model="form"
    ref="form"
    label-width="100px"
  >
    <div class="dialog-info">
      <el-form-item
        class="is-required"
        verify
        label="名称"
        prop="name"
      >
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item
        class="is-required"
        verify
        label="自动叫号触发人数"
        prop="call_trigger_num"
      >
        <el-input v-model="form.call_trigger_num"></el-input>
      </el-form-item>
      <el-form-item
        class="is-required"
        verify
        label="自动叫号单次人数"
        prop="single_call_num"
      >
        <el-input v-model="form.single_call_num"></el-input>
      </el-form-item>
    </div>
    <div class="dialog-btn-wrap">
      <el-button @click="handleClose">
        关闭
      </el-button>
      <el-button
        @click="submit"
        type="primary"
      >
        保存
      </el-button>
    </div>
  </el-form>
</template>

<script>
import { createQueue, updateQueue } from '@/api/ticketType.js'
export default {
  props: {
    type: {
      type: String,
      default: 'new'
    },
    currentItem: {
      type: Object,
      default: () => {}
    },
    groupList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      form: {
        name: '',
        call_trigger_num: 0,
        single_call_num: 0
      }
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let action = this.type === 'new' ? createQueue : updateQueue
          action(this.form).then(res => {
            this.$message.success('操作成功')
            this.handleClose()
            this.$emit('updateList')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  },
  watch: {
    visible (val) {
      if (val) {
        this.reset()
        if (this.type === 'edit') {
          this.form = { ...this.currentItem }
        }
      }
    }
  }
}
</script>
