<template>
  <div
    id="publicContent"
    :class="[
      activeName == 'index0' && formClass ? formClass : '',
      $route.path === '/screening/draft' ? 'p_padding' : '',
    ]"
  >
    <div v-if="formTabs && formTabs.length > 0" style="position: relative">
      <el-button
        v-if="!disabled"
        class="draftButton"
        :loading="loading"
        :disabled="!infoCompelete"
        @click="onSubmit(formType)"
        >{{ formType == 1 ? "临时保存" : "返回" }}</el-button
      >
      <el-tabs
        v-model="activeName"
        v-loading="fromLoading"
        type="card"
        :style="{ width: formType == 1 ? '100%' : 'calc(100% - 200px)' }"
        class="publicTab"
        @tab-click="handleTabClick"
      >
        <el-tab-pane
          v-for="(form, index) in formTabs"
          :key="form.id"
          :label="form.label"
          :name="'index' + index"
          :disabled="tabDisabled"
        >
          <!-- disabled -->
          <transition mode="out-in" name="fade-transform">
            <div v-show="activeName === 'index' + index">
              <template v-if="form.formId">
                <form-tab
                  ref="formTabs"
                  :patient-id="patientId"
                  :patient-standby-id="patientStandbyId"
                  :newform-record-id="newformRecordId"
                  :form="form"
                  :disabled="disabled"
                  :operation="operation"
                  :isDraft="isDraft"
                  :active-name="activeName"
                  contrast
                  :form-initial="formInitial"
                  :survival-flag="survivalFlag"
                  @setFormJson="setFormJson"
                  @handleConfirm="handleConfirm"
                  @onPrev="onPrev"
                  @temporaryConfirm="temporaryConfirm"
                ></form-tab>
              </template>
            </div>
          </transition>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-empty v-else description="暂无数据"></el-empty>
    <public-dialog ref="showDialog" :show-close="true">
      <!-- 保存草稿 -->
      <template v-if="dialogType == 'draft'" slot="content">
        <div class="title center">已保存至草稿箱!</div>
        <div class="content center">
          <div class="img">
            <img src="~@/assets/img/DataCenter/draft.png" alt />
          </div>
          <div class="showTips">您可以在【草稿箱】页面找到该数据</div>
        </div>
        <div class="btn">
          <el-button type="primary" :loading="loading" @click="continueSubmit"
            >继续添加</el-button
          >
        </div>
      </template>
      <!-- 保存提交 -->
      <template v-if="dialogType == 'submit'" slot="content">
        <div class="title center">提交成功!</div>
        <div class="content center">
          <div class="img">
            <img src="~@/assets/img/DataCenter/submit.png" alt />
          </div>
          <div class="showTips">您可以在【筛查查询】页面查询到该数据</div>
        </div>
        <div class="btn">
          <el-button
            type="primary"
            :loading="loading"
            class="textBtn"
            @click="viewJump"
            >查看</el-button
          >
          <el-button type="primary" :loading="loading" @click="continueSubmit"
            >继续添加</el-button
          >
        </div>
      </template>
    </public-dialog>
  </div>
</template>

<script>
import mixin from "./mixin"
import CustomForm from "@/components/FormComponents/CustomForm/index"
import publicDialog from "../../audit-detail/components/publicDialog.vue"
import { getPatientDetail } from "@/api/patient.js"

