<template>
  <view class="Scanning_Result">
    <view class="Scanning_top" v-if="res.status == 200">
      <view class="Scanning_top_icon">
        <text class="cubeic-ok icon_transport transport-duigou"></text>
      </view>
      <view class="Scanning_top_text">
        <view class="text1"> 操作成功 </view>
        <view class="success_tips" v-if="type == 'inspect' || type == 'patientTransport'">
          (请您评估患者的意识状况、生命体征、配合程度，如有异常请及时联系护士！)
        </view>
      </view>
    </view>

    <view class="Scanning_top" v-if="res.status != 200">
      <view class="Scanning_top_icon">
        <text class="cubeic-close icon_transport transport-shibai"></text>
      </view>
      <view class="Scanning_top_text">
        <view class="text1"> 操作失败 </view>
      </view>
    </view>

    <view class="Scanning_cont" v-if="
        (res.status == 200 && type == 'specimenPlan') ||
        (res.status == 200 && type == 'specimen')
      ">
      <view> 标本类型 : {{ infoDATA.stype.name || "-" }} </view>
      <view> 标本编号 : {{ infoDATA.scode || "-" }} </view>
      <view> 患者姓名 : {{ infoDATA.patientName || "-" }} </view>
      <view> 已扫描 : {{ speNum }} </view>
    </view>

    <view class="Scanning_cont" v-if="
        (res.status == 200 && type == 'inspect') || type == 'patientTransport'
      ">
      <view v-if="res.patient"> 患者姓名 : {{ res.patient || "-" }} </view>
      <view v-if="res.patientCode">
        患者编号 : {{ res.patientCode || "-" }}
      </view>
      <view v-if="res.deptName">
        住院科室 : {{ res.deptName !== "undefined" ? res.deptName : "-" }}
      </view>
      <view v-if="res.bedNum">
        床号 : {{ res.bedNum !== "undefined" ? res.bedNum : "-" }}
      </view>
      <view v-if="res.data">
        检查项目 :
        <text v-for="(data, i) in res.data" :key="i">{{
          data.inspectName
        }}</text>
      </view>
      <view v-if="
          res.status == 200 &&
          infoType != 'ins-zxz-1' &&
          infoType != 'ins-dsd-1' &&
          infoType != 'ins-ddd-1' &&
          infoType != 'trans-ddd-1' &&
          infoType != 'trans-dsd-1'
        ">
        {{ res.msg || "" }}
      </view>
      <view v-if="infoType == 'trans-dsd-1' && res.status == 200">
        工单已完成
      </view>
      <view v-if="infoType == 'ins-dsd-1' && res.status == 200">
        工单已完成。
      </view>
    </view>
    <view class="Scanning_cont" v-if="res.status == 200 && (type == 'drugsBag' || type == 'jPBag')">
      <view v-if="type == 'drugsBag'"> 药包编码 : #{{ infoDATA.packid }} </view>
      <view v-if="type == 'jPBag'">
        静配药包编码 : #{{ infoDATA.packid }}
      </view>
      <view> 所属科室 : {{ infoDATA.target.dept || "-" }} </view>
      <view v-if="infoType === 'drug-dsd-2' || infoType === 'jp-dsd-2'">
        工单已完成。
      </view>
    </view>
    <view class="Scanning_cont" v-if="res.status != 200">
      <view class="Scanning_cont_center">
        {{ res.msg === "undefined" || !res.msg ? "" : res.msg }}
      </view>
    </view>
    <view class="foot_btn" v-if="
        (type == 'specimen' && isKs != '1') ||
        (type == 'specimenPlan' && isKs != '1')
      ">
      <view class="btn1" @click="Scanning_again()"> 继续扫描 </view>
      <view class="btn2" @click="Scanning_complete(dataId)"> 完成扫描 </view>
    </view>
    <view class="foot_btn" v-if="
        (type != 'specimenPlan' &&
          type != 'specimen' &&
          type != 'drugsBag' &&
          type != 'jPBag' &&
          (type != 'inspect') & (type != 'patientTransport')) ||
        (queryObj.status == 200 &&
          type == 'drugsBag' &&
          infoType == 'drug-ddd-2') ||
        (queryObj.status == 200 &&
          type == 'drugsBag' &&
          infoType == 'drug-dsd-2') ||
        (queryObj.status == 200 && type == 'jPBag' && infoType == 'jp-ddd-2') ||
        (queryObj.status == 200 && type == 'jPBag' && infoType == 'jp-dsd-2') ||
        (queryObj.status == 200 && type == 'inspect') ||
        (queryObj.status == 200 && type == 'patientTransport')
      ">
      <view class="btn3" @click="showAlert(dataId)"> 知道了 </view>
    </view>
    <view class="foot_btn" v-if="
        (isKs != '1' &&
          queryObj.status != 200 &&
          type == 'drugsBag' &&
          infoType == 'drug-ddd-2') ||
        (isKs != '1' &&
          queryObj.status != 200 &&
          type == 'drugsBag' &&
          infoType == 'drug-dsd-2') ||
        (isKs != '1' &&
          queryObj.status != 200 &&
          type == 'jPBag' &&
          infoType == 'jp-ddd-2') ||
        (isKs != '1' &&
          queryObj.status != 200 &&
          type == 'jPBag' &&
          infoType == 'jp-dsd-2') ||
        (isKs != '1' && queryObj.status != 200 && type == 'inspect') ||
        (isKs != '1' && queryObj.status != 200 && type == 'patientTransport')
      ">
      <view class="btn3" @click="Scanning_again()"> 继续扫描 </view>
      <view class="btn3" @click="photograph(queryObj, { DEPTCode, DEPT, infoType })" v-if="
          (isKs != '1' && queryObj.status != 200 && type == 'inspect') ||
          (isKs != '1' && queryObj.status != 200 && type == 'patientTransport')
        ">
        拍照
      </view>
    </view>
    <view class="foot_btn" v-if="
        infoType == 'spe-ddd-1' ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'spe-dsd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'spe-dsd-1') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'spe-ddd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'spe-dsd-3') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          infoType == 'plan-spe-ddd-1') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          infoType == 'plan-spe-ddd-2') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          infoType == 'plan-spe-dsd-1') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          infoType == 'plan-spe-dsd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'drug-ddd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'drug-dsd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'jp-ddd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'jp-dsd-2') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'drug-ddd-1') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'drug-dsd-1') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'jp-ddd-1') ||
        (isKs == '1' && queryObj.status != 200 && infoType == 'jp-dsd-1') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          infoType == 'plan-spe-dsd-3') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          type == 'inspect' &&
          queryObj.type1 === 'undefined') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          type == 'patientTransport' &&
          queryObj.type1 === 'undefined')
      ">
      <view class="btn3" @click="showAlert(dataId)"> 知道了 </view>
    </view>
    <view class="foot_btn" v-if="
        (isKs == '1' &&
          queryObj.status != 200 &&
          type == 'inspect' &&
          queryObj.type1 === 'ins-zxz-1') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          type == 'inspect' &&
          queryObj.type1 === 'ins-dsd-1') ||
        (isKs == '1' &&
          queryObj.status != 200 &&
          type == 'patientTransport' &&
          queryObj.type1 === 'trans-dsd-1')
      ">
      <view class="btn1" @click="showAlert(dataId)"> 知道了 </view>
      <view class="btn2" @click="goToRight(dataId)"> 继续执行工单 </view>
    </view>
    <!-- 弹窗 -->
    <showModel :title="models.title" :icon="models.icon" :disjunctor="models.disjunctor" :content="models.content"
      @ok="ok" @cancel="cancel" :operate="models.operate"></showModel>
    <!-- 弹窗 -->
    <showModel :title="models1.title" :icon="models1.icon" :disjunctor="models1.disjunctor"
      :radioItem="models1.radioItem" @ok="ok1" @radioChange="radioChange1" @cancel="cancel1" :operate="models1.operate">
    </showModel>
  </view>
