<template>
  <Popup ref="popup" title="疾病种类" @closed="dialogClosed">
    <el-form :model="form" :rules="rules" ref="form" label-width="130px" v-loading="loading">
      <el-form-item label="所属院区" prop="wardId">
        <el-select v-model="form.wardId" @change="form.deptId = ''">
          <el-option
            v-for="item in wardListOptions"
            :key="item.id"
            :disabled="item.disabled"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科室归属" prop="deptId">
        <TreeDept
          ref="treeDept"
          :autoFetch="false"
          :defaultName="defaultValue.deptName"
          v-model="form.deptId"
        />
      </el-form-item>
      <el-form-item label="疾病名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入" :maxlength="32"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </Popup>
</template>

<script>
import Popup from "@/components/Popup";
import TreeDept from "@/components/TreeDept";
import { addDisease, updateDisease, getAllHospitalList, getTreeHospDept } from "@/api/admin";
export default {
  components: {
    Popup,
    TreeDept,
  },
  data() {
    return {
      form: this.getFormDefaultValue(),
      rules: {
        name: [
          { required: true, message: "请输入疾病名称", trigger: "blur" },
          { max: 32, message: "长度不能超过32个字符", trigger: "blur" },
        ],
        deptId: [{ required: true, message: "请选择科室归属", trigger: "change" }],
        wardId: [{ required: true, message: "请选择所属院区", trigger: "change" }],
      },
      loading: false,
      wardList: [],
      defaultValue: {},
    };
  },
  created() {
    this.loadHospital();
  },
  computed: {
    // 院区如果禁用了，需要回显，但是不可以选择
    wardListOptions() {
      if (!this.defaultValue.wardId) {
        return this.wardList;
      }
      if (this.wardList.find((item) => item.id === this.defaultValue.wardId)) {
        return this.wardList;
      }
      return [
        {
          id: this.defaultValue.wardId,
          name: this.defaultValue.wardName,
          disabled: true,
        },
        ...this.wardList,
      ];
    },
  },
  watch: {
    "form.wardId"() {
      if (this.form.wardId) {
        this.$nextTick(() => {
          this.$refs.treeDept.loadDept({ wardId: this.form.wardId });
        });
      }
    },
  },
  methods: {
    loadHospital() {
      getAllHospitalList().then((res) => {
        this.wardList = res.data;
      });
    },
    getFormDefaultValue() {
      return {
        name: "",
        deptId: "",
        wardId: "",
      };
    },
    open(params) {
      const defaultValue = {};
      if (params) {
        for (const key of [...Object.keys(this.form), "id"]) {
          this.form[key] = params[key];
        }
        defaultValue.wardId = params.wardId;
        defaultValue.wardName = params.wardName;
        defaultValue.deptName = params.deptName;
      }
      this.defaultValue = defaultValue;
      this.$refs.popup.open(params);
      this.promise = {};
      return new Promise((resolve, reject) => {
        this.promise.resolve = resolve;
        this.promise.reject = reject;
      });
    },
    handleDeptLevelChange() {
      this.$refs.form.clearValidate(["parentId", "wardId"]);
    },
    handleCancel() {
      this.$refs.popup.close();
    },
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            if (this.form.id) {
              await updateDisease(this.form);
              this.$message.success("修改成功");
            } else {
              await addDisease(this.form);
              this.$message.success("新增成功");
            }
          } finally {
            this.loading = false;
          }
          this.$refs.popup.close();
          this.promise.resolve();
        } else {
          // 表单校验失败
          return false;
        }
      });
    },
    dialogClosed() {
      this.form = this.getFormDefaultValue();
      this.$refs.treeDept.clear();
      this.$nextTick(this.$refs.form.clearValidate);
      // this.promise.reject();
    },
  },
};
</script>
