<template>
  <view class="Scanning_djEnd">
    <view class="Scanning_top" v-if="infoDATA.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>
    </view>

    <view class="Scanning_top" v-if="
        infoDATA.status == 10005 ||
        infoDATA.status == 100018 ||
        infoDATA.status == 10004 ||
        infoDATA.status == 100021 ||
        infoDATA.status == 500 ||
        infoDATA.status == 100031
      ">
      <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 class="text1 f30" v-if="queryObj.qrcode !== undefined">扫描内容：{{queryObj.qrcode}}</view>
      </view>
    </view>

    <view class="Scanning_cont" v-if="
        (infoDATA.status == 200 && type == 'specimenPlan') ||
        (infoDATA.status == 200 && type == 'specimen')
      "></view>
    <view class="Scanning_cont" v-if="
        (infoDATA.status == 200 && type == 'patientTransport') ||
        (infoDATA.status == 200 && type == 'inspect')
      ">扫描患者腕带并将患者送往目标科室.</view>
    <view class="Scanning_cont" v-if="
        (infoDATA.status == 200 &&
          type == 'drugsBag' &&
          (type1 == 'drug-ddd-2' || type1 == 'drug-dsd-2')) ||
        (infoDATA.status == 200 &&
          type == 'jPBag' &&
          (type1 == 'jp-ddd-2' || type1 == 'jp-dsd-2'))
      ">已到达目标科室,请扫描药包或静配包.</view>
    <view class="Scanning_cont" v-if="
        (infoDATA.status == 200 &&
          type == 'drugsBag' &&
          (type1 == 'drug-ddd-1' || type1 == 'drug-dsd-1')) ||
        (infoDATA.status == 200 &&
          type == 'jPBag' &&
          (type1 == 'jp-ddd-1' || type1 == 'jp-dsd-1'))
      ">
      <view v-if="accountObj"> 交接人账号 : {{ accountObj.account }} </view>
      <view v-if="accountObj"> 交接人姓名 : {{ accountObj.accountName }} </view>
      <view>已到达目标科室,请取药包或静配包后送达目标科室.</view>
    </view>
    <view class="Scanning_cont" v-if="
        infoDATA.status == 200 && type == 'other' && infoDATA.type == 'qt-ddd-1'
      ">已到达目标科室,请执行该工单.</view>
    <view class="Scanning_cont" v-if="
        (infoDATA.state == 'finish' &&
          infoDATA.status == 200 &&
          type == 'specimenPlan' &&
          infoDATA.type == 'plan-spe-dsd-1') ||
        (infoDATA.status == 200 &&
          type == 'other' &&
          infoDATA.type == 'qt-dsd-1')
      ">已到达目标科室,工单已完成.</view>
    <view class="Scanning_cont" v-if="
        (infoDATA.status == 200 &&
          type == 'specimenPlan' &&
          infoDATA.type != 'plan-spe-ddd-1' &&
          infoDATA.type != 'plan-spe-dsd-1') ||
        (infoDATA.status == 200 &&
          type == 'specimen' &&
          infoDATA.type != 'spe-ddd-1' &&
          infoDATA.type != 'spe-dsd-1')
      ">已到达目标科室,请扫描标本.</view>
    <view class="Scanning_cont" v-if="
        (infoDATA.status == 200 &&
          type == 'specimenPlan' &&
          infoDATA.type == 'plan-spe-ddd-1') ||
        (infoDATA.status == 200 &&
          type == 'specimen' &&
          infoDATA.type == 'spe-ddd-1')
      ">
      <view v-if="accountObj"> 交接人账号 : {{ accountObj.account }} </view>
      <view v-if="accountObj"> 交接人姓名 : {{ accountObj.accountName }} </view>
      <view>已到达目标科室,请取标本后送达目标科室.</view>
    </view>
    <view class="Scanning_cont" v-if="
        (infoDATA.state != 'finish' &&
          infoDATA.status == 200 &&
          type == 'specimenPlan' &&
          infoDATA.type == 'plan-spe-dsd-1') ||
        (infoDATA.status == 200 &&
          type == 'specimen' &&
          infoDATA.type == 'spe-dsd-1')
      ">
      <view v-if="accountObj"> 交接人账号 : {{ accountObj.account }} </view>
      <view v-if="accountObj"> 交接人姓名 : {{ accountObj.accountName }} </view>
      <view>已到达目标科室,请放置标本完成该工单.</view>
    </view>
    <view class="Scanning_cont" v-if="infoDATA.status != 200">
      <view>{{ infoDATA.msg }}</view>
    </view>
    <view class="foot_btn" v-if="type == 'inspect' || type == 'patientTransport'">
      <view class="btn3" @click="Scanning_again()">继续扫描</view>
    </view>
    <view class="foot_btn" v-if="type == 'drugsBag' || type == 'jPBag'">
      <view class="btn3" @click="Scanning_again()" v-if="
          type1 === 'drug-ddd-2' ||
          type1 === 'drug-dsd-2' ||
          type1 === 'jp-ddd-2' ||
          type1 === 'jp-dsd-2'
        ">继续扫描</view>
      <view class="btn3" @click="showAlert()" v-if="
          type1 === 'drug-ddd-1' ||
          type1 === 'drug-dsd-1' ||
          type1 === 'jp-ddd-1' ||
          type1 === 'jp-dsd-1'
        ">知道了</view>
    </view>
    <view class="foot_btn" v-if="
        (type == 'specimenPlan' &&
          type1 != 'plan-spe-dsd-1' &&
          (type1 != 'plan-spe-ddd-1') & (type1 != 'plan-spe-ddd-2')) ||
        (type == 'specimen' &&
          type1 != 'spe-dsd-1' &&
          (type1 != 'spe-ddd-1') & (type1 != 'spe-ddd-2'))
      ">
      <view class="btn1" @click="Scanning_again()">扫描标本</view>
      <view class="btn3" @click="hand_again()">手动录入</view>
      <view class="btn3" @click="getNoScanSpecimen()">未扫描标本</view>
      <view class="btn2" @click="Scanning_complete(ids)">完成扫描</view>
    </view>
    <view class="foot_btn" v-if="
        (type == 'specimenPlan' && type1 == 'plan-spe-ddd-2') ||
        (type == 'specimen' && type1 == 'spe-ddd-2')
      ">
      <view class="btn1" @click="Scanning_again()">扫描标本</view>
      <view class="btn3" @click="hand_again()">手动录入</view>
      <view class="btn3" @click="getNoScanSpecimen()">未扫描标本</view>
      <view class="btn2" @click="Scanning_complete(ids)">完成扫描</view>
    </view>
    <view class="foot_btn" v-if="
        (infoDATA.state != 'finish' &&
          type == 'specimenPlan' &&
          type1 == 'plan-spe-dsd-1') ||
        (type == 'specimen' && type1 == 'spe-dsd-1')
      ">
      <view class="btn1" @click="Scanning_complete1(ids)">完成工单</view>
      <view class="btn2" @click="showAlert()">继续配送</view>
    </view>
    <view class="foot_btn" v-if="
        infoDATA.status != 200 &&
        type != 'specimenPlan' &&
        type != 'specimen' &&
        type !== 'jPBag' &&
        type !== 'inspect' &&
        type !== 'drugsBag' &&
        type !== 'patientTransport'
      ">
      <view class="btn3" @click="showAlert()">知道了</view>
    </view>
    <view class="foot_btn" v-if="
        (infoDATA.status == 200 && type1 == 'spe-ddd-1') ||
        (infoDATA.status == 200 && type1 == 'plan-spe-ddd-1')
      ">
      <view class="btn3" @click="showAlert()">知道了</view>
    </view>
    <view class="foot_btn" v-if="
        (infoDATA.state == 'finish' &&
          infoDATA.status == 200 &&
          type == 'specimenPlan' &&
          type1 == 'plan-spe-dsd-1') ||
        (infoDATA.status == 200 && type == 'other')
      ">
      <view class="btn3" @click="showAlert()">知道了</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="models2.title" :icon="models2.icon" :disjunctor="models2.disjunctor" :content="models2.content"
      @ok="ok2" @know="know2" @cancel="cancel2" :operate="models2.operate">
    </showModel>
    <!-- 手动查询标本弹窗 -->
    <handViewSpecimen v-if="speModels.disjunctor" :title="speModels.title" :disjunctor="speModels.disjunctor"
      @ok="speOk" @cancel="speCancel">
    </handViewSpecimen>
  </view>
</template>
<script>
  import showModel from "../../components/showModel/showModel.vue";
  import {
    get,
    post,
    SM,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        currentCode: '', //当前小扫描的科室二维码
        // 手动查询标本弹窗model
        speModels: {
          disjunctor: false,
        },
        // 交接人信息
        accountObj: undefined,
        //传给后端查询是否继续扫描
        deptId: undefined,
        SMFlag: true,
        // 弹窗model
        models: {
          disjunctor: false,
        },
        // 完成扫描弹窗model1
        models2: {
          disjunctor: false,
        },
        wcId: "", //工单完成ID
        wcFlag: false,
        modelFlag: "", //工单完成类型
        infoDATA: [],
        ids: [],
        type: "",
        type1: "",
        queryObj: {}, //路由传递过来的参数
        gotoFlag: true,
        speNum: 0,
        content: "",
      };
    },
    methods: {
      // 手动查询标本-确认
      speOk(data) {
        console.log(data);
        if (!data.id) {
          //没有查询到标本
          uni.showModal({
            title: '提示',
            content: "没有查询到标本！",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定');
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
          return;
        }
        this.speModels.disjunctor = false;
        this.hand_scanning_common(data.scode, 'hand');
      },
      // 手动查询标本-取消
      speCancel() {
        this.speModels.disjunctor = false;
      },
      // 手动查询标本弹窗
      showHandViewSpecimen() {
        this.speModels = {
          title: '填写标本编码',
          disjunctor: true,
        }
      },
      // 手动录入标本
      hand_again() {
        this.showHandViewSpecimen();
      },
      // 手动输入标本和扫码公共方法
      hand_scanning_common(ress1, type) {
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        // ----------------
        //检验二维码的有效性
        post("/dept/scanning", {
          content: ress1,
        }).then((result) => {
          this.SMFlag = true;
          this.currentCode = result.code;
          if (result.state == 200 || result.state == 201) {
            let ress = result.code;
            let speCode = ress;
            let data = {
              code: speCode,
              type: this.queryObj.type1,
              deptCode: this.code,
              ids: this.ids,
            };
            let postType = "";
            if (this.type == "jPBag" || this.type == "drugsBag") {
              postType = "handleDrugsAndJp";
              delete data.ids;
              if (this.accountObj) {
                data.handover = this.accountObj.accountId;
              }
              data.deptId = this.deptId;
            }
            if (this.type == "specimen") {
              postType = "handleSpes";
              data["speCode"] = data.code;
              delete data.code;
              if (this.accountObj) {
                data.handover = this.accountObj.accountId;
              }
            }
            if (this.type == "specimenPlan") {
              postType = "handlePlanSpes";
              data["speCode"] = data.code;
              delete data.code;
              if (this.accountObj) {
                data.handover = this.accountObj.accountId;
              }
            }
            if (this.type == "inspect") {
              postType = "handleIns";
            }
            if (this.type == "patientTransport") {
              postType = "handleTrans";
            }
            post("/workerOrder/" + postType, data).then((ress) => {
              uni.hideLoading();
              if (ress.status == 200) {
                //标本和标本轮巡工单，扫描标本后会自动调用摄像头，继续扫描，直到status不是200
                if (this.type == "specimenPlan" || this.type == "specimen") {
                  if (type === 'scan') {
                    this.Scanning_again();
                  } else if (type === 'hand') {
                    // this.hand_again();
                  }
                }
                this.speNum = ress.specimenCount;
                uni.navigateTo({
                  url: `../scanning_djInfo/scanning_djInfo?type=${
                  this.queryObj.type
                }&type1=${
                  this.queryObj.type1
                }&infoData=${encodeURIComponent(
                  JSON.stringify(ress.data)
                )}&status=${ress.status}&msg=${ress.msg}&deptCode=${
                  this.queryObj.code
                }&dept=${this.queryObj.dept}&ids=${encodeURIComponent(
                  JSON.stringify(this.ids)
                )}&model=${encodeURIComponent(
                  JSON.stringify(ress)
                )}&speNum=${this.speNum}&accountObj=${encodeURIComponent(
                  JSON.stringify(this.accountObj)
                )}&deptId=${this.deptId}&scanOrHand=${type}`,
                });
              } else {
                uni.navigateTo({
                  url: `../scanning_djInfo/scanning_djInfo?type=${
                  this.queryObj.type
                }&type1=${this.queryObj.type1}&status=${ress.status}&msg=${
                  ress.msg
                }&deptCode=${this.queryObj.code}&dept=${
                  this.queryObj.dept
                }&ids=${encodeURIComponent(
                  JSON.stringify(this.ids)
                )}&infoData=${encodeURIComponent(
                  JSON.stringify(ress.data)
                )}&model=${encodeURIComponent(
                  JSON.stringify(ress)
                )}&accountObj=${encodeURIComponent(
                  JSON.stringify(this.accountObj)
                )}&deptId=${this.deptId}&scanOrHand=${type}&qrcode=${this.currentCode}`,
                });
              }
            });
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        });
        // ------------------------------
      },
      // 查看未扫描标本
      getNoScanSpecimen() {
        if (!this.gotoFlag) {
          return;
        }
        this.gotoFlag = false;
        let ids = JSON.parse(this.queryObj.ids);
        uni.navigateTo({
          url: `../noScanSpecimen/noScanSpecimen?workOrderId=${encodeURIComponent(
          JSON.stringify(ids)
        )}&deptCode=${this.queryObj.code}`,
        });
      },
      // 正常完成扫描
      overFinish() {
        //完成扫描
        let postType = "";
        if (
          this.queryObj.type1 === "plan-spe-ddd-2" ||
          this.queryObj.type1 === "plan-spe-dsd-2" ||
          this.queryObj.type1 === "plan-spe-dsd-3"
        ) {
          postType = "finishPlanSpes";
        } else if (this.queryObj.type1 == "spe-ddd-2") {
          postType = "finishSpes";
        }
        let data = {
          type: this.queryObj.type1,
          ids: this.wcId,
        };
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/" + postType, data).then((res) => {
          if (res.status == 200) {
            this.models2 = {
              disjunctor: true,
              title: "提示",
              content: `完成扫描成功`,
              icon: "success",
              operate: {
                know: "知道了",
              },
            };
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        });
      },
      // 确定
      ok() {
        this.models.disjunctor = false;
        if (this.modelFlag === "complete") {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          let ids = JSON.parse(this.queryObj.ids);
          let postData = {
            ids,
            sign: true,
            deptQrCode: this.queryObj.code,
          };
          post("/workerOrder/expectedAndActual", postData).then((ress) => {
            uni.hideLoading();
            if (ress.status == 200) {
              if (this.wcFlag) {
                //正常完成扫描
                this.overFinish();
              } else {
                this.gotoOver();
              }
            } else if (ress.status == 1000035) {
              let content = "";
              if (
                this.queryObj.type1 === "spe-ddd-2" ||
                this.queryObj.type1 === "plan-spe-ddd-2"
              ) {
                //待到达
                content =
                  `系统内预计标本<strong class="red">${ress.expectReceiveNum}</strong>只，您扫描收取标本<strong class="red">${ress.actualReceiveNum}</strong>只，其中<strong class="red">${ress.notReceiveNum}</strong>只未扫描；`;
              } else {
                //待送达
                this.content = `本工单已签到<strong class="red">${
                ress.scanSet ? ress.scanSet.join("、") : ""
              }</strong>，剩余需签到科室<strong class="red">${
                ress.notScanSet ? ress.notScanSet.join("、") : ""
              }</strong>，总签收<strong class="red">${
                ress.totalAcceptance
              }</strong>只，剩余<strong class="red">${
                ress.notAcceptance
              }</strong>只未签收，您确定完成工单吗？`;
                content =
                  `您在<strong class="red">${ress.deptName}</strong>检验科扫描了<strong class="red">${ress.deptScanNum}</strong>个标本，还需扫描<strong class="red">${ress.deptNotScanNum}</strong>标本。`;
              }
              this.models2 = {
                disjunctor: true,
                title: "提示",
                content,
                icon: "warn",
                operate: {
                  ok: "确定",
                  cancel: "取消",
                },
              };
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
        } else if (this.modelFlag === "complete1") {
          //1对1标本完成工单
          let ids = this.wcId;
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          post("/workerOrder/finishSpeOrder", ids).then((res) => {
            if (res.status == 200) {
              uni.reLaunch({
                url: "../receiptpage/receiptpage",
              });
            } else {
              uni.hideLoading();
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
        }
      },
      // 取消
      cancel() {
        this.models.disjunctor = false;
      },
      // 确定
      ok2() {
        this.models2.disjunctor = false;
        let ids = JSON.parse(this.queryObj.ids);
        let postData = {
          ids,
          deptQrCode: this.queryObj.code,
        };
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/expectedAndActual", postData).then((ress) => {
          uni.hideLoading();
          if (ress.status == 200) {
            if (this.wcFlag) {
              this.overFinish();
            } else {
              this.gotoOver();
            }
          } else {
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        });
      },
      // 取消
      cancel2() {
        this.models2.disjunctor = false;
      },
      know2() {
        this.models2.disjunctor = false;
        uni.reLaunch({
          url: "../receiptpage/receiptpage",
        });
      },
      //完成扫描
      Scanning_complete(id) {
        this.modelFlag = "complete";
        this.wcId = id;
        if (
          this.queryObj.type1 == "plan-spe-ddd-2" ||
          this.queryObj.type1 == "spe-ddd-2"
        ) {
          this.wcFlag = true;
          this.models = {
            disjunctor: true,
            title: "提示",
            content: "是否确定标本已扫描完成?",
            icon: "warn",
            operate: {
              ok: "确定",
              cancel: "取消",
            },
          };
        } else if (
          this.queryObj.type1 == "plan-spe-dsd-2" ||
          this.queryObj.type1 == "plan-spe-dsd-3" ||
          this.queryObj.type1 == "spe-dsd-2" ||
          this.queryObj.type1 == "spe-dsd-3"
        ) {
          this.wcFlag = false;
          this.models = {
            disjunctor: true,
            title: "提示",
            content: "是否确定标本已扫描完成？",
            icon: "warn",
            operate: {
              ok: "确定",
              cancel: "取消",
            },
          };
        } else {
          this.gotoOver();
        }
      },
      // 跳转完成工单页面
      gotoOver() {
        uni.navigateTo({
          url: `../scanning/scanning?type=${this.queryObj.type}&type1=${
          this.queryObj.type1
        }&deptCode=${this.queryObj.code}&dept=${
          this.queryObj.dept
        }&id=${encodeURIComponent(JSON.stringify(this.wcId))}&content=${
          this.content
        }`,
        });
      },
      //一对一完成工单
      Scanning_complete1(id) {
        this.modelFlag = "complete1";
        this.wcId = id;
        this.models = {
          disjunctor: true,
          title: "提示",
          content: "是否确定标本配送完成?",
          icon: "warn",
          operate: {
            ok: "确定",
            cancel: "取消",
          },
        };
      },
      //继续扫描
      Scanning_again() {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM()
          .then((ress1) => {
            this.hand_scanning_common(ress1, 'scan');
          })
          .catch((err) => {
            this.SMFlag = true;
          });
      },
      // 知道了
      showAlert() {
        uni.navigateTo({
          url: "../receiptpage/receiptpage",
        });
      },
    },
    onShow() {
      this.SMFlag = true;
      this.gotoFlag = true;
    },
    onLoad(options) {
      console.log(options, "djEnd");
      if (options.speNum) {
        this.speNum = options.speNum;
      }
      if (options.accountObj && options.accountObj != "undefined") {
        this.accountObj = JSON.parse(options.accountObj);
      }
      if (options.deptId && options.deptId != "undefined") {
        this.deptId = options.deptId;
      }
      this.queryObj = options;
      let list = JSON.parse(options.model); //签到返回的信息
      this.code = options.code; //二维码code
      this.ids = options.ids && JSON.parse(options.ids); //科室ID数组
      this.infoDATA = list;
      if (options.type) {
        //关联类型
        this.type = options.type;
      } else {
        this.type = list.type;
      }
      this.type1 = options.type1;
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less">
  .Scanning_djEnd {
    padding: 0px 20rpx;

    .Scanning_top {
      height: 270rpx;

      .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;
        }
      }
    }

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

      view {
        margin-bottom: 16rpx;
      }

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

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

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

      view {
        height: 88rpx;
        flex: 1;
        margin: 0 1%;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        color: #fff;
        border-radius: 8rpx;
        font-size: 32rpx;
        margin-top: 16rpx;
      }
    }
  }
</style>