export default {
  name: "ConfigForms",
  components: { publicDialog },
  mixins: [mixin],
  props: {
    disabled: Boolean,
    tabDisabled: { type: Boolean, default: true },
    formType: String,
    patientId: String,
    formClass: String,
    operation: String,
    isDraft: String,
  },
  data() {
    return {
      // showIndex:false,
      // survivalFlag:false
      dialogType: "",
      loading: false,
      infoCompelete: false,
      formInitial: {}, // 第四步的数据
    }
  },
  computed: {
    survivalFlag() {
      if (this.activeName == "index0" && this.formClass) {
        return true
      } else {
        return false
      }
    },
    showIndex() {
      if (this.activeName == "index0" && this.formClass) {
        return false
      } else {
        return true
      }
    },
  },
  watch: {
    activeName(val) {
      if (!this.disabled) {
        if (!["index0", "index1"].includes(val)) {
          this.infoCompelete = true
        } else {
          this.infoCompelete = false
        }
      }
      if (val == "index1") {
        // 第二步问卷调查
        this.$nextTick(() => {
          let index1Data = sessionStorage.getItem("index1Data")
          if (index1Data) {
            let { birthday } = JSON.parse(index1Data)
            let date = new Date(String(birthday).replace(/-/g, "/"))
            let d = new Date()
            let age =
              d.getFullYear() -
              date.getFullYear() -
              (d.getMonth() < date.getMonth() ||
              (d.getMonth() == date.getMonth() && d.getDate() < date.getDate())
                ? 1
                : 0)
            //!
            this.formInitial = {
              birthday,
              age,
            }
          }
        })
      } else {
        this.formInitial = false
      }

      if (val == "index3") {
        // 第四步风险评估
        this.$nextTick(() => {
          getPatientDetail({
            formId: this.formId, // 每个大表单的id
            patientId: this.patientId || this.patientStandbyId,
            // formRecordId: this.newformRecordId,
          })
            .then((res) => {
              const data = res.data.data
              this.formInitial = data
            })
            .finally(() => {
              this.loading = false
            })
          this.$nextTick(() => {
            console.log("走了")
            //!
            // this.formInitial = {
            //   risk_rank: "中危",
            //   risk_score: "5分",
            //   screening_advise: "立刻住院",
            // }
          })
        })
      }
    },
  },
  created() {
    // 字典formType 1 筛查表单 2随访表单
    // console.log('回显2',this.formEdit)
    this.getCurrentFormByType(this.formType)
    console.log(this.$route.path)
    this.infoCompelete = this.$route.path == "/followupentry" ? true : false
  },
  methods: {
    handleConfirm(data, done, cb) {
      this.addPatient(data, done, cb)
      this.$emit("addMethods", {
        activeName: this.activeName,
        form: data.data,
      })
    },
    temporaryConfirm(data, done, cb) {
      this.addPatient(data, done, cb, () => {
        console.log("临时保存")
        this.$refs.showDialog.dialogVisible = true
        this.loading = false
      })
    },
    onPrev(done) {
      this.prevTab(done)
    },
    onSubmit(type) {
      // this.loading = true
      if (type == 1) {
        // 临时保存草稿
        this.dialogType = "draft"
        // 保存逻辑处理
        // !获取当前tab的formTab
        let formTab = this.$refs.formTabs.filter(
          (e) => e.form.formId == this.formId
        )[0]
        formTab.temporarySave()
        // console.log(formTab)
      } else if (type == 3) {
        //提交
        this.dialogType = "submit"
        this.$refs.showDialog.dialogVisible = true
        this.loading = false
      } else {
        // 返回
        this.$router.back()
      }
    },
    continueSubmit() {
      sessionStorage.removeItem("index1Data")
      this.activeName = "index0"
      // 清空表单
      for (let i = 0; i < this.$refs.formTabs.length; i++) {
        this.$refs.formTabs[i].resetForm()
        this.$refs.formTabs[i].initForm()
      }
      // 清空红字:不符合筛查条件
      this.$refs.showDialog.dialogVisible = false
      //判断路径
      if (this.$route.path != "/screening/add") {
        this.$router.push("/screening/add")
      }
    },
    // 去查看跳转
    viewJump() {
      console.log(this.patientId)
      if (this.$route.path == "/screening/index") {
        this.$store.commit("table/setRefreshFlag", 1)
      } else {
        this.$router.push({
          path: "/screening/index",
          query: {
            patientId: this.patientId || this.patientStandbyId,
          },
        })
      }
      this.$refs.showDialog.dialogVisible = false
      // alert("跳转")
    },
  },
  provide() {
    return {
      configForms: this,
    }
  },
}
</script>

<style lang="scss" scoped>
#publicContent {
  position: relative;
  padding: 32px 24px 32px;
  .draftButton {
    position: absolute;
    top: 0px;
    right: 40px;
    z-index: 999;
    border-radius: 4px;
    border: 1px solid #4e68ff;
    color: #4e68ff;
  }
  .content {
    img {
      width: 248px;
      height: 180px;
    }
  }
}
#publicContent.p_padding {
  padding: 10px 24px 32px;
}
::v-deep .el-dialog__body {
  .title {
    font-size: 22px;
    font-family: AlibabaPuHuiTiM;
    color: rgba(0, 0, 0, 0.8);
    margin-bottom: 20px;
  }
  .showTips {
    font-size: 16px;
    font-family: AlibabaPuHuiTiR;
    color: rgba(0, 0, 0, 0.8);
    margin-bottom: 20px;
  }
  .btn {
    .el-button {
      width: 100px;
      height: 32px;
    }
  }
}
.publicTab {
  // width: calc(100%);
  margin-top: 10px;
}
::v-deep {
  .el-button.is-disabled {
    color: #c0c4cc !important;
    cursor: not-allowed;
    background-image: none;
    background-color: #fff;
    border-color: #ebeef5 !important;
  }
  .el-tabs__item.is-disabled {
    color: #252c49;
  }
  .el-tabs--card > .el-tabs__header {
    width: calc(100% - 100px);
    border-bottom: none;
  }
  .el-tabs--card > .el-tabs__header .el-tabs__nav {
    border: none;
  }
  .el-tabs--card > .el-tabs__header .el-tabs__item {
    min-width: 116px;
    text-align: center;
    background: #f0f1fa;
    font-size: 14px;
    border: none;
  }
  .el-tabs--card > .el-tabs__header .el-tabs__item.is-active {
    background: #4e68ff;
    color: #fff;
  }
}
.my-form {
  overflow-x: scroll;
}
.follow-form {
  .publicTab {
    margin-bottom: 10px;
    ::v-deep {
      .el-collapse-item__header {
        border-width: 1px 0px 0px;
      }
      .row24 {
        border-top: 1px solid #cccccc;
      }
      .custom-form-item {
        border-width: 1px 0px 0px;
      }
      .el-collapse-item {
        margin-bottom: 20px;
        border-radius: 4px;
        overflow: hidden;
        border: 1px solid #cccccc;
      }
      .el-collapse-item__wrap {
        border: none;
        .el-collapse {
          margin: 10px 10px 20px;
        }
      }
      // .row24.el-col-lg-12:nth-child(2n) {
      //   border-width: 0px 0px 0px 1px;
      // }
      .el-form-item {
        .el-form-item__label {
          text-align: left;
          background: #fafafa;
          font-size: 14px;
          color: #333333;
          border-right: 1px solid #cccccc;
          // width: 100px !important;
          white-space: nowrap;
          padding: 18px 10px;
          margin-right: 20px;
          text-align: center;
        }
      }
      .el-form-item {
        padding: 0px;
      }
    }
  }
}
.textBtn {
  width: 100px;
  height: 32px;
  background: #ffffff;
  border: 1px solid #4e68ff;
  color: #4e68ff;
}
</style>