<template>
  <Popup ref="popup" title="员工" @closed="dialogClosed">
    <el-form :model="form" :rules="rules" ref="form" label-width="130px">
      <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>
    <template slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </template>
  </Popup>
</template>

<script>
import Popup from "@/components/Popup";
import TreeDept from "@/components/TreeDept";
import { getAllHospitalList } from "@/api/admin";
export default {
  components: {
    Popup,
    TreeDept,
  },
  data() {
    return {
      form: this.getFormDefaultValue(),
      rules: {
        wardId: [{ required: true, message: "请选择所属院区", trigger: "change" }],
        deptId: [{ required: true, message: "请选择所属科室", trigger: "change" }],
      },
      wardList: [],
      api: null,
      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 {
        wardId: "",
        deptId: "",
      };
    },
    open({ api, isEdit = false, ...params }) {
      const defaultValue = {};
      this.api = api;
      if (params) {
        for (const key of Object.keys(this.form)) {
          this.form[key] = params[key] || "";
        }
        defaultValue.wardId = params.wardId;
        defaultValue.wardName = params.wardName;
        defaultValue.deptName = params.deptName;
      }
      this.defaultValue = defaultValue;
      this.$refs.popup.open(isEdit);
      this.promise = {};
      return new Promise((resolve, reject) => {
        this.promise.resolve = resolve;
        this.promise.reject = reject;
      });
    },
    close() {
      this.$refs.popup.close();
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 表单校验通过,可以提交数据
          if (this.api) {
            this.api(this.form).then(() => {
              this.promise.resolve();
              this.$refs.popup.close();
            });
          } else {
            this.promise.resolve(this.form);
            this.$refs.popup.close();
          }
        } else {
          // 表单校验失败
          return false;
        }
      });
    },
    dialogClosed() {
      this.form = this.getFormDefaultValue();
      this.$refs.treeDept.clear();
      this.$nextTick(this.$refs.form.clearValidate);
      this.api = null;
      // this.promise.reject();
    },
  },
};
</script>
