<template>
  <!-- 随访审核 -->
  <div id="audit-detail">
    <div class="audit-detail_header p-24">
      <div class="left">
        <span>当前审核</span>
        <span class="f-b"
          >【{{ medicalCombination }}】【剩余{{ mcCount }}】</span
        >
      </div>
      <div class="right">
        <el-button class="btn" @click="$router.go(-1)">返回</el-button>
      </div>
    </div>
    <div class="table-content p-24">
      <follow-review
        :formDataList="formEdit"
        :tabActive="tabActive"
      ></follow-review>
    </div>
    <div class="choose-handle p-24">
      <span v-if="!editStatus">请选择审核结果:</span>
      <div class="btn_group">
        <el-button
          v-for="(item, index) in btnGroup"
          :key="index"
          type="primary"
          class="p-btn"
          size="medium"
          @click="showDialog(item.value)"
          >{{ item.text }}</el-button
        >
        <span v-if="editStatus" class="op">驳回修改建议:胃镜图片不合规</span>
      </div>
    </div>
    <public-dialog ref="editDialog" @onCancel="onCancel">
      <!-- 修改审核结果 优先触发提示 -->
      <template v-if="!confirmStatus" slot="content">
        <div class="title">温馨提示</div>
        <div class="content">
          <div class="showTips">
            该病例已经审核【驳回修改】，需要改为{{
              btnGroup[curBtn - 1] ? btnGroup[curBtn - 1].text : ""
            }}吗
          </div>
        </div>
        <div class="btn">
          <el-button type="primary" :loading="loading" @click="onCancel"
            >否</el-button
          >
          <el-button type="primary" :loading="loading" @click="editSubmit"
            >是</el-button
          >
        </div>
      </template>
    </public-dialog>
    <public-dialog ref="publicDialog" @onCancel="onCancel">
      <!-- 审核结果回显 -->
      <template v-if="!confirmStatus" slot="content">
        <div class="title">
          {{ btnGroup[curBtn - 1] ? btnGroup[curBtn - 1].text : "" }}
        </div>
        <div class="content">
          <div v-if="curBtn != 1" class="noPass">
            <el-form
              ref="form"
              label-position="top"
              :model="form"
              :rules="rules"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item
                :label="curBtn == 2 ? '不合格原因' : '驳回修改建议'"
                prop="reson"
              >
                <el-input
                  v-model="form.reason"
                  type="textarea"
                  :placeholder="
                    curBtn == 2 ? '请填写不合格原因' : '请填写驳回修改建议'
                  "
                ></el-input>
              </el-form-item>
            </el-form>
            <!-- 标签 -->
            <div class="tag-form" v-loading="tagLoading">
              <el-tag
                :key="index"
                v-for="(item, index) in tagList"
                size="medium"
                :class="item.isSelect ? '' : 'unselect-tag'"
                @click="seleckTag(item, index)"
              >
                <span>{{ item.note }}</span>
              </el-tag>
              <p class="handle-row" @click="showNote = true">编辑标签</p>
            </div>
          </div>
        </div>
        <div class="btn">
          <el-button type="primary" :loading="loading" @click="onSubmit"
            >确 定</el-button
          >
        </div>
      </template>
      <!-- 提交后结果 -->
      <template v-if="confirmStatus" slot="content">
        <div class="title">{{ btnGroup[curBtn - 1].text }}</div>
        <div class="content">
          <div v-if="curBtn == 1" class="pass">该病例审核合格成功！</div>
          <div v-if="curBtn != 1" class="noPass">
            <!-- 不合格和驳回 -->
            <div class="label">
              {{ curBtn == 2 ? "不合格原因" : "驳回修改建议" }}
            </div>
            <div class="reson">{{ form.reson }}</div>
          </div>
        </div>
        <div v-if="curBtn != 1" class="showTips">
          该病例审核{{ curBtn == 2 ? "不合格原因" : "驳回修改建议" }}提交成功！
        </div>
        <div class="btn">
          <el-button
            type="primary"
            @click="nextExample"
            :loading="auditLoading"
          >
            <span v-if="tabActive < this.formEdit.length - 1"
              >确认并进入下一列审核</span
            >
            <span v-else>确 定</span>
          </el-button>
        </div>
      </template>
    </public-dialog>
  </div>
