<template>
  <Popup ref="popup" title="随访问卷～" @closed="dialogClosed">
    <el-form :model="form" :rules="rules" ref="form" label-width="80px">
      <el-form-item label="问卷名称" prop="name">
        <el-input v-model="form.name" :maxlength="32"></el-input>
      </el-form-item>

      <el-form-item label="应用科室" prop="deptId">
        <TreeDept :defaultName="defaultDeptName" v-model="form.deptId"
          @change="form.labelId = ''" />
      </el-form-item>

      <el-form-item label="应用疾病" prop="labelId">
        <el-select v-model="form.labelId" filterable>
          <el-option v-for="item in diseaseList" :key="item.id" :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </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 { addSurvey, editSurvey, getMedicalLabel, copySurvey } from "@/api/hospital";
export default {
  components: {
    Popup,
    TreeDept,
  },
  data() {
    return {
      form: this.getFormDefaultValue(),
      rules: {
        name: [
          { required: true, message: "请输入问卷名称", trigger: "blur" },
          { max: 32, message: "长度不能超过32个字符", trigger: "blur" },
        ],
        labelId: [{ required: true, message: "请选择应用疾病", trigger: "change" }],
        deptId: [{ required: true, message: "请选择应用疾病", trigger: "change" }],
      },
      diseaseList: [],
      defaultDeptName: "",
    };
  },
  watch: {
    "form.deptId": {
      handler: "loadDisease",
    },
  },
  methods: {
    async loadDisease() {
      const res = await getMedicalLabel({ deptId: this.form.deptId || "" });
      this.diseaseList = res.data || [];
    },
    getFormDefaultValue() {
      return {
        name: "",
        labelId: "",
        deptId: "",
      };
    },
    open(params) {
      if (params) {
        for (const key of [
          ...Object.keys(this.form),
          "id",
          "surveyId",
          "clientId",
          "deptName",
          "labelName",
          "status",
        ]) {
          this.form[key] = params[key];
          this.defaultDeptName = params.deptName || "";
        }
      }
      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 editSurvey(this.form);
              this.$message.success("保存成功");
            } else if (this.form.surveyId) {
              //没有id但有surveyId，那就是复制
              const res = await copySurvey(this.form);
              this.form.surveyId = res.data.surveyId;
              this.form.id = res.data.id;
              this.$message.success("复制成功");
            } else {
              // 新增接口
              const res = await addSurvey(this.form);
              this.form.surveyId = res.data.surveyId;
              this.form.id = res.data.id;
              this.$message.success("新增成功");
            }
            this.$router.push({
              name: "DesignSurvey",
              query: { id: this.form.id, surveyId: this.form.surveyId },
            });
          } finally {
            this.loading = false;
          }
          this.$refs.popup.close();
          this.promise.resolve();
        } else {
          // 表单校验失败
          return false;
        }
      });
    },
    dialogClosed() {
      this.form = this.getFormDefaultValue();
      this.$nextTick(this.$refs.form.clearValidate);
      // this.promise.reject();
    },
  },
};
</script>
