<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="500px" 
    append-to-body 
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-form-item label="上级分类" prop="parentId">
        <attach-classify-select v-model="form.parentId" check-strictly/>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入分类名称" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="form.sort" placeholder="请输入排序" style="width: 100%;" controls-position="right" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAttachClassify, updateAttachClassify, getAttachClassify } from "@/api/bst/attachClassify"
import AttachClassifySelect from '@/components/Business/AttachClassify/AttachClassifySelect.vue'

export default {
  name: "AttachClassifyEditDialog",
  components: {
    AttachClassifySelect
  },
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: String,
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级分类不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.id ? '修改分类' : '新增分类'
    },
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  methods: {
    /** 获取详细信息 */
    getInfo(id) {
      this.loading = true
      getAttachClassify(id).then(response => {
        this.form = response.data
      }).finally(() => {
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        parentId: null,
        sort: 1,
        ...this.initData
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          const submitFunc = this.form.id ? updateAttachClassify : addAttachClassify
          submitFunc(this.form).then(() => {
            this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功")
            this.$emit('success')
            this.handleClose()
          }).finally(() => {
            this.submitLoading = false
          })
        }
      })
    },
    // 取消按钮
    handleClose() {
      this.$emit('update:show', false)
    },
    // 打开事件
    handleOpen() {
      if (this.id) {
        this.getInfo(this.id)
      } else {
        this.reset()
      }
    }
  }
}
</script> 