<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="60px" label-position="left">
      <el-form-item label="名称" prop="uo_name">
        <el-input v-model="dialogForm.uo_name"></el-input>
      </el-form-item>
      <el-form-item label="序号" prop="uo_sort">
        <el-input v-model="dialogForm.uo_sort" type="number" :min="0"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { orgAdd, orgEdit, orgDetail } from "../api";
export default {
  data() {
    return {
      dialogForm: {
        uo_name: "",
        uo_sort: "",
        uo_attrib: "2",
        uo_pid: "",
        // uo_level: "0",
      },
      rules_check: {
        uo_name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        uo_sort: [{ required: true, message: '请选择序号', trigger: 'blur' }],
      },
    }
  },
  props: ["operateType", "currentId"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (this.operateType == 1) {
          this.resetForm();
        } else if (this.operateType == 2) {
          this.initDetailInfo(val);
        }
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();
    },
    async initDetailInfo(id) {
      let res = await orgDetail({ uo_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }
    },
    commitData() {
      if (this.operateType == 1) {
        this.dialogForm.uo_pid = this.currentId;
      }
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          let data = {
            issubmit: '1',
            user_org: this.dialogForm,
          };
          let obj = {};
          if (this.operateType == 1) {
            obj = await orgAdd(data);
          } else if (this.operateType == 2) {
            data = Object.assign({ uo_id: this.currentId }, data);
            obj = await orgEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1, 1);
          }
        }
      })
    }
  }
}
</script>

<style>
</style>