<template>
  <el-dialog
    :append-to-body="true"
    :visible.sync="dialog"
    :title="isAdd ? '新增营业部' : '编辑营业部'"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      size="small"
      label-width="100px"
    >
      <el-form-item
        v-if="isAdd"
        style="margin-bottom: 10px;"
        label="营业部编号"
      >
        <el-input v-model="form.id" size="mini" style="width: 300px;" />
      </el-form-item>
      <el-form-item label="营业部名称">
        <el-input v-model="form.name" size="mini" style="width: 300px;" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" size="small" @click="cancel">取消</el-button>
      <el-button
        :loading="loading"
        size="small"
        type="primary"
        @click="doSubmit"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { add, edit, getDepts } from "@/api/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children:
            node.children && node.children.length > 0 ? node.children : 0
        };
      },
      loading: false,
      dialog: false,
      depts: [],
      form: {
        id: null,
        name: "",
        pid: 1,
        status: 1
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }]
      }
    };
  },
  methods: {
    close() {
      this.resetForm();
    },
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      // this.$refs['form'].validate((valid) => {
      //   if (valid) {
      //     if (this.form.pid !== undefined) {
      //       this.loading = true
      //       if (this.isAdd) {
      //         this.doAdd()
      //       } else this.doEdit()
      //     } else {
      //       this.$message({
      //         message: '上级部门不能为空',
      //         type: 'warning'
      //       })
      //     }
      //   }
      // })
      if (this.isAdd) {
        this.doAdd();
      } else this.doEdit();
    },
    doAdd() {
      // console.log(this.form)
      let isNum = /^\d+$/.test(this.form.id);
      if (!this.form.id.trim()) {
        return this.$message.error("请输入营业部编号");
      }
      if (!isNum) {
        return this.$message.error("营业部编号必须为纯数字");
      }
      if (this.form.id.length > 10) {
        return this.$message.error("营业部编号长度不得超过10个字符");
      }
      if (!this.form.name.trim()) {
        return this.$message.error("请输入营业部名称");
      }
      if (this.form.name.length > 20) {
        return this.$message.error("营业部名称长度不得超过20个字符");
      }
      this.loading = true;
      const params = {};
      params.id = this.form.id;
      params.name = this.form.name;
      params.status = 1;
      add(params)
        .then(res => {
          if (res.code === 0) {
            this.resetForm();
            this.$notify({
              title: "添加成功",
              type: "success",
              duration: 2500
            });
            this.$parent.init();
          } else {
            this.$notify({
              title: res.message,
              type: "error",
              duration: 2500
            });
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    doEdit() {
      // console.log(this.form)
      if (!this.form.name.trim()) {
        return this.$message.error("请输入营业部名称");
      }
      if (this.form.name.length > 20) {
        return this.$message.error("营业部名称长度不得超过20个字符");
      }
      this.loading = true;
      const params = {};
      params.id = this.form.id;
      params.name = this.form.name;
      params.pid = this.form.pid;
      params.status = this.form.status;
      edit(params)
        .then(res => {
          if (res.code === 0) {
            this.resetForm();
            this.$notify({
              title: "修改成功",
              type: "success",
              duration: 2500
            });
            this.$parent.init();
          } else {
            this.$notify({
              title: res.message,
              type: "error",
              duration: 2500
            });
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    resetForm() {
      this.dialog = false;
      this.$refs["form"].resetFields();
      this.form = {
        id: "",
        name: "",
        pid: 1,
        status: 1
      };
    },
    getDepts() {
      getDepts({ status: 1 }).then(res => {
        this.depts = res.data;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__title {
  font-weight: 700;
}
</style>
