<template>
  <view class="Scanning_Result">
    <view class="Scanning_top">
      <view class="Scanning_top_icon">
        <text class="cubeic-ok icon_transport transport-duigou"></text>
      </view>
      <view class="Scanning_top_text">
        <view class="text1"> 签到成功 </view>
      </view>
    </view>

    <view class="Scanning_cont">
      <view>您已到达{{queryObj.deptName}}，请核对以下数量</view>
      <view class="table_head">
        <view>科室<text class="red ml16">{{deptTotalCount}}</text></view>
        <view>患者<text class="red ml16">{{patientTotalCount}}</text></view>
        <view>血制品<text class="red ml16">{{bloodTotalCount}}</text></view>
      </view>
      <view class="table_bodys">
        <view class="table_body" v-for="(item, index) in dataList" :key="index" @click="toDetail(item)">
          <view>{{item.dept}}</view>
          <view>{{item.patientCount}}</view>
          <view>{{item.bloodCount}}</view>
        </view>
      </view>
    </view>

    <view class="foot_btn_spe">
      <view class="btn1" @click="complete()" v-if="orderIds">核对完成</view>
      <view class="btn3" @click="goBack()">取消</view>
    </view>
    <!-- 填写交接人工号弹窗 -->
    <selectAccount @click.stop.native v-if="hosModels.disjunctor" :content="hosModels.content" :disjunctor="hosModels.disjunctor" @ok="hosOk"
      @cancel="hosCancel">
    </selectAccount>
  </view>
</template>
<script>
  import {
    get,
    post,
    SM,
    webHandle
  } from "@/http/http.js";
  export default {
    data() {
      return {
        orderIds: '',
        bloodIds: '',
        hosId: uni.getStorageSync('userData').user.currentHospital.id,
        // 填写交接人工号弹窗model
        hosModels: {
          disjunctor: false,
        },
        deptTotalCount: 0,
        patientTotalCount: 0,
        bloodTotalCount: 0,
        dataList: [],
        queryObj: {}, //路由传递过来的数据
      };
    },
    methods: {
      // 核对完成
      complete(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post('/simple/data/fetchDataList/taskTypeConfig', {
          idx: 0,
          sum: 1,
          taskTypeConfig: {
            "taskTypeDTO": {
                "hosId": {
                    "id": this.hosId
                },
                "ordinaryField": {
                    "key": "ordinary_field",
                    "value": "blood"
                }
            }
          },
        }).then(res => {
          uni.hideLoading();
          if(res.status == 200){
           let list = res.list || [];
           if(list.length > 0){
             let signTypeList = list[0].signTypeList || [];
             let signType = signTypeList.find( v => v.value == 5 );
             if(signType){
               this.showSelectAccount();
             }else{
               this.checkComplete();
             }
           }else{
            uni.showToast({
              icon: "none",
              title: "请配置血制品任务类型！",
            });
           }
          }else{
            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;
        this.checkComplete(data);
      },
      // 填写交接人工号-取消
      hosCancel() {
        this.hosModels.disjunctor = false;
        this.flag = true;
      },
      // 填写交接人工号弹窗
      showSelectAccount() {
        this.hosModels = {
          content: "确定核对完成,请输入交接人员工号",
          disjunctor: true,
        };
      },
      checkComplete(accountObj){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        let postData = {
          "type": "bloodTake",
          "orderId": 0,
          "bloodIds": this.bloodIds || undefined,
        };
        if (accountObj) {
          postData.handoverUser = accountObj.accountId;
        }
        post('/transflow/checkComplete', postData).then(res => {
          uni.hideLoading();
          if(res.state == 200){
             uni.navigateTo({
               url: `/pages/receiptpage/receiptpage`,
             });
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        })
      },
      // 返回
      goBack() {
        uni.navigateBack();
      },
      // 跳转血制品列表
      toDetail(data){
        uni.navigateTo({
          url: `/pages/newBlood/startOrderSignBloodDetail/startOrderSignBloodDetail?bloodIds=${data.bloodIds}&deptName=${data.dept}`,
        });
      },
      //获取页面信息
      getInfo(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post('/transflow/scanInfo', {
          "code": "nb",
          "id": 0,
          "type": "bloodTake",
          "deptOrder": true,
          "deptId": this.queryObj.deptId,
          "taskTypeId": this.queryObj.taskTypeId,
        }).then(res => {
          uni.hideLoading();
          if(res.state == 200){
            this.orderIds = res.data.orderIds;
            this.dataList = res.data.data || [];
            this.deptTotalCount = this.dataList.length;
            this.bloodIds = this.dataList.reduce((pre, current) => pre + current.bloodIds + ',', '').slice(0, -1);
            this.patientTotalCount = this.dataList.reduce((pre, current) => pre + current.patientCount, 0);
            this.bloodTotalCount = this.dataList.reduce((pre, current) => pre + current.bloodCount, 0);
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        })
      },

    },
    onLoad(options) {
      console.log(options, "result");
      this.queryObj = options;
      this.getInfo();
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
    // 监听自定义事件并进行页面刷新操作
    onShow() {
      uni.$on('refresh', (data) => {
        if (data.refresh) {
            // 刷新操作
          this.getInfo();
        }
      });
    },
    onUnload() {
      uni.$off('refresh'); // 需要手动解绑自定义事件
    }
  };
</script>
<style lang="less" scoped>
  .Scanning_Result {
    padding: 0 20rpx;
    display: flex;
    flex-direction: column;
    height: 100vh;

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

        .speNum{
          text-align: center;
          font-size: 140rpx;
        }

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

        .cubeic-close {
          font-size: 140rpx;
          color: #ff3b53;
        }
      }

      .Scanning_top_text {
        .text1 {
          font-size: 48rpx;
          text-align: center;
        }

        .success_tips {
          color: red;
          font-size: 30rpx;
        }
      }
    }

    .Scanning_cont {
      flex: 1;
      text-align: center;
      display: flex;
      flex-direction: column;

      .table_bodys{
        overflow-y: auto;
        flex: 1;
      }

      .table_head{
        display: flex;
        margin-top: 50rpx;
        .ml16{
          margin-left: 16rpx;
        }

        view {
          height: 110rpx;
          line-height: 1;
          flex: 1;
          font-weight: bold;
          color: #000;
          font-size: 38rpx;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }

      .table_body{
        background: #F3FAF7;
        border-radius: 8px;
        border: 1px solid #E9E9E9;
        display: flex;
        margin-bottom: 20rpx;
        padding: 30rpx 20rpx;
        view {
          line-height: 1;
          flex: 1;
          color: #555;
          font-size: 38rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          word-break: break-all;
        }
      }
    }

    .foot_btn_spe {
      line-height: 64rpx;
      height: 64rpx;
      margin-bottom: 40rpx;
      text-align: center;
      display: flex;
      justify-content: space-between;

      view {
        height: 64rpx;
        flex: 1;
        margin: 0 1%;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        color: #fff;
        border-radius: 8rpx;
        font-size: 26rpx;
      }
    }
  }
</style>
