<template>
  <el-form
    inline
    :model="form"
    ref="form"
    class="form-wrap"
    label-width="100px">
    <div class="table-box">
      <el-form-item
        verify
        label="名称"
        prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
    </div>

    <div class="dialog-btn-wrap">
      <el-button
        @click="submit"
        type="primary">
        保存
      </el-button>
    </div>
  </el-form>
</template>
<script>
import { addTicketSpot, updateTicketSpot } from '@/api/ticketSpot'
export default {
  props: {
    dialogType: {
      type: String,
      default: ''
    },
    currentItem: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      visible: false,
      form: {
        name: ''
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleClose () {
      this.$parent.handleClose()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.dialogType === 'add') {
            this.addTicketSpot()
          } else {
            this.updateTicketSpot()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addTicketSpot () {
      addTicketSpot(this.form).then(res => {
        this.$message.success('添加成功')
        this.reset()
        this.handleClose()
        this.$emit('updateList')
      })
    },
    updateTicketSpot () {
      updateTicketSpot(this.form).then(res => {
        this.$message.success('保存成功')
        this.reset()
        this.handleClose()
        this.$emit('updateList')
      })
    }
  },
  watch: {
    visible (val) {
      // 重置表单
      this.reset()
      // 初始化编辑数据
      if (val && this.dialogType === 'edit' && this.currentItem) {
        this.form = Object.assign({}, this.form, this.currentItem)
      }
    }
  }
}
</script>
