<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>申请科室 : {{ infoDATA.sickRoom || "-" }}</view>
      <view>检验科室 : {{ infoDATA.checkDept || "-" }}</view>
      <view>检验项目 : {{ res.data.specimenDesc || "-" }}</view>
      <view>患者姓名 : {{ res.data.patientName}}<text v-if="res.data.bedNum">({{res.data.bedNum}})</text></view>
      <view>住院号 : {{ res.data.residenceNo || "-" }}</view>
    </view>
    <view class="Scanning_cont" v-else>
      <view>{{ res.msg }}</view>
    </view>
    <view class="foot_btn">
      <view class="btn3" @click="showAlert()">知道了</view>
      <block v-if="res.scanCodeSpecimenOrderSign == 1 && res.data.speState && res.data.speState.value != 8">
        <view class="btn3" @click="buildAndOrderSign(res.data.id,res.workOrder.id)"
          v-if="res.workOrder&&(res.workOrder.gdState.value == 2||res.workOrder.gdState.value == 4)">接单并签到</view>
        <!-- <view class="btn3" @click="orderSign(res.workOrder)"
          v-else-if="res.workOrder&&res.workOrder.gdState.value != 2&&res.workOrder.gdState.value != 4">执行工单</view> -->
        <view class="btn3" @click="buildAndOrderSign(res.data.id)" v-if="!res.workOrder && res.data">建单并签到</view>
      </block>
      <view class="btn3" @click="back(res.data,res.data.gdid)"
        v-if="res.data.speState&&(res.data.speState.value == 2||res.data.speState.value == 3||res.data.speState.value == 4||res.data.speState.value == 5)">
        退回</view>
    </view>
    <!-- 填写交接人账号弹窗 -->
    <selectAccount v-if="hosModels.disjunctor" :title="hosModels.title" :disjunctor="hosModels.disjunctor" @ok="hosOk"
      @cancel="hosCancel">
    </selectAccount>
    <!-- 退回弹窗 -->
    <backModel v-if="backModels.disjunctor" :title="backModels.title" :disjunctor="backModels.disjunctor" @ok="backOk"
      @cancel="backCancel">
    </backModel>
    <!-- 弹窗 -->
    <showModel :title="models.title" :icon="models.icon" :disjunctor="models.disjunctor" :content="models.content"
      :operate="models.operate" @know="know"></showModel>
  </view>
