<template>
  <view class="bloodDetails">
    <view class="Scanning_top">
      <view class="Scanning_top_icon">
        <text class="cubeic-ok newicon newicon-duigou"></text>
      </view>
      <view class="Scanning_top_text">
        <view class="text1">扫描成功</view>
      </view>
    </view>
    <view class="page_item_wrap">
      <view class="page_item">
        <view class="page_item_top">
          <view class="page_item_top-inner">
            <view class="page_item_top_L">
              <text class="L_text">血袋号：{{ bloodDTO.bloodCode||'无' }}</text>
            </view>
            <view class="page_item_top_R">
              <text class="L_iocn"></text>
            </view>
          </view>
        </view>
        <view class="page_item_cont">
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>血液类型</text>
              <text class="text_big">
                <text>
                  {{bloodDTO.type ? bloodDTO.type.name : "无"}}
                </text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>血量</text>
              <text class="text_big">
                <text>{{ bloodDTO.volume?bloodDTO.volume+'ML':'无' }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>采血日期</text>
              <text class="text_big">
                <text v-if="bloodDTO.printDate">{{bloodDTO.printDate | formatDate('yyyy-MM-dd hh:mm')}}</text>
                <text v-else>无</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>有效期至</text>
              <text class="text_big">
                <text v-if="bloodDTO.overDate">{{bloodDTO.overDate | formatDate('yyyy-MM-dd hh:mm')}}</text>
                <text v-else>无</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>血型</text>
              <text class="text_big">
                <text>{{ bloodDTO.aboType||'无' }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>RH(D)</text>
              <text class="text_big">{{bloodDTO.rhType || '无'}}</text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>患者</text>
              <text class="text_big">
                <text>{{ bloodDTO.patientName }}<text v-if="bloodDTO.hosNum">({{ bloodDTO.hosNum }})</text></text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>申请科室</text>
              <text class="text_big">
                <text>{{ bloodDTO.applyDeptDTO?bloodDTO.applyDeptDTO.dept:'无' }}</text>
              </text>
            </view>
          </view>
        </view>
        <view class="L"></view>
        <view class="R"></view>
      </view>
      <view class="L-l"></view>
      <view class="R-l"></view>
    </view>
    <view class="cube-toolbar-item-wrap" v-show="bloodDTO.id">
      <button class="cube-toolbar-item" v-show="orderId && receiveOrder" @click="receiveOrderAndSign()">
        接单并签到
      </button>
      <button class="cube-toolbar-item" v-show="!orderId && buildOrder" @click="buildOrderAndSign()">
        建单并签到
      </button>
      <button class="cube-toolbar-item" v-show="signDeptOrder" @click="signDeptOrderAndSign()">
        指定科室建单
      </button>
      <button class="cube-toolbar-item" @click="goBack()">
        返回
      </button>
    </view>
  </view>
</template>
<script>
  import {
    webHandle,
    post
  } from "../../http/http.js";
  export default {
    data() {
      return {
        options: {},
        receiveOrder: false,
        buildOrder: false,
        signDeptOrder: false,
        bloodDTO: {}, //血制品信息
        taskTypeConfig: {}, //血制品配置信息
        orderId:'',//工单id
        gotoFlag: true,
				configs:null,
				hosId:null,
				orderState:null
      };
    },
    methods: {
      goBack() {
        uni.navigateTo({
          url: "../receiptpage/receiptpage",
        });
      },
      // 接单并签到
      receiveOrderAndSign() {
        if (!this.gotoFlag) {
          return;
        }
				this.gotoFlag = false;
				let content = null
				if(this.configs.sendingMergeOrder &&
					this.configs.sendingMergeOrder==1 &&
					this.orderState==5){
					content = "您有当前科室未送达的血制品工单，将为您合并工单。"
				}else{
					content = "请确认是否接单并签到？"
				}
        uni.showModal({
          title: "提示",
          content: content,
          success: (result) => {
            if (result.confirm) {
              console.log("用户点击确定");
              let postData = {
                type: 'blood',
                id: this.bloodDTO.id,
                orderId: this.orderId
              };
              uni.showLoading({
                title: "加载中",
                mask: true,
              });
              post("/transflow/createOrTakeOrder", postData).then((ress) => {
                uni.hideLoading();
                if (ress.state == 200) {
                  uni.navigateTo({
                    url: `../scanning_blood_process/scanning_blood_process?orderId=${ress.data.orderId}&bloodDTO=${encodeURIComponent(JSON.stringify(this.bloodDTO))}&scanCount=${ress.data.scanCount}&status=${ress.state}`,
                  });
                } else {
                  uni.showToast({
                    icon: "none",
                    title: ress.msg || "接口获取数据失败！",
                  });
                }
              });
            } else if (result.cancel) {
							this.gotoFlag = true;
              console.log("用户点击取消");
            }
          },
        });
      },
      // 建单并签到
      buildOrderAndSign() {
        if (!this.gotoFlag) {
          return;
        }
        this.gotoFlag = false;
        uni.showModal({
          title: "提示",
          content: "请确认是否建单并签到？",
          success: (result) => {
            if (result.confirm) {
              console.log("用户点击确定");
              let postData = {
                type: 'blood',
                id: this.bloodDTO.id,
              };
              uni.showLoading({
                title: "加载中",
                mask: true,
              });
              post("/transflow/createOrTakeOrder", postData).then((ress) => {
                uni.hideLoading();
                if (ress.state == 200) {
                  uni.navigateTo({
                    url: `../scanning_blood_process/scanning_blood_process?orderId=${ress.data.orderId}&bloodDTO=${encodeURIComponent(JSON.stringify(this.bloodDTO))}&scanCount=${ress.data.scanCount}&status=${ress.state}`,
                  });
                } else {
                  uni.showToast({
                    icon: "none",
                    title: ress.msg || "接口获取数据失败！",
                  });
                }
              });
            } else if (result.cancel) {
							this.gotoFlag = true;
              console.log("用户点击取消");
            }
          },
        });
      },
      // 指定科室建单
      signDeptOrderAndSign(){
        uni.showModal({
          title: "提示",
          content: "请选择血制品要送达的科室！",
          success: (result) => {
            if (result.confirm) {
              console.log("用户点击确定");
              uni.navigateTo({
                url: `../search/search?type=bloodSelect&bloodDTO=${encodeURIComponent(JSON.stringify(this.bloodDTO))}`,
              });
            } else if (result.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      },
      //获取血制品信息及其关联的工单信息
      scanInfo(value, type) {
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/common/common/getDictionary", {"type":"list","key":"ttconfig_sign_type"}).then((res1) => {
          post("/transflow/scanInfo", {
            type: 'blood',
            [type]: value,
          }).then((res) => {
            uni.hideLoading();
            if (res.state == 200) {
              this.bloodDTO = res.data.dto || {};
              this.orderId = res.data.orderId;
							this.orderState = res.data.orderState
              console.log(this.orderId);
              this.taskTypeConfig = res.data.taskTypeConfig;
              let sign_ids = this.taskTypeConfig.signTypeIds?this.taskTypeConfig.signTypeIds.split(','):[];
              console.log(sign_ids);
              this.receiveOrder = false;
              this.buildOrder = false;
              this.signDeptOrder = false;
              sign_ids.forEach(v => {
                let obj = res1.find(vv => vv.id == v);
                console.log(obj);
                // 接单
                if(obj && obj.value == 1){
                  this.receiveOrder = true;
                }
                // 建单
                if(obj && obj.value == 2){
                  this.buildOrder = true;
                }
                // 指定科室建单
                if(obj && obj.value == 3){
                  this.signDeptOrder = true;
                }
              })
            } else {
              uni.showToast({
                icon: "none",
                title: res.msg || "接口获取数据失败！",
              });
            }
          });
        });
      },
			//获取血制品配置页面
			getTaskConfig(){
			  let postData = {
			      "idx": 0,
			      "sum": 10,
			      "taskTypeConfig": {
			          "taskTypeDTO": {
			              "hosId": {
			                  "id": this.hosId
			              },
			              "ordinaryField": {
			                  "key": "ordinary_field",
			                  "value": "blood"
			              }
			          }
			      }
			  };
			  post('/simple/data/fetchDataList/taskTypeConfig', postData).then(result => {
			    if(result.status == 200){
			      this.configs = result.list[0] || {};
			    }else{
			      uni.showToast({
			        icon: "none",
			        title: result.msg || "接口获取数据失败！",
			      });
			    }
			  })
			},
    },
    onShow() {
      this.gotoFlag = true;
			// #ifdef APP-PLUS
			uni.$off('scan') // 每次进来先 移除全局自定义事件监听器
			// uni.$on('scan', function(data) {
			// 	that.pdaChange(data)
			// })
			// #endif
    },
    onLoad(options) {
			this.hosId = uni.getStorageSync('userData').user.currentHospital.id;
      console.log(options);
      this.options = options;
      if (this.options.qrcode) {
        this.scanInfo(this.options.qrcode, 'code');
      }else if (this.options.id) {
        this.scanInfo(this.options.id, 'id');
      }
			this.getTaskConfig()
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
		created(){
		}
  };
</script>
<style lang="less" scoped>
  .bloodDetails {
    background-color: rgb(249, 250, 251);
    .Scanning_top {

      .Scanning_top_icon {
        width: 140rpx;
        height: 140rpx;
        margin: 0 auto;
        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;
        }
      }
    }

    .page_item_wrap {
      position: relative;
      margin-top: 32rpx;

      .page_item {
        margin-top: 16rpx;
        margin-bottom: 124rpx;
        background: #fff;
        border-radius: 8rpx;
        margin: 0 20rpx;
        border: 2rpx solid #e5e9ed;
        position: relative;
        overflow: hidden;
        padding: 0 16rpx;

        .L {
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          background: #f9fafb;
          position: absolute;
          left: -20rpx;
          top: 68rpx;
          border: 2rpx solid #e5e9ed;
        }

        .R {
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          background: #f9fafb;
          position: absolute;
          float: right;
          right: -20rpx;
          top: 68rpx;
          border: 2rpx solid #e5e9ed;
        }

        .page_item_top {
          height: 88rpx;
          border-bottom: 2rpx dashed #e5e9ed;
          padding: 0 16rpx;

          .page_item_top-inner {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 100%;

            .page_item_top_L {
              .L_text {
                font-size: 32rpx;
                font-weight: 700;
              }
            }

            .page_item_top_R {
              font-size: 32rpx;

              .L_iocn {
                color: rgb(7, 134, 60);
                font-size: 36rpx;
                font-weight: 700;
              }
            }
          }
        }

        .page_item_cont {
          min-height: 180rpx;
          padding: 0 16rpx;
          text-align: left;
          position: relative;

          .text_big {
            width: 50%;
            text-align: right;
            font-size: 32rpx;
            font-weight: 700;

            text {
              font-weight: 700;
              line-height: 1.5;
            }
          }

          .line {
            height: 20rpx;
            width: 2rpx;
            border-left: 2rpx solid #e5e9ed;
            position: absolute;
            top: 82rpx;
            left: 40rpx;
          }

          .page_item_cont_T {
            padding-top: 28rpx;
            font-size: 28rpx;

            .page_item_cont_title {
              height: 100%;
              font-size: 32rpx;
              display: flex;
              justify-content: space-between;
            }
          }

          .page_item_cont_B {
            padding-top: 28rpx;
            margin-bottom: 28rpx;

            .page_item_cont_title {
              font-size: 32rpx;
              display: flex;
              justify-content: space-between;
              align-items: center;
            }
          }

          .page_item_cont_C {
            margin-bottom: 28rpx;

            .page_item_cont_title_C {
              font-size: 32rpx;
              display: flex;
              justify-content: space-between;
              align-items: center;
              &>text{
                flex-shrink: 0;
              }
            }
          }

          #infos {
            display: none;
          }
        }
      }

      .L-l {
        width: 2rpx;
        height: 40rpx;
        background: #f9fafb;
        position: absolute;
        left: 20rpx;
        top: 72rpx;
      }

      .R-l {
        width: 2rpx;
        height: 40rpx;
        background: #f9fafb;
        position: absolute;
        right: 20rpx;
        top: 72rpx;
      }
    }

    .cube-toolbar-item-wrap {
      display: flex;
      position: fixed;
      left: 0;
      bottom: 20rpx;
      width: 100%;
      .cube-toolbar-item {
        flex: 1;
        margin: 0 1%;
        height: 68rpx;
        line-height: 68rpx;
        border-radius: 8rpx;
        background: linear-gradient(to right, #72c172, #3bb197);
        font-size: 28rpx;
        color: #fff;
        text-align: center;
      }
    }

    .btn-wrap {
      display: flex;
      justify-content: space-between;
      position: fixed;
      left: 20rpx;
      bottom: 160rpx;
    }

    .cube-toolbar-item1 {
      width: 350rpx;
      height: 68rpx;
      line-height: 68rpx;
      border-radius: 8rpx;
      margin: 0 5rpx;
      background: linear-gradient(to right, #72c172, #3bb197);
      font-size: 36rpx;
      color: #fff;
      text-align: center;
    }
  }
</style>
