<template>
  <view class="toolbar" @click="Scanning()" hover-class="seimin-btn-hover">
    <text class="toolbar-icon icon_transport transport-saoma"></text>
    <text class="toolbar-sao">扫一扫</text>
  </view>
</template>

<script>
  import {
    post,
    SM
  } from "../../http/http.js";
  export default {
    data() {
      return {
        currentCode: '', //当前小扫描的科室二维码
        SMFlag: true,
      }
    },
    methods: {
      // 底部扫描
      Scanning() {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((content) => {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          //检验二维码的有效性(扫码前必须验证)
          post("/dept/scanning", {
              content,
            })
            .then((result) => {
              this.currentCode = result.code;
              this.SMFlag = true;
              // 200检测通过，201没有有效期也通过。
              if (result.state == 200 || result.state == 201) {
                let data = {
                  code: result.code, //二维码
                };
                //微信—大扫描（科室码，患者码，标本码，快捷建单码，药包码）
                post("/workerOrder/scanCode", data)
                  .then((res) => {
                    uni.hideLoading();
                    if (res.status == 200) {
                      // 扫描标本码
                      if (res.type == "specimen") {
                        let infoDATA = {
                          stype: res.data.stype,
                          scode: res.data.scode,
                          patientName: res.data.patientName,
                          sickRoom: res.data.sickRoom ?
                            res.data.sickRoom.dept : "-",
                          checkDept: res.data.checkDept ?
                            res.data.checkDept.dept : "-",
                          bedNum: res.data.bedNum,
                        };
                        uni.navigateTo({
                          url: `../scanning_B/scanning_B?res=${encodeURIComponent(
                          JSON.stringify(res)
                        )}&infoDATA=${encodeURIComponent(
                          JSON.stringify(infoDATA)
                        )}`,
                        });
                      } else if (res.type == "patient") {
                        //如果是患者腕带码
                        // code: data.code //二维码
                        res.workOrder = res.workOrder ? res.workOrder : []; //liao
                        uni.navigateTo({
                          url: `../scanning_ins/scanning_ins?code=${
                          data.code
                        }&infoDATA=${encodeURIComponent(
                          JSON.stringify(res.data)
                        )}&workData=${encodeURIComponent(
                          JSON.stringify(res.workOrder)
                        )}&patientOrders=${encodeURIComponent(
                          JSON.stringify(res.patientOrders)
                        )}`,
                        });
                      } else if (res.type == "quickOrder") {
                        //快捷建单
                        uni.navigateTo({
                          url: `../shortcutbuildOrders/shortcutbuildOrders?infoDATA=${encodeURIComponent(
                          JSON.stringify(res.data)
                        )}`,
                        });
                      } else if (res.type == "dept") {
                        // 扫描科室码
                        // code: data.code //二维码
                        let infoDATA = res.data;
                        uni.navigateTo({
                          url: `../scanning_all/scanning_all?infoDATA=${encodeURIComponent(
                          JSON.stringify(infoDATA)
                        )}&code=${data.code}`,
                        });
                      } else if (res.type == 'drugsBag') {
                        //扫药包码
                        uni.navigateTo({
                          url: `/pages/scanning_drug/scanning_drug?drugsBagId=${res.drugsBagId}`,
                        });
                      }
                    } else {
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?status=${res.status}&msg=${res.msg}&qrcode=${this.currentCode}`,
                      });
                    }
                  });
              } else {
                uni.hideLoading();
                uni.showToast({
                  icon: "none",
                  title: '请求失败',
                });
              }
            })
        }).catch(err => {
          this.SMFlag = true;
        });
      },
    },
    onShow() {
      this.SMFlag = true;
    }
  };
</script>

<style lang="less">
  // 底部扫一扫
  .toolbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999;
    height: 88rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    border-radius: 4rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.24);
    background-color: #e5e9ed;

    .toolbar-icon {
      font-size: 52rpx;
      margin-right: 16rpx;
      color: #07863c;
    }

    .toolbar-sao {
      font-size: 36rpx;
      color: #333;
    }
  }
</style>
