<template>
  <view class="Scanning_B">
    <view class="Scanning_top" v-if="res.status == 200 || res.status == 666">
      <view class="Scanning_top_icon">
        <text class="cubeic-ok icon_transport transport-duigou"></text>
      </view>
      <view class="Scanning_top_text">
        <view class="text1" v-if="res.status == 200">扫描成功</view>
        <view class="text1" v-else-if="res.status == 666">查询成功</view>
      </view>
    </view>
    <view class="Scanning_top" v-if="res.status != 200 && res.status != 666">
      <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 || res.status == 666">
      <view>标本类型 : {{ infoDATA.stype.name || "-" }}</view>
      <view>标本编码 : {{ infoDATA.scode || "-" }}</view>
      <view>标本状态 : {{ res.data.speState ? res.data.speState.name : "-" }}</view>
      <view>患者姓名 : {{ res.data.patientName || "-" }}</view>
      <view>住院号 : {{ res.data.residenceNo || "-" }}</view>
      <view>申请科室 : {{ infoDATA.sickRoom || "-" }}</view>
      <view>检查科室 : {{ infoDATA.checkDept || "-" }}</view>
      <view>检验项目 : {{ res.data.specimenDesc || "-" }}</view>
    </view>
    <view class="Scanning_cont" v-else>
      <view>{{ res.msg }}</view>
    </view>
    <view class="foot_btn" v-if="res.status != 666">
      <view class="btn3" @click="showAlert()">知道了</view>
      <block v-if="res.scanCodeSpecimenOrderSign == 1">
        <view class="btn3" @click="orderSign(res.workOrder)" v-if="res.workOrder">执行工单</view>
        <view class="btn3" @click="buildAndOrderSign(res.data.id)" v-if="!res.workOrder && res.data">建单并签到</view>
      </block>
    </view>
    <!-- 填写交接人账号弹窗 -->
    <selectAccount v-if="hosModels.disjunctor" :title="hosModels.title" :disjunctor="hosModels.disjunctor" @ok="hosOk"
      @cancel="hosCancel">
    </selectAccount>
  </view>
</template>
<script>
  import {
    post,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        infoDATA: {},
        res: {},
        // 填写交接人账号弹窗model
        hosModels: {
          disjunctor: false,
        },
        currentCode: "",
        type: "", //类型,orderSign|buildAndOrderSign
      };
    },
    methods: {
      // 如果不是患者陪检或患者转运或其他
      // 科室签到
      nextDeptOrder_s(data, accountObj) {
        console.log(data, accountObj);
        let ids = [];
        let id = data.id;
        ids.push(id);
        let code = "";
        let postData = {
          ids,
        };
        if (this.type === 'orderSign') {
          postData.specimenOrderSign = data.specimenSet.map(v => v.id); //破坏性,必须扫标本
        }
        if (accountObj) {
          postData.handover = [accountObj.accountId];
        }
        if (this.currentCode) {
          code = this.currentCode;
          // 科室签到
          post("/workerOrder/orderSign/" + code, postData).then((res) => {
            uni.hideLoading();
            if (res.status == 200) {
              // 跳转到扫描科室
              // type1: res.type, //type类型
              // id: data.id, //工单ID
              // deptCode: code, //二维码
              // dept: res.dept //科室名称
              uni.navigateTo({
                url: `/pages/scanning_code/scanning_code?type=${
                data.taskType.associationType.value
              }&type1=${res.type}&id=${data.id}&deptCode=${code}&dept=${
                res.dept
              }&accountObj=${encodeURIComponent(
                JSON.stringify(accountObj)
              )}&deptId=${res.deptId}`,
              });
            } else {
              uni.navigateTo({
                url: `/pages/scanning_Result/scanning_Result?type=${data.taskType.associationType.value}&type1=${res.type}&id=${data.id}&status=600&msg=${res.msg}&isKs=1`,
              });
            }
          });
        }
      },
      // 执行工单
      orderSign(workOrder) {
        this.type = 'orderSign';
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        let postData = {
          content: workOrder.startDept.qrcode,
          taskTypeId: workOrder.taskType.id,
          gdState: workOrder.gdState.id,
        };
        //检验二维码的有效性
        post("/dept/scanning", postData).then((result) => {
          this.currentCode = result.code;
          if (result.state == 200 || result.state == 201) {
            if (result.account) {
              this.nextDeptOrder_s(workOrder, {
                account: result.account,
                accountName: result.name,
                accountId: result.id,
              });
            } else {
              this.nextDeptOrder_s(workOrder);
            }
          } else if (result.state == "0000") {
            uni.hideLoading();
            this.showSelectAccount();
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: "请求失败!",
            });
          }
        });
      },
      // 建单并签到
      buildAndOrderSign(speId) {
        this.type = 'buildAndOrderSign';
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/specimenCreateAndSign", {
          speId,
        }).then((res) => {
          if (res.state == 200 || res.state == 201) {
            uni.hideLoading();
            if (res.status == 200) {
              // 跳转到扫描科室
              // type1: res.type, //type类型
              // id: data.id, //工单ID
              // deptCode: code, //二维码
              // dept: res.dept //科室名称
              uni.navigateTo({
                url: `/pages/scanning_code/scanning_code?type=${
                res.workOrder.taskType.associationType.value
              }&type1=${res.type}&id=${res.workOrder.id}&deptCode=${
                this.currentCode
              }&dept=${res.dept}&accountObj=${encodeURIComponent(
                JSON.stringify(undefined)
              )}&deptId=${res.deptId}`,
              });
            } else {
              uni.navigateTo({
                url: `/pages/scanning_Result/scanning_Result?type=${res.workOrder.taskType.associationType.value}&type1=${res.type}&id=${res.workOrder.id}&status=600&msg=${res.msg}&isKs=1`,
              });
            }
          } else if (res.state == "0000") {
            uni.hideLoading();
            this.currentCode = res.code;
            this.res.workOrder = res.workOrder;
            this.showSelectAccount();
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: res.msg || "请求失败!",
            });
          }
        });
      },
      // 填写交接人账号-确认
      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;
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        this.nextDeptOrder_s(this.res.workOrder, data);
      },
      // 填写交接人账号-取消
      hosCancel() {
        this.hosModels.disjunctor = false;
      },
      // 填写交接人账号弹窗
      showSelectAccount() {
        this.hosModels = {
          title: "填写交接人账号",
          disjunctor: true,
        };
      },
      // 知道了
      showAlert() {
        uni.navigateTo({
          url: "../receiptpage/receiptpage",
        });
      },
    },
    onLoad(options) {
      this.res = JSON.parse(options.res);
      this.infoDATA = JSON.parse(options.infoDATA); //详细信息
      console.log(this.infoDATA);
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less">
  .Scanning_B {
    padding: 0px 74rpx;

    .uni-modal__btn_primary {
      color: #49b856 !important;
    }

    .Scanning_top {
      height: 270rpx;

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

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

      view {
        margin-bottom: 16rpx;
        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;

      .btn3 {
        flex: 1;
        height: 88rpx;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        color: #fff;
        border-radius: 8rpx;
        font-size: 32rpx;
        margin-top: 16rpx;
        margin-right: 16rpx;
        text-align: center;

        &:last-of-type {
          margin-right: 0;
        }
      }
    }
  }
</style>