<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="50%"
    :visible.sync="showEditFlag"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    append-to-body
    @close="cancel"
    :title="title + '编码'"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="使用对象" prop="useObjList">
            <tree ref="treeRef"></tree>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="创建人" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        确认
      </el-button>
    </template>
  </ele-modal>
</template>
  
  <script>
const defaultForm = {
  name: '', //名称
  parentId: '', //父id
  sort: '',
  status: '', //状态
  type: 2, //类型
  useObjList: [], //使用对象信息集合
  remark: '' //备注
};
import { save } from '@/api/businessCode';
import tree from './tree.vue';
export default {
  data() {
    return {
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {},
      // 提交状态
      loading: false,
      showEditFlag: false,
      title: '',
      type: '',
      rules: {
        name: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  props: {
    parentId: ''
  },
  components: {
    tree
  },
  created() {},
  methods: {
    async open(type, row) {
      this.title = type === 'edit' ? '编辑' : '新增';
      this.showEditFlag = true;
      this.type = type;
      if (type == 'add') {
        this.form.parentId = this.parentId;
      } else {
        this.form = JSON.parse(JSON.stringify(row));
      }
      console.log(this.form, 'row');

      this.$nextTick(() => {
        this.$refs.treeRef.init(this.form.useObjList || []);
      });
    },

    /* 保存编辑 */
    save() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }

        if (this.type != 'edit') {
          delete this.form.id;
        }
        this.form.useObjList = this.$refs.treeRef.getList();
        this.loading = true;
        save(this.form)
          .then((msg) => {
            this.loading = false;
            this.cancel();
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
          });
      });
    },

    cancel() {
      this.form = { ...defaultForm };
      this.$refs.form.clearValidate();
      this.showEditFlag = false;
    }
  }
};
</script>
  <style scoped lang="scss">
.aaa {
  width: 100%;

  ::v-deep .upload-demo {
    width: 100%;

    .el-upload--text {
      width: 100%;

      button {
        width: 100%;
        background: #ffffff;
        border: 1px solid #dbdbdb;
        border-radius: 5px;
      }
    }

    .el-upload-list {
      transform: translate(10px, -39px);
      position: absolute;
    }
  }
}
</style>
  