<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="名字" prop="name">
                      <el-input v-model="formData.name" placeholder="请输入名字" />
                    </el-form-item>
                    <el-form-item label="父编号" prop="parentId">
                      <TreeSelect
                          v-model="formData.parentId"
                          :options="categoryTree"
                          :normalizer="normalizer"
                          placeholder="请选择父编号"
                      />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as CategoryApi from '@/api/infra/demo'
    import TreeSelect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    export default {
    name: "CategoryForm",
    components: {
                  TreeSelect,
            },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            name: undefined,
                            parentId: undefined,
        },
        // 表单校验
        formRules: {
                        name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
                        parentId: [{ required: true, message: '父编号不能为空', trigger: 'blur' }],
        },
                       categoryTree: [], // 树形结构
              };
    },
    methods: {
      /** 打开弹窗 */
     open(id) {
        this.dialogVisible = true;
        this.reset();
        const that = this;
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            CategoryApi.getCategory(id).then(res=>{
              that.formData = res.data;
              that.title = "修改分类";
            })
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增分类";
                    this.getCategoryTree();
      },
      /** 提交按钮 */
      submitForm() {
        this.formLoading = true;
        try {
          const that = this;
          let data = this.formData;
          let validate = false;
          // 校验主表
          this.getRef("formRef").validate(valid => {
            validate = valid;
          });
                  // 所有表单校验通过后方可提交
          if (!validate) {
            return;
          }
          // 修改的提交
          if (data.id) {
                  CategoryApi.updateCategory(data).then(response => {
              that.$modal.msgSuccess("修改成功");
              that.dialogVisible = false;
              that.$emit('success');
            });
            return;
          }
          // 添加的提交
          CategoryApi.createCategory(data).then(response => {
            that.$modal.msgSuccess("新增成功");
            that.dialogVisible = false;
            that.$emit('success');
          });
        }finally {
          this.formLoading = false;
        }
      },
      getRef(refName){
        return this.$refs[refName];
      },
                  /** 获得分类树 */
          getCategoryTree() {
            const that = this;
            that.categoryTree = [];
                  CategoryApi.getCategoryList().then(res=>{
              const root = { id: 0, name: '顶级分类', children: [] };
              root.children = this.handleTree(res.data, 'id', 'parentId')
              that.categoryTree.push(root)
            });
          },
                  /** 转换分类数据结构 */
          normalizer(node) {
            if (node.children && !node.children.length) {
              delete node.children;
            }
                return {
                  id: node.id,
                  label: node.name,
                  children: node.children
                };
          },
      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            name: undefined,
                            parentId: undefined,
        };
        this.resetForm("formRef");
      },
    }
  };
</script>
