<!-- 用户编辑弹窗 -->
<template>
  <el-dialog
    class="ele-dialog-form"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="450px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="50px">
      <el-row>
        <el-col :span="24">
          <el-form-item
            label="父级"
            prop="parentId">
            <ele-tree-select
              clearable
              ref="projectRef"
              filterable
              :data="projectList"
              v-model="form.parentId"
              childrenKey="sonTreeList"
              valueKey="id"
              labelKey="name"
              placeholder="请选择"
              default-expand-all
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="名称"
            prop="name">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.name"
              placeholder="请输入分类名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="备注"
            prop="remark">
            <el-input
              type="textarea"
              clearable
              :maxlength="20"
              v-model="form.remark"
            />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template v-slot:footer>
      <el-button type="primary" :loading="loading" @click="save">保存</el-button>
      <el-button @click="handleClose">取消</el-button>

    </template>
  </el-dialog>
</template>

<script>
import {getProcessTypeGetByIdAPI, processTypeSaveAPI, processTypeUpdateAPI} from "@/api/bpm/processClass";
import {deepClone} from "@/utils";


export default {
  data() {
    const defaultForm = function () {
      return {
        id: '',
        name: '',
        parentId: '',
        remark: '',
      };
    };
    return {
      defaultForm,
      // 表单数据
      form: {...defaultForm()},
      // 表单验证规则
      rules: {
        name: [{required: true, message: '请输入名称', trigger: 'blur'}],
      },
      visible: false,
      type: '', // add/edit
      loading: false,
    };
  },
  computed: {
    title() {
      switch (this.type) {
        case 'add':
          return '新增';
          break;
        case 'edit':
          return '编辑';
          break;
        default:
          break;
      }
    }
  },
  methods: {
    open(type, row) {
      this.type = type;
      this.visible = true;
      if (type == 'edit') {
        this.getInfo(row.id)
      }
    },
    async getInfo(id) {
      const res = await getProcessTypeGetByIdAPI(id)
      this.form = deepClone(res)
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        if (this.type == 'add') {
          delete this.form.id;
        }
        let API = this.type == 'add' ? processTypeSaveAPI : processTypeUpdateAPI
        API(this.form)
          .then((msg) => {
            this.loading = false;
            this.$message.success('操作成功');
            this.handleClose();
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      });
    },
    restForm() {
      this.form = {...this.defaultForm()};
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.restForm();
      this.visible = false;
    },
  }
};
</script>
<style lang="scss" scoped>

</style>
