<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="640px"
    @close="handleClose">
    <el-form
      inline
      :model="form"
      ref="form"
      class="form-wrap"
      label-width="100px">
      <div class="dialog-info">
        <el-form-item
          style="width:100%"
          verify
          label="标签名称"
          prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          v-if="!isRoot"
          label="父级标签"
          prop="parent_id">
          <el-select
            :disabled="type === 'edit'"
            v-model="form.parent_id"
            filterable>
            <el-option
              v-for="item in parent"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="标签描述"
          prop="description">
          <el-input
            type="textarea"
            v-model="form.description"></el-input>
        </el-form-item>
      </div>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">
        取 消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="handleSubmit">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addTicketTypeTag, updateTicketTypeTag } from '@/api/ticketTypeTag'

const defaultForm = {
  name: '',
  description: '',
  parent_id: 0

}

export default {
  props: {
    parent: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    ticketTypeList () {
      return this.$store.state.app.ticketTypeList
    }
  },
  data () {
    return {
      title: '',
      type: '',
      visible: false,
      loading: false,
      isRoot: false,
      form: {
        ...defaultForm
      },
      parentNode: {}
    }
  },
  methods: {
    show (data, type, isRoot) {
      this.isRoot = isRoot
      this.type = type
      this.title = type === 'edit' ? '编辑票种标签' : '新增票种标签'
      this.visible = true

      this.$nextTick(() => {
        this.reset()
        if (type === 'edit' && data) {
          this.form = Object.assign({}, this.form, JSON.parse(JSON.stringify(data)))
        } else {
          this.form = JSON.parse(JSON.stringify(defaultForm))
          this.form.parent_id = data.id
        }
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form))
          this.loading = true
          if (this.type === 'edit') {
            updateTicketTypeTag(params).then(res => {
              this.$message.success('修改成功')
              this.$emit('update')
              this.handleClose()
            }).finally(() => {
              this.loading = false
            })
          } else {
            addTicketTypeTag(params).then(res => {
              this.$message.success('添加成功')
              this.$emit('update')
              this.handleClose()
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.periodItem {
  display: flex;  margin: 0 10px 10px 0; align-items: center;
}
.form-wrap {
  .priceInput {
    margin: 0 10px;
    .el-input-number {
      width: 160px;
    }
    ::v-deep .el-input {
      width: 100%;
    }
  }
}

</style>