</template>
<script>
  import showModel from "../../components/showModel/showModel.vue";
  import {
    get,
    post,
    SM,
    webHandle
  } from "../../http/http.js";
  import {
    photographTool
  } from "../../tools/photograph.js";
  export default {
    data() {
      return {
        // 完成扫描的id
        wcId: "",
        // 弹窗model
        models: {
          disjunctor: false,
        },
        // 单选框弹窗model1
        models1: {
          disjunctor: false,
        },
        isKs: "", //是否科室签到 1
        infoDATA: {},
        res: {},
        infoType: "",
        DEPTCode: "",
        RESData: {},
        DEPT: "",
        dataId: "",
        type: "",
        model: {},
        speNum: 0,
        queryObj: {}, //路由传递过来的数据
        selectRadio: [], //单选框选中的数据，第一项是qrcode，第二项是名称
      };
    },
    methods: {
      // 拍照
      photograph(data, obj) {
        photographTool(data, obj);
      },
      // 继续扫描
      Scanning_again() {
        SM().then((ress1) => {
          // ----------------
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          //检验二维码的有效性
          post("/dept/scanning", {
            content: ress1,
          }).then((result) => {
            if (result.state == 200 || result.state == 201) {
              let codes = result.code;
              if (codes) {
                let speCode = codes;
                let data = {
                  code: speCode,
                  type: this.queryObj.type1,
                  deptCode: this.DEPTCode,
                  ids: [],
                };
                data.ids.push(this.dataId);
                let postType = "";
                if (this.type == "jPBag" || this.type == "drugsBag") {
                  postType = "handleDrugsAndJp";
                }
                if (this.type == "specimen") {
                  data["speCode"] = data.code;
                  delete data.code;
                  postType = "handleSpes";
                }
                if (this.type == "specimenPlan") {
                  data["speCode"] = data.code;
                  delete data.code;
                  postType = "handlePlanSpes";
                }
                if (this.type == "inspect") {
                  postType = "handleIns";
                }
                if (this.type == "patientTransport") {
                  postType = "handleTrans";
                }
                post("/workerOrder/" + postType, data).then((ress) => {
                  uni.hideLoading();
                  if (ress.status == 200) {
                    if (this.type == "specimenPlan" || this.type == "specimen") {
                      this.infoDATA = ress.data;
                      this.speNum++;
                      uni.setStorageSync("speNum", this.speNum);
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${
                        this.queryObj.type
                      }&type1=${
                        this.queryObj.type1
                      }&infoData=${encodeURIComponent(
                        JSON.stringify(ress.data)
                      )}&status=${ress.status}&msg=${ress.msg}&deptCode=${
                        this.queryObj.deptCode
                      }&dept=${this.queryObj.dept}&id=${this.dataId}&speNum=${
                        this.speNum
                      }&model=${encodeURIComponent(JSON.stringify(ress))}`,
                      });
                    } else if (
                      this.type == "inspect" ||
                      this.type == "patientTransport"
                    ) {
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${
                        this.queryObj.type //baba
                      }&type1=${
                        this.queryObj.type1
                      }&infoData=${encodeURIComponent(
                        JSON.stringify(ress.data)
                      )}&status=${ress.status}&msg=${ress.msg}&deptCode=${
                        this.queryObj.deptCode
                      }&dept=${this.queryObj.dept}&id=${this.dataId}&speNum=${
                        this.speNum
                      }&model=${encodeURIComponent(
                        JSON.stringify(ress)
                      )}&patient=${ress.patient}&patientCode=${
                        ress.patientCode
                      }&deptName=${ress.deptName}&bedNum=${ress.bedNum}`,
                      });
                    } else {
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${
                        this.queryObj.type
                      }&type1=${
                        this.queryObj.type1
                      }&infoData=${encodeURIComponent(
                        JSON.stringify(ress.data)
                      )}&status=${ress.status}&msg=${ress.msg}&deptCode=${
                        this.queryObj.deptCode
                      }&dept=${this.queryObj.dept}&id=${this.dataId}&speNum=${
                        this.speNum
                      }&model=${encodeURIComponent(JSON.stringify(ress))}`,
                      });
                    }
                  } else {
                    uni.navigateTo({
                      url: `../scanning_Result/scanning_Result?type=${
                      this.queryObj.type
                    }&type1=${this.queryObj.type1}&status=${ress.status}&msg=${
                      ress.msg
                    }&deptCode=${this.queryObj.deptCode}&dept=${
                      this.queryObj.dept
                    }&id=${this.dataId}&speNum=${
                      this.speNum
                    }&model=${encodeURIComponent(
                      JSON.stringify(this.infoDATA)
                    )}`,
                    });
                  }
                });
              } else {
                uni.hideLoading();
              }
            } else {
              uni.hideLoading();
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
          // ------------------------------
        });
      },
      // 确定
      ok() {
        this.models.disjunctor = false;
        let data = {
          type: this.queryObj.type1,
          ids: [],
        };
        data.ids.push(this.wcId);
        // uni.showLoading({
        //   title: '加载中',
        //   mask: true
        // });
        //只要是标本轮巡1对多或者多对多
        if (
          this.queryObj.type1 === "plan-spe-ddd-2" ||
          this.queryObj.type1 === "plan-spe-ddd-3" ||
          this.queryObj.type1 === "plan-spe-dsd-2" ||
          this.queryObj.type1 === "plan-spe-dsd-3"
        ) {
          post("/workerOrder/finishPlanSpes", data).then((res) => {
            // uni.hideLoading()
            if (res.status == 200) {
              uni.navigateTo({
                url: "../receiptpage/receiptpage",
              });
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
        } else {
          post("/workerOrder/finishSpes", data).then((res) => {
            // uni.hideLoading()
            if (res.status == 200) {
              uni.navigateTo({
                url: "../receiptpage/receiptpage",
              });
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
        }
      },
      // 取消
      cancel() {
        this.models.disjunctor = false;
      },
      // 完成扫描
      Scanning_complete(id) {
        let data = {
          type: this.queryObj.type1,
          ids: [],
        };
        data.ids.push(id);
        if (
          this.queryObj.type1 == "plan-spe-ddd-2" ||
          this.queryObj.type1 == "spe-ddd-2"
        ) {
          this.wcId = id;
          this.models = {
            disjunctor: true,
            title: "提示",
            content: "是否确定标本已扫描完成？",
            icon: "warn",
            operate: {
              ok: "确定",
              cancel: "取消",
            },
          };
        } else {
          uni.navigateTo({
            url: `../scanning/scanning?type=${this.queryObj.type}&type1=${
            this.queryObj.type1
          }&id=${encodeURIComponent(JSON.stringify(data.ids))}&deptCode=${
            this.queryObj.deptCode
          }&dept=${this.queryObj.dept}&speNum=${this.speNum}`,
          });
        }
      },
      // 继续执行工单,当扫错科室码的时候，直接跳过，默默执行正确的科室码
      goToRight(id) {
        // flag 为1时查检查科室，flag为2时查终点科室
        let flag;
        // 患者转科，患者陪检
        if (
          this.queryObj.type1 == "trans-dsd-1" ||
          this.queryObj.type1 == "ins-dsd-1"
        ) {
          flag = 2;
        }
        if (this.queryObj.type1 == "ins-zxz-1") {
          flag = 1;
        }
        let postData = {
          workOrderId: id,
          flag: flag,
        };
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/continueWorkOrder", postData).then((res) => {
          uni.hideLoading();
          console.log(res);
          if (res.state == 200) {
            let radioItem = [];
            res.data.forEach((item) => {
              let flag = radioItem.every(
                (item1) => item1.qrcode !== item.qrcode + "__" + item.deptName
              ); //如果radioItem里的所有项都与之不重复
              if (flag) {
                radioItem.push({
                  qrcode: item.qrcode + "__" + item.deptName,
                  deptName: item.deptName,
                });
              }
            });
            this.models1 = {
              disjunctor: true,
              title: "请选择正确的科室",
              radioItem,
              icon: "",
              operate: {
                ok: "确定",
                cancel: "取消",
              },
            };
          } else {
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        });
      },
      // 选中单选框
      radioChange1(item) {
        console.log(item);
        this.selectRadio = item.split("__");
      },
      // 确定
      ok1() {
        this.models1.disjunctor = false;
        if (!this.selectRadio.length) {
          //默认选中第一项，选中则是选中项
          this.selectRadio = [
            this.models1.radioItem[0].qrcode.split("__")[0],
            this.models1.radioItem[0].deptName,
          ];
        }
        uni.navigateTo({
          url: `../scanning_code/scanning_code?type=${this.queryObj.type}&type1=${this.queryObj.type1}&id=${this.queryObj.id}&deptCode=${this.selectRadio[0]}&dept=${this.selectRadio[1]}`,
        });
      },
      // 取消
      cancel1() {
        this.models1.disjunctor = false;
      },
      // 知道了
      showAlert(id) {
        uni.navigateTo({
          url: "../receiptpage/receiptpage",
        });
      },
    },
    onLoad(options) {
      console.log(options, "result");
      this.queryObj = options;
      this.isKs = this.queryObj.isKs;
      if (options.infoData && options.infoData != "undefined") {
        this.infoDATA = JSON.parse(options.infoData);
      } else if (options.model) {
        this.infoDATA = JSON.parse(options.model);
      }

      this.dataId = options.id;
      this.RESData = options.resData;
      this.res["status"] = options.status;
      this.res["msg"] = options.msg;
      console.log(options.patient);
      this.res["patient"] = options.patient;
      this.res["patientCode"] = options.patientCode;
      this.res["deptName"] = options.deptName;
      this.res["bedNum"] = options.bedNum;
      this.infoType = options.type1;
      if (options.dept) {
        this.DEPT = options.dept;
      }
      if (options.speNum) {
        this.speNum = options.speNum;
      } else {
        this.speNumLocal = uni.getStorageSync("speNum");
        if (this.speNumLocal) {
          this.speNum = Number(this.speNumLocal);
        } else {
          this.speNum = 0;
        }
      }
      this.DEPTCode = options.deptCode;
      this.type = options.type;
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less">
  .Scanning_Result {
    padding: 0 74rpx;

    .Scanning_top {
      .Scanning_top_icon {
        width: 140rpx;
        height: 140rpx;
        margin: 0 auto;
        margin-top: 116rpx;
        border-radius: 50%;
        line-height: 140rpx;

        .cubeic-ok {
          font-size: 140rpx;
          color: #35b34a;
        }

        .cubeic-close {
          font-size: 140rpx;
          color: #ff3b53;
        }
      }

      .Scanning_top_text {
        .text1 {
          margin-top: 40rpx;
          font-size: 48rpx;
          text-align: center;
        }

        .success_tips {
          color: red;
          font-size: 30rpx;
        }
      }
    }

    .Scanning_cont {
      font-size: 32rpx;
      text-align: center;

      view {
        margin-bottom: 16rpx;
      }

      .Scanning_cont_center {
        text-align: center;
      }

      .text {
        margin-top: 24rpx;
        color: #35b34a;
      }

      .text1 {
        margin-top: 24rpx;
        color: #ff3b53;
      }
    }

    .foot_btn {
      line-height: 88rpx;
      height: 100rpx;
      margin-top: 40rpx;
      display: flex;
      justify-content: center;

      .btn1,
      .btn2,
      .btn3 {
        height: 88rpx;
        width: 45%;
        margin: 0 1%;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        color: #fff;
        border-radius: 8rpx;
        font-size: 32rpx;
        margin-top: 16rpx;
        text-align: center;
      }
    }
  }
</style>
