<template>
  <div>
    <el-form ref="form" size="mini"  label-width="80px">
      <el-form-item label="分类名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="父级分类">
        <el-select v-model="form.parent_id" placeholder="请选择活动区域">
          <el-option
            v-for="item in cates"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加分类</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {postMethodCommon} from '@/api/common'
export default {
  name:"AddForm",
  props:['id'],
  data(){
    return {
      cates:[],
      form:{
        name:"",
        parent_id:0,
      },
    }
  },
  created(){
    
    this.getCates();
  },
  methods:{
    getCate(){
      postMethodCommon("/CimApiDescCategory/GetById?Id="+this.id).then(res=>{
        if(res.code == 200){
          this.form = res.result;
        }
      })
    },
    getCates(){
      postMethodCommon("/CimApiDescCategory/LoadTree").then(res=>{
        if(res.code == 200){
          this.cates = res.data;
          this.cates.unshift({
            id:"0",
            name:"无上级分类"
          })
          this.getCate();
        }
      })
    },
    onSubmit(){
      if(this.form.parent_id == '0'){
        this.form.parent_id = 0;
      }
      postMethodCommon("/CimApiDescCategory/AddOrUpdate",this.form).then(res=>{
        
        if(res.code == 200){
          this.$message({
            type:"success",
            message:"操作成功",
          })
          this.$emit("close");
        }
      })
    }
  }
}
</script>