<template>
  <el-dialog
    :title=" '新建类别'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="类别名称" prop="categoryName">
        <el-input v-model="dataForm.categoryName" placeholder="类别名称"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui"
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          categoryName: '',
        },
        dataRule: {
          categoryName: [
            { required: true, message: '类别名称不能为空', trigger: 'blur' }
          ]
        },
        tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
      }
    },
    methods: {
      init () {
       
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/category/save`),
              method: 'post',
              data: this.$http.adornData({
                'categoryId': undefined,
                'categoryName': this.dataForm.categoryName,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                Message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                Message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