</template>
<script>
import publicDialog from "./components/publicDialog.vue"
import followReview from "./components/followReview.vue"
import { getFollowUnCheckDetail, putFollowCheck } from "@/api/followup"
import { getSysCheckNote, postSysCheckNote } from "@/api/note"
export default {
  components: { publicDialog, followReview },
  data() {
    return {
      medicalCombination: "无锡市人民医院医联体",
      mcCount: 7,
      editStatus: false, //进入的状态是第一次审核还是修改审核,false为第一次,true为修改
      formEdit: {
        name: "djksh",
        contact_phone: "12345678",
        is_accept: "0",
        gas_exam: "15",
        G_17: "5",
      }, //填报数据
      form: {},
      rules: {
        reason: [{ required: true, message: "请填写", trigger: "blur" }],
      },
      btnGroup: [
        {
          text: "合格",
          value: 3,
        },
        {
          text: "不合格",
          value: 4,
        },
        {
          text: "驳回修改",
          value: 2,
        },
      ],
      loading: false,
      curBtn: 0, //点击的按钮
      curComponent: "followReview",
      confirmStatus: false,
      tagList: [
        // {
        //   note: "不合格原因",
        //   isSelect: false,
        // },
        // {
        //   note: "不合格原因不合格原因",
        // },
        // {
        //   note: "不合格原因",
        // },
        // {
        //   note: "不合格原因",
        // },
        // {
        //   note: "不合格原因",
        // },
      ],
      newNote: "",
      minHeight: "170px",
      showNote: false,
    }
  },
  watch: {
    // tagList(){
    //   this.$nextTick(() => {
    //     this.minHeight = this.$refs.publicContent.offsetHeight + "px"
    //   })
    // }
  },
  created() {
    this.getDetail()
  },
  computed: {
    unionId() {
      return this.$route.query.id
    },
  },
  methods: {
    showDialog(val, index) {
      console.log("显示框", val)
      this.curBtn = index + 1
      if (!this.editStatus) {
        if (this.curBtn == 1) {
          // 走接口，保存合格
          this.confirmStatus = true
        } else if (this.curBtn == 2) {
          this.rules.reason[0].message = "请输入不合格原因"
          //获取审核标签
          this.getSysCheckNote()
        } else {
          this.rules.reason[0].message = "请输入驳回修改建议"
        }
        this.$refs.publicDialog.dialogVisible = true
      } else {
        this.$refs.editDialog.dialogVisible = true
      }
    },
    onCancel() {
      this.confirmStatus = false
      if (this.curBtn != 1) {
        this.form = {}
      }
      this.$refs.editDialog.dialogVisible = false
      this.$refs.publicDialog.dialogVisible = false
      // this.$refs.noteDialog.dialogVisible = false
      if (this.$refs.form) {
        this.$refs.form.clearValidate()
      }
    },
    editSubmit() {
      // this.$refs.editDialog.dialogVisible = false
      if (this.curBtn == 1) {
        // 走接口，保存合格
        this.confirmStatus = true
      } else if (this.curBtn == 2) {
        this.rules.reason[0].message = "请输入不合格原因"
      } else {
        this.rules.reason[0].message = "请输入驳回修改建议"
      }
      this.$refs.publicDialog.dialogVisible = true
    },
    nextExample() {
      if (Number(this.tabActive) < this.formEdit.length - 1) {
        this.tabActive = String(Number(this.tabActive) + 1)
      }
      console.log("tab激活", this.tabActive)
      this.onCancel()
    },
    async getDetail() {
      this.loading = true
      let res = await getFollowUnCheckDetail(this.unionId)
      if (res.code == 1) {
        this.formEdit = res.data[0]
      }
      this.loading = false
    },
    //审核
    async handleCheck() {
      this.auditLoading = true
      let params = {
        id: this.formEdit[this.tabActive],
        checkStatus: this.btnGroup[this.curBtn].value,
        checkNote: this.form.reason,
        patientId: this.unionId,
      }
      console.log("审核值", params)
      // let res = await putFollowCheck(params)
      // if(res.code==1){
      //   // this.formEdit=res.data //调试注释
      // }
      this.auditLoading = false
    },
    // 获取审核标签
    async getSysCheckNote() {
      this.tagLoading = true
      let res = await getSysCheckNote()
      if (res.code == 1) {
        this.tagList = res.data
      }
      this.tagLoading = false
    },
    //选择标签
    seleckTag(item, index) {
      this.tagList[index]["isSelect"] = true
      this.form.reason = this.form.reason + item.note + "、"
      this.$forceUpdate()
    },
    //标签保存
    async saveTag() {
      let params = this.tagList.map((e) => e.note)
      let res = await postSysCheckNote(params)
      if (res.code == 1) {
        this.$message.success("保存标签成功")
        this.getSysCheckNote()
        // this.$refs.noteDialog.dialogVisible = false
      }
    },
    //标签添加
    addTag() {
      this.tagList.push({
        note: "",
      })
      this.$forceUpdate()
    },
    //标签删除
    delTag(item, index) {
      this.$confirm("确定要删除该标签吗？", "警告", {
        type: "warning",
      })
        .then(() => {
          this.tagList = this.tagList.filter((e, i) => i != index)
        })
        .catch(() => {})
    },
    //标签对话框关闭
    handleNoteDialog(i) {
      this.showNote = false
      this.getSysCheckNote()
    },
    //   //标签保存对话框操作
    //   noteSave(){
    //     this.$refs.noteDialog.dialogVisible = false
    //     this.getSysCheckNote()
    //   },
    //   //添加、编辑标签
    //   async handleTag(item){
    //     if(!item.note) return
    //     else{
    //       let params = {
    //         note:item.note
    //       }
    //       item.id ? params['id']=item.id : ''
    //       let res = item.id ? await putSysCheckNote(params) : await postSysCheckNote(params)
    //     }
    //   }
  },
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  border-top: none;
  text-align: center;
  .tips {
    font-size: 16px;
  }
}
.p-24 {
  padding: 0 24px;
}
.f-b {
  font-weight: bold;
  font-size: 14px;
}

