<template>
  <el-dialog
    :title="type==='edit'?'编辑':'添加'"
    :visible.sync="visible"
    width="30%"
    @close="handleClose">
    <el-form
      style="padding:20px"
      inline
      :model="form"
      ref="form"
      label-width="100px">
      <el-form-item
        verify
        class="is-required"
        label="名称"
        prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="visible= false">取 消</el-button>
      <el-button
        type="primary"
        @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { updatePayChannel, addPayChannel } from '@/api/payChannel'
const defaultForm = {
  name: ''
}
export default {
  data () {
    return {
      type: '',
      visible: false,
      form: {
        ...defaultForm
      }
    }
  },
  methods: {
    show (type, data) {
      this.type = type
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (type === 'edit' && data) {
          this.form = Object.assign({}, this.form, JSON.parse(JSON.stringify(data)))
        } else {
          this.form = JSON.parse(JSON.stringify(defaultForm))
        }
      })
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.type === 'edit') {
            updatePayChannel(this.form).then(res => {
              this.$message.success('操作成功')
              this.handleClose()
              this.$emit('update')
            })
          } else {
            addPayChannel(this.form).then(res => {
              this.$message.success('操作成功')
              this.handleClose()
              this.$emit('update')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
