<template>
  <view class="Scanning_Result">
    <view class="Scanning_top">
      <view class="Scanning_top_icon">
        <text class="cubeic-ok newicon newicon-duigou"></text>
      </view>
    </view>

    <view class="Scanning_cont">
      <scroll-view scroll-y class="scrollContent">
        <view class="column">
          <view class="name">单号：</view>
          <view class="value">{{drugsBag.packid}}</view>
        </view>
        <view class="column">
          <view class="name">申请科室：</view>
          <view class="value">{{drugsBag.target ? drugsBag.target.dept : ''}}</view>
        </view>
        <view class="column">
          <view class="name">发药科室：</view>
          <view class="value">{{drugsBag.launch ? drugsBag.launch.dept : ''}}</view>
        </view>
        <view class="column">
          <view class="name">种类数：</view>
          <view class="value">{{drugsBag.drugsTypeCount}}</view>
        </view>
        <view class="column">
          <view class="name">药品数：</view>
          <view class="value">{{drugsBag.drugsCount}}</view>
        </view>
      </scroll-view>
    </view>

    <view class="foot_btn_spe">
      <view class="column">
        <view class="btn" @click="goIndex()">知道了</view>
        <view class="btn" @click="createOrBuildOrder()" v-if="queryObj.type === 'build' && config.drugsCreateSign === 1">建单并签到</view>
        <view class="btn" @click="createOrBuildOrder(orderId)" v-if="queryObj.type === 'receive' && config.drugsReceiveSign === 1">接单并签到</view>
      </view>
    </view>
  </view>
</template>
<script>
  import {
    get,
    post,
    SM,
    webHandle
  } from "@/http/http.js";
  export default {
    data() {
      return {
        queryObj: {}, //路由传递过来的数据
        drugsBag: {}, //药包信息
        config: {}, //药品的业务流程配置
        orderId: undefined, //根据药包查询到的工单ID
      };
    },
    methods: {
      // 知道了
      goIndex(){
       uni.redirectTo({
         url: `/pages/receiptpage/receiptpage`,
       });
      },
      // 根据配置跳转页面
      pageNavigateByConfig(config, responseData, queryObj){
        let drugsBagType = queryObj.drugsBagType;
        let orderId = responseData.orderId;
        let drugsBagId = queryObj.drugsBagId;
        let orderStateValue = responseData.orderStateValue;
        if(config.drugsModel === 1){
          // 一单一码
          if(responseData.orderStateValue == 4){
            // 待到达
            if(config.drugsStartCheck === 1){
              // 起点科室支持核对
              uni.navigateTo({
                url: `/pages/newDrug/checkPage/checkPage?drugsBagType=${drugsBagType}&orderId=${orderId}`,
              });
            }else{
              // 起点科室不支持核对
              if(config.drugsStartPhoto === 1){
                // 起点科室支持拍照留存
                uni.navigateTo({
                  url: `/pages/newDrug/photoRetention/photoRetention?drugsBagType=${drugsBagType}&orderId=${orderId}`,
                });
              }else{
                // 起点科室不支持拍照留存
                this.completeOrder(responseData, queryObj);
              }
            }
          }else if(responseData.orderStateValue == 5){
            // 待送达
            if(config.drugsEndCheck === 1){
              // 起点科室支持核对
              uni.navigateTo({
                url: `/pages/newDrug/checkPage/checkPage?drugsBagType=${drugsBagType}&orderId=${orderId}`,
              });
            }else{
              // 起点科室不支持核对
              if(config.drugsEndPhoto === 1){
                // 起点科室支持拍照留存
                uni.navigateTo({
                  url: `/pages/newDrug/photoRetention/photoRetention?drugsBagType=${drugsBagType}&orderId=${orderId}`,
                });
              }else{
                // 起点科室不支持拍照留存
                this.completeOrder(responseData, queryObj);
              }
            }
          }
        }else if(config.drugsModel === 2){
          // 一单多码
          uni.navigateTo({
            url: `/pages/newDrug/continueScanning/continueScanning?drugsBagType=${drugsBagType}&orderId=${orderId}&drugsBagId=${drugsBagId}&orderStateValue=${orderStateValue}`,
          });
        }
      },
      // 工单完成
      completeOrder(responseData, queryObj){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        let postData = {
          "type": queryObj.drugsBagType,
          "orderId": responseData.orderId,
        };
        post('/transflow/checkComplete', postData).then(res => {
          uni.hideLoading();
          if(res.state == 200){
            uni.showToast({
              icon: "none",
              title: responseData.orderStateValue == 4 ? "交接成功，请尽快送达科室！" : (responseData.orderStateValue == 5 ? "交接成功，完成配送！" : ""),
              duration: 60000,
              mask: true,
              complete(){
                setTimeout(() => {
                  uni.hideToast();
                  uni.redirectTo({
                    url: `/pages/receiptpage/receiptpage`,
                  });
                }, 2000)
              }
            });
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        })
      },
      // 建单并签到/接单并签到-orderId
      createOrBuildOrder(orderId){
        uni.showModal({
          title: "提示",
          content: `请确认是否${orderId ? '接' : '建'}单并签到？`,
          success: (result) => {
            if (result.confirm) {
              console.log("用户点击确定");
              let postData = {
                type: this.queryObj.drugsBagType,
                id: +this.queryObj.drugsBagId,
                orderId,
              };
              uni.showLoading({
                title: "加载中",
                mask: true,
              });
              post(`/transflow/createOrTakeOrder`, postData).then((ress) => {
                uni.hideLoading();
                if (ress.state == 200) {
                  if(ress.data.msg){
                    uni.showToast({
                      icon: "none",
                      title: ress.data.msg || "接口获取数据失败！",
                    });
                  }else{
                    this.pageNavigateByConfig(this.config, ress.data, this.queryObj);
                  }
                } else {
                  uni.showToast({
                    icon: "none",
                    title: ress.msg || "接口获取数据失败！",
                  });
                }
              });
            } else if (result.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      },
      //获取页面信息
      getInfo(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });

        post(`/transflow/scanInfo`, {type: this.queryObj.drugsBagType, id: +this.queryObj.drugsBagId, orderStateValue: '4'}).then(res => {
          uni.hideLoading();
          if(res.state == 200){
            if(res.data){
              this.drugsBag = res.data.dto || {};
              this.orderId = this.queryObj.orderId;
              this.config = res.data.taskTypeConfig || {};
            }
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        })
      },
    },
    onLoad(options) {
      console.log(options, "options");
      this.queryObj = options;
      this.getInfo();
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less" scoped>
  .Scanning_Result {
    background: #FAFBFD;
    padding: 0 24rpx;
    display: flex;
    flex-direction: column;
    height: 100vh;

    .Scanning_top {
      margin: 50rpx auto 64rpx;
      .Scanning_top_icon {
        width: 128rpx;
        height: 128rpx;
        border-radius: 50%;

        .cubeic-ok {
          font-size: 128rpx;
          color: #35b34a;
        }
      }
    }

    .Scanning_cont {
      flex: 1;
      min-height: 0;
      display: flex;
      flex-direction: column;
      .scrollContent{
        flex: 1;
        min-height: 0;
      }
      .column{
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 30rpx;
        margin-bottom: 32rpx;
        .name{
          font-weight: bold;
          word-break: break-all;
          flex-shrink: 0;
        }
        .value{
          word-break: break-all;
        }
      }
    }

    .foot_btn_spe {
      margin: 24rpx 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 24rpx;
      font-weight: bold;

      .column{
        width: 100%;
        height: 78rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 24rpx;

        .btn {
          height: 100%;
          flex: 1;
          background: linear-gradient( 90deg, #6FC073 0%, #3DB197 100%);
          color: #fff;
          border-radius: 4rpx;
          font-size: 30rpx;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
</style>