#audit-detail {
  // padding: 22px 24px;
  .audit-detail_header {
    height: 65px;
    border-bottom: 1px solid #f3f3f3;
    line-height: 65px;
    display: flex;
    justify-content: space-between;
    .right {
      .btn {
        margin-top: 0px;
        width: 80px;
        height: 32px;
        border-radius: 4px;
      }
    }
  }
  .choose-handle {
    width: 100%;
    height: 72px;
    line-height: 72px;
    font-size: 14px;
    font-family: AlibabaPuHuiTiM;
    color: #333333;
    box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    display: flex;
    .btn_group {
      margin-left: 24px;
      ::v-deep .p-btn {
        width: 100px;
        border-radius: 4px;
      }
    }
  }
}
.title {
  height: 26px;
  font-size: 22px;
  font-family: AlibabaPuHuiTiM;
  color: rgba(0, 0, 0, 0.8);
  line-height: 26px;
}
.content {
  margin: 28px 23px;
  .pass {
    font-size: 18px;
    font-family: AlibabaPuHuiTiM;
    color: #30b9a6;
    letter-spacing: 1px;
  }
}
.btn {
  margin-top: 20px;
  .el-button {
    // width: 100px;
    height: 32px;
  }
}
::v-deep .el-form-item__label {
  width: 100%;
  text-align: left;
}
.reason {
  margin-top: 20px;
}
.showTips {
  color: #4e68ff;
}
.op {
  font-size: 14px;
  font-family: AlibabaPuHuiTiR;
  color: #fa6400;
  margin-left: 20px;
}
.table-content {
  padding-top: 20px;
  padding-bottom: 20px;
}
.tag-form {
  text-align: initial;

  ::v-deep {
    .el-tag {
      margin: 0px 12px 12px 0px;
      cursor: pointer;
      border-color: #4e68ff;
      span {
        display: inline-block;
      }
    }
    .unselect-tag {
      color: #353a45;
      border-color: #edf0ff;
    }
  }
  .handle-row {
    color: #4e68ff;
    text-decoration: underline;
    text-align: right;
    cursor: pointer;
  }
}
.note-dialog {
  ::v-deep {
    .tag-input {
      .el-input__inner {
        background-color: rgba(78, 104, 255, 0.06);
        border-radius: 2px;
      }
    }
    .handle-text {
      line-height: 36px;
      cursor: pointer;
    }
  }
}
.left-back {
  position: absolute;
  padding: 7px 5px;
  top: 10px;
  font-size: 22px;
  text-align: left;
  cursor: pointer;
}
::v-deep {
  .el-dialog__headerbtn {
    font-weight: bold;
    font-size: 22px;
  }
}
</style>
