<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>
    <!-- 填写交接人账号弹窗 -->
    <selectAccount @click.stop.native v-if="hosModels.disjunctor" :title="hosModels.title" :disjunctor="hosModels.disjunctor" @ok="hosOk"
      @cancel="hosCancel">
    </selectAccount>
  </view>
</template>

<script>
  import {
    post,
    SM
  } from "../../http/http.js";
  export default {
    data() {
      return {
        // 填写交接人账号弹窗model
        hosModels: {
          disjunctor: false,
        },
        userId: {
          ids: [],
        },
        workData: [],
        infoDATA: {},
        tabType: "", //当前选中项
        selectArr: [], //选中项
        code: "",
        currentCode: '', //当前小扫描的科室二维码
        SMFlag: true,
      }
    },
    methods: {
      // 填写交接人账号-确认
      hosOk(data) {
        console.log(data);
        const {
          accountName,
          account,
          accountId
        } = data;
        if (!accountName && !account) {
          //没有填写交接人
          uni.showModal({
            title: "提示",
            content: "请填写交接人账号！",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log("用户点击确定");
              } else if (res.cancel) {
                console.log("用户点击取消");
              }
            },
          });
          return;
        } else if ((!accountName && account) || (accountName && !account)) {
          //没有填写交接人
          uni.showModal({
            title: "提示",
            content: "请填写正确的交接人账号！",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log("用户点击确定");
              } else if (res.cancel) {
                console.log("用户点击取消");
              }
            },
          });
          return;
        }
        this.hosModels.disjunctor = false;
        this.orderDeptHandler(false, data);
      },
      // 填写交接人账号-取消
      hosCancel() {
        this.hosModels.disjunctor = false;
        this.flag = true;
      },
      // 填写交接人账号弹窗
      showSelectAccount() {
        this.hosModels = {
          title: "填写交接人账号",
          disjunctor: true,
        };
      },
      //科室签到
      //trueBigScanner----判断是否大扫描
      //bigScanner----判断是否需要交接人
      //accountObj----弹窗填写的交接人信息
      orderDeptHandler(bigScanner, accountObj) {
        let type = "orderSign/" + this.code;
        let list = {
          ids: this.userId.ids,
          trueBigScanner: ['666']
        };
        bigScanner && (list.bigScanner = ['666']);
        if (accountObj) {
          list.handover = [accountObj.accountId];
        }
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/" + type, list).then((res) => {
          console.log(this.tabType)
          uni.hideLoading();
          if (res.status == 200) {
            if (this.tabType == 'specimenPlan') {
              // -----------------------------
              console.log(this.selectArr, '选中工单')
              let gd = this.infoDATA.specimenPlan.find(v => v.id == this.selectArr[0]);
              let startDeptId = gd.startDept.id; //起点科室id
              let isDigitalHandover = gd.taskType.isDigitalHandover; //是否数字交接开关
              let signDeptId = res.deptId; //签到的科室id
              let gdId = gd.id;
              let gdState = gd.gdState.value;
              let endDepts = gd.endDepts.map(v => v.id).toString();
              uni.navigateTo({
                url: `../scanning_djEnd/scanning_djEnd?type=${
                  this.tabType
                }&type1=${res.type}&code=${this.code}&dept=${
                  res.dept
                }&ids=${encodeURIComponent(
                  JSON.stringify(this.userId.ids)
                )}&model=${encodeURIComponent(JSON.stringify(res))}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}&deptId=${res.deptId}&startDeptId=${startDeptId}&isDigitalHandover=${isDigitalHandover}&signDeptId=${signDeptId}&gdId=${gdId}&gdState=${gdState}&endDepts=${endDepts}`,
              });
            } else if (this.tabType == 'specimen') {
              if (res.deptType && res.deptType.value === 'middleRoom') {
                // 如果是中转科室
                uni.navigateTo({
                  url: `../checkAfterBigScreen/checkAfterBigScreen?type=${
                  this.tabType
                }&type1=${res.type}&code=${this.code}&dept=${
                  res.dept
                }&ids=${encodeURIComponent(
                  JSON.stringify(this.userId.ids)
                )}&model=${encodeURIComponent(JSON.stringify(res))}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}&deptId=${res.deptId}`,
                });
              } else {
                uni.navigateTo({
                  url: `../scanning_djEnd/scanning_djEnd?type=${
                  this.tabType
                }&type1=${res.type}&code=${this.code}&dept=${
                  res.dept
                }&ids=${encodeURIComponent(
                  JSON.stringify(this.userId.ids)
                )}&model=${encodeURIComponent(JSON.stringify(res))}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}&deptId=${res.deptId}`,
                });
              }
            }
          } else if (res.status == "0000") {
            this.showSelectAccount();
          } else {
            this.flag = true;
            uni.navigateTo({
              url: `../scanning_djEnd/scanning_djEnd?type=${this.tabType}&type1=${
              res.type
            }&code=${this.code}&dept=${res.dept}&ids=${encodeURIComponent(
              JSON.stringify(this.userId.ids)
            )}&model=${encodeURIComponent(JSON.stringify(res))}&deptId=${res.deptId}`,
            });
          }
        });
      },
      // 底部扫描
      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") {
                        //快捷建单
                        if(res.data.isDigitalHandover == 1){
                          uni.navigateTo({
                            url: `../specimenChecking/specimenChecking?infoDATA=${encodeURIComponent(
                            JSON.stringify(res.data)
                          )}`,
                          });
                        }else{
                          uni.navigateTo({
                            url: `../shortcutbuildOrders/shortcutbuildOrders?infoDATA=${encodeURIComponent(
                            JSON.stringify(res.data)
                          )}`,
                          });
                        }
                      } else if (res.type == "dept") {
                        // 扫描科室码
                        // code: data.code //二维码
                        let infoDATA = res.data;
                        console.log(infoDATA)
                        // 判断是否是标本配送，并且仅仅只有这一种业务类型，默认全选,并跳转到下一步;
                        // 判断是否是标本轮巡 ，并且仅仅只有这一种业务类型，并且只有一个工单，默认选中,并跳转到下一步;
                        if(Object.keys(infoDATA).length === 1 && (infoDATA.specimen || (infoDATA.specimenPlan && infoDATA.specimenPlan.length === 1))){
                          this.infoDATA = infoDATA;
                          this.workData = infoDATA.specimen || infoDATA.specimenPlan;
                          this.tabType = infoDATA.specimen ? 'specimen' : 'specimenPlan';
                          this.code = data.code;
                          this.selectArr = [];
                          for (var i = 0; i < this.workData.length; i++) {
                            this.selectArr.push(this.workData[i].id);
                          }
                          this.userId.ids = this.selectArr;
                          this.orderDeptHandler(true);
                        }else{
                          uni.navigateTo({
                            url: `../scanning_all/scanning_all?infoDATA=${encodeURIComponent(
                            JSON.stringify(infoDATA)
                          )}&code=${data.code}&deptName=${res.deptName}`,
                          });
                        }
                      } else if (res.type == 'drugsBag') {
                        //扫药包码
                        uni.navigateTo({
                          url: `/pages/scanning_drug/scanning_drug?drugsBagId=${res.drugsBagId}`,
                        });
                      } else if (res.type == 'nucleicAcid') {
                        //扫核酸码
                        uni.navigateTo({
                          url: `/pages/scanning_nucleicAcid/scanning_nucleicAcid?qrcode=${data.code}`,
                        });
                      } else if (res.type == 'blood') {
                        //扫血制品
                        uni.navigateTo({
                          url: `/pages/scanning_blood/scanning_blood?qrcode=${data.code}`,
                        });
                      }
                    } 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: 30rpx;
    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>