</template>
<script>
  import {
    post,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        backParams: {},
        workOrderId: 0,
        // 弹窗model
        models: {
          disjunctor: false,
        },
        backSpecimen: {},
        backStatus: '',
        infoDATA: {},
        res: {},
        // 填写交接人账号弹窗model
        hosModels: {
          disjunctor: false,
        },
        // 退回弹窗model
        backModels: {
          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}&specimenId=${res.specimenId}&fromSpecimenBuild=1`,
              });
            } 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&qrcode=${this.currentCode}`,
              });
            }
          });
        }
      },
      // 退回
      back(specimen, workOrderId) {
        this.backSpecimen = specimen;
        this.backStatus = 'back';
        this.workOrderId = workOrderId;
        this.showBack();
      },
      // 执行工单
      // orderSign(workOrder) {
      //   this.type = 'orderSign';
      //   uni.showModal({
      //     title: '提示',
      //     content: '是否执行工单?',
      //     success: (res) => {
      //       if (res.confirm) {
      //         console.log('用户点击确定');
      //         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: result.info || "接口获取数据失败!",
      //             });
      //           }
      //         });
      //       } else if (res.cancel) {
      //         console.log('用户点击取消');
      //       }
      //     }
      //   });
      // },
      // 接单并签到或建单并签到
      buildAndOrderSign(speId, orderId) {
        this.type = 'buildAndOrderSign';
        if (orderId) {
          // 接单并签到
          this.type = 'specimenAssignAndSign';
        } else {
          // 建单并签到
          this.type = 'buildAndOrderSign';
        }
        uni.showModal({
          title: '提示',
          content: `是否${this.type == 'specimenAssignAndSign'?'接单并签到':'建单并签到'}?`,
          success: (res) => {
            if (res.confirm) {
              console.log('用户点击确定');
              uni.showLoading({
                title: "加载中",
                mask: true,
              });
              let postData = {
                speId
              }
              if (orderId) {
                // 接单并签到
                postData.orderId = orderId;
              }
              post("/workerOrder/specimenCreateAndSign", postData).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}&specimenId=${speId}&fromSpecimenBuild=1`,
                    });
                  } 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 || "接口获取数据失败!",
                  });
                }
              });
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }
        });
      },
      know() {
        this.models.disjunctor = false;
        if (this.models.icon === 'success') {
          uni.navigateTo({
            url: "../receiptpage/receiptpage",
          });
        }
      },
      // 退回
      backHandler(specimen, workorderId, data) {
        console.log(specimen);
        const {
          reasonForReturn,
          remarks,
        } = this.backParams;
        uni.showLoading({
          title: '加载中',
          mask: true
        })
        let postData = {
          reasonForReturn: reasonForReturn.id,
          remarks,
          scode: specimen.scode,
          hosId: uni.getStorageSync("userData").user.currentHospital.id,
          speState: specimen.speState ? specimen.speState.id : undefined,
          handoverId: data ? data.accountId : undefined,
          handoverName: data ? data.accountName : undefined,
          gdId: workorderId || undefined,
        }
        post('/workerOrder/returnSpecimen', postData).then(res => {
          uni.hideLoading();
          if (res.state == 200) {
            this.models = {
              disjunctor: true,
              title: "提示",
              content: `标本退回成功`,
              icon: "success",
            };
          } else {
            this.models = {
              disjunctor: true,
              title: "提示",
              content: `标本退回失败`,
              icon: "error",
            };
          }
        })
      },
      // 填写交接人账号-确认
      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,
        });
        if (this.backStatus = 'back') {
          // 退回
          this.backHandler(this.backSpecimen, this.res.data.gdid, data);
        } else {
          this.nextDeptOrder_s(this.res.workOrder, data);
        }
      },
      // 填写交接人账号-取消
      hosCancel() {
        this.hosModels.disjunctor = false;
      },
      // 填写交接人账号弹窗
      showSelectAccount() {
        this.hosModels = {
          title: "填写交接人账号",
          disjunctor: true,
        };
      },
      // 知道了
      showAlert() {
        uni.navigateTo({
          url: "../receiptpage/receiptpage",
        });
      },
      // 退回-确认
      backOk(data) {
        console.log(data);
        this.backParams = data;
        this.hosModels.disjunctor = false;
        let postData = {
          "idx": 0,
          "sum": 1,
          "hospitalConfig": {
            "hosId": uni.getStorageSync("userData").user.currentHospital.id,
            "key": "returnSpecimenWhetherHandover"
          }
        };
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post('/simple/data/fetchDataList/hospitalConfig', postData).then((result) => {
          uni.hideLoading();
          if (result.status == 200) {
            if (result.list[0].value == 1 && this.backParams.reasonForReturn.extra1 == 1) {
              this.backCancel();
              this.showSelectAccount();
            } else {
              this.backHandler(this.backSpecimen, this.workOrderId);
            }
          } else {
            uni.showToast({
              icon: "none",
              title: result.msg || "接口获取数据失败!",
            });
          }
        })
      },
      // 退回-取消
      backCancel() {
        this.backModels.disjunctor = false;
      },
      // 退回弹窗
      showBack() {
        this.backModels = {
          title: "退回",
          disjunctor: true,
        };
      },
    },
    onLoad(options) {
      this.res = JSON.parse(options.res);
      this.infoDATA = JSON.parse(options.infoDATA); //详细信息
      console.log(this.infoDATA);
      console.log(this.res);
      // #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>