<template>
  <view class="Scanning_Result">
    <view class="Scanning_top">
      <view class="Scanning_top_icon">{{scanCount}}</view>
      <view class="tips" v-if="workOrder.gdState && workOrder.gdState.value == 4">您已到达<text class="red">起点科室-{{workOrder.startDept.dept}},</text>请扫描核对药包码。</view>
      <view class="tips" v-if="workOrder.gdState && workOrder.gdState.value == 5">您已到达<text class="red">终点科室-{{workOrder.endDeptNames}}</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" v-if="config.showDrugsBagTypeCount == 1">
          <view class="name">种类数：</view>
          <view class="value">{{drugsBag.drugsTypeCount}}</view>
        </view>
        <view class="column" v-if="config.showDrugsBagTypeCount == 1">
          <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="Scanning_again()">继续扫描</view>
        <view class="btn" @click="hand_again()">手动录入</view>
      </view>
      <view class="column">
        <view class="btn" @click="checkHandover()">核对交接</view>
      </view>
    </view>
    <!-- 手动查询药品弹窗 -->
    <handViewDrugsBag v-if="drugbagModels.disjunctor" :title="drugbagModels.title"
      :disjunctor="drugbagModels.disjunctor" @ok="drugbagOk" @cancel="drugbagCancel">
    </handViewDrugsBag>
  </view>
</template>
<script>
  import {
    get,
    post,
    SM,
    webHandle
  } from "@/http/http.js";
  export default {
    data() {
      return {
        hosId: uni.getStorageSync('userData').user.currentHospital.id,
        config: {}, //配置
        workOrder: {}, //工单信息
        drugsBag: {}, //药包信息
        queryObj: {}, //路由传递过来的数据
        scanCount: '', //已扫描数量
        SMFlag: true,
        // 手动查询弹窗model
        drugbagModels: {
          disjunctor: false,
        },
      };
    },
    onShow() {
      this.SMFlag = true;
    },
    methods: {
      // 手动查询药品-确认
      drugbagOk(data) {
        console.log(data);
        if (!data.id) {
          //没有查询到药品
          uni.showModal({
            title: '提示',
            content: "没有查询到发药单号！",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定');
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
          return;
        }
        this.drugbagModels.disjunctor = false;
        this.hand_scanning_common(data.packid, 'hand');
      },
      // 手动查询药品-取消
      drugbagCancel() {
        this.drugbagModels.disjunctor = false;
      },
      // 手动查询药品弹窗
      showHandViewDrugsbag() {
        this.drugbagModels = {
          title: '填写发药单号',
          disjunctor: true,
        }
      },
      // 手动录入
      hand_again() {
        this.showHandViewDrugsbag();
      },
      // 核对交接
      checkHandover(){
        uni.navigateTo({
          url: `/pages/newDrug/checkPage/checkPage?drugsBagType=${this.queryObj.drugsBagType}&orderId=${this.queryObj.orderId}`,
        });
      },
      // 继续扫描
      Scanning_again(isFlag = false) {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((ress1) => {
          this.hand_scanning_common(ress1, 'scan', isFlag);
        }).catch(err => {
          this.SMFlag = true;
        });
      },
      // 手动输入和扫码公共方法
      hand_scanning_common(ress1, type, isFlag = false) {
        // ----------------
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        //检验二维码的有效性
        post("/dept/scanning", {
          content: ress1,
        }).then((result) => {
          this.SMFlag = true;
          if (result.state == 200 || result.state == 201) {
            let codes = result.code;
            if (codes) {
              this.input_common(ress1, type, isFlag);
            } else {
              uni.hideLoading();
            }
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: result.info || "接口获取数据失败！",
            });
          }
        });
        // ------------------------------
      },
      // 录入到工单
      input_common(ress1, type, isFlag = false){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/transflow/scanBind", { type: this.queryObj.drugsBagType, orderId: this.queryObj.orderId, code: ress1 }).then((ress) => {
          uni.hideLoading();
          if (ress.state == 200) {
            if(ress.data.msg){
              uni.showToast({
                icon: "none",
                title: ress.data.msg || "接口获取数据失败！",
              });
            }else{
              uni.redirectTo({
                url: `/pages/newDrug/continueScanning/continueScanning?drugsBagType=${this.queryObj.drugsBagType}&orderId=${this.queryObj.orderId}&scanOrHand=${type}&drugsBagId=${ress.data.dto.id}&orderStateValue=${this.queryObj.orderStateValue}`,
              });
            }
          } else {
            uni.showToast({
              icon: "none",
              title: ress.msg || "接口获取数据失败！",
            });
          }
        });
      },
      //获取页面信息
      getInfo(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        
        let info$ = post(`/transflow/scanInfo`, {type: this.queryObj.drugsBagType, id: +this.queryObj.drugsBagId, orderStateValue: this.queryObj.orderStateValue, orderId: +this.queryObj.orderId});
        let config$ = post(`/simple/data/fetchDataList/taskTypeConfig`, {
            "idx": 0,
            "sum": 1,
            "taskTypeConfig": {
                "taskTypeDTO": {
                    "hosId": {
                        "id": this.hosId
                    },
                    "ordinaryField": {
                        "key": "ordinary_field",
                        "value": this.queryObj.drugsBagType
                    }
                }
            }
        });
        
        Promise.all([info$, config$]).then(result => {
          uni.hideLoading();
          let [info, config] = result || [];
        
          if(info.state == 200){
            if(info.data){
              this.scanCount = info.data.scanCount;
              this.drugsBag = info.data.dto || {};
              let workOrder = info.data.workOrder || {};
              workOrder.endDeptNames = workOrder.endDepts.map(v=>v.dept).toString();
              this.workOrder = workOrder;
            }
          }else{
            uni.showToast({
              icon: "none",
              title: info.msg || "接口获取数据失败！",
            });
          }
          if(config.status == 200){
            let list  = config.list || [];
            this.config = list.length ? list[0] : {};
          }else{
            uni.showToast({
              icon: "none",
              title: config.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 {
      display: flex;
      flex-direction: column;
      align-items: center;
      .tips{
        margin: 24rpx 0 40rpx;
        text-align: justify;
        font-weight: bold;
        font-size: 30rpx;
      }
      .Scanning_top_icon {
        font-size: 140rpx;
        color: #FF0000;
        font-weight: bold;
      }
    }

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