<template>
  <!-- 随访审核 -->
  <div id="audit-detail">
    <div class="audit-detail_header p-24">
      <div class="left">
        <span>当前审核</span>
        <span class="f-b">【{{ unionName }}】【剩余{{ 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, index)"
        >{{ 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 && !showNote" slot="content">
        <div class="title">{{ btnGroup[curBtn - 1] ? btnGroup[curBtn - 1].text : "" }}</div>
        <div class="content" ref="publicContent">
          <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="reason">
                <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="onCancel">确 定</el-button>
        </div>
      </template>
      <!-- 标签操作 -->
      <template slot="content" v-if="!confirmStatus && showNote">
        <div class="left-back" @click="handleNoteDialog">
          <i class="el-icon-back"></i>
        </div>
        <div class="title">编辑标签</div>
        <div class="content note-dialog" v-if=" tagList.length>0" :style="{'min-height':minHeight}">
          <el-row :key="index" v-for="(item, index) in tagList" class="mb-16">
            <el-col :span="18">
              <el-input
                size="medium"
                v-model="item.note"
                class="tag-input"
                :class="item.isSelect ? '' : 'unselect-tag'"
                placeholder="请输入标签"
              ></el-input>
            </el-col>
            <el-col :span="3">
              <span class="handle-text" style="color: #E02020;" @click="delTag(item,index)">删除</span>
            </el-col>
            <el-col :span="2">
              <span
                class="handle-text"
                style="color: #4E68FF;"
                v-if="index == tagList.length - 1"
                @click="addTag"
              >添加</span>
            </el-col>
          </el-row>
        </div>
        <div class="content" :style="{'min-height':minHeight}" v-else>
          <el-row class="mb-16">
            <el-col :span="18">
              <el-input size="medium" v-model="newNote" class="tag-input" placeholder="请输入标签"></el-input>
            </el-col>
            <el-col :span="3">
              <span class="handle-text" style="color: #E02020;" @click="delTag(item,index)">删除</span>
            </el-col>
            <el-col :span="2">
              <span class="handle-text" style="color: #4E68FF;" @click="addTag">添加</span>
            </el-col>
          </el-row>
        </div>
        <div class="btn">
          <el-button type="primary" :loading="auditLoading" @click="saveTag">
            <span>保 存</span>
          </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="reason">{{ form.reason }}</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 {
      mcCount: 7,
      editStatus: false, //进入的状态是第一次审核还是修改审核,false为第一次,true为修改
      tabActive: "0", //当前随访病例
      formEdit: [
        {
          patientId: "1623190906909835266",
          id: "1623216842623492098",
          formRecordId: "1623216842522828802",
          age: "",
          name: "",
          checkStatus: 1,
          patientFrom: 1,
          unionName: "合肥高新附院",
          unionId: "2",
          reportTime: "",
          riskRank: "",
          formId: "1598197066503389185",
          is_accept: "0",
        },
        {
          name: "djksh",
          contact_phone: "12345678",
          is_accept: "1",
          gas_exam: "15",
          G_17: "6",
        },
      ], //填报数据
      auditLoading: false,
      tagLoading:false,
      form: {
        reason: "",
      }, //审核表单
      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.unionId
    },
    unionName() {
      return this.$route.query.unionName || "--"
    },
  },
  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 //调试注释
      }
      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 {
        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>
