<template>
  <view class="pharmacyDetails" v-show="drugsBagDto.id">
    <view class="pharmacyDetails_title">请领信息</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">请领单：{{ drugsBagDto.packid||'-' }}</text>
            </view>
            <view class="page_item_top_R">
              <text class="L_iocn">{{
                drugsBagDto.drugsState ? drugsBagDto.drugsState.name : "-"
              }}</text>
            </view>
          </view>
        </view>
        <view class="page_item_cont">
          <view class="page_item_cont_B">
            <view class="page_item_cont_title">
              <text>药包类型</text>
              <text class="text_big">
                <text>
                  {{(drugsBagDto.packType && drugsBagDto.packType !== 0) ? DRUGSBAG_TYPE[drugsBagDto.packType] : "-"}}
                </text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>记账时间</text>
              <text class="text_big">
                <text>{{ drugsBagDto.creatTime||'-' }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>请领科室</text>
              <text class="text_big">
                <text>{{
                  drugsBagDto.target ? drugsBagDto.target.dept : "-"
                }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C" v-show="workOrder.id">
            <view class="page_item_cont_title_C">
              <text>单号</text>
              <text class="text_big">
                <text>{{ workOrder.gdcode||'-' }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C" v-show="workOrder.id">
            <view class="page_item_cont_title_C">
              <text>状态</text>
              <text class="text_big">
                <text>{{
                  workOrder.gdState ? workOrder.gdState.name : "-"
                }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C" v-show="workOrder.id">
            <view class="page_item_cont_title_C">
              <text>服务人员</text>
              <text class="text_big">
                <text>{{ workOrder.worker ? workOrder.worker.name : "-" }}</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>
    <button class="cube-toolbar-item" v-show="isShowBtn" @click="receiveOrderAndSign()">
      接单并签到
    </button>
    <button class="cube-toolbar-item" v-show="!workOrder.id" @click="buildOrderAndSign()">
      建单并签到
    </button>
    <button class="cube-toolbar-item" v-show="!isShowBtn && workOrder.id" @click="goBack()">
      返回
    </button>
    <!-- 弹窗 -->
    <showModel :title="models.title" :icon="models.icon" :disjunctor="models.disjunctor" :content="models.content"
      @ok="ok" @cancel="cancel" :operate="models.operate"></showModel>
    <!-- 填写交接人工号弹窗 -->
    <selectAccount v-if="hosModels.disjunctor" :disjunctor="hosModels.disjunctor" @ok="hosOk"
      @cancel="hosCancel">
    </selectAccount>
  </view>
</template>
<script>
  import {
    DRUGSBAG_TYPE
  } from '../../tools/enum.drugsbagType.js';
  import selectAccount from "@/components/selectAccount/selectAccount";
  import {
    webHandle,
    post
  } from "../../http/http.js";
  export default {
    data() {
      return {
        hosId: null,
        DRUGSBAG_TYPE,
        // 弹窗model
        models: {
          disjunctor: false,
        },
        drugsBagDto: {}, //药包信息
        workOrder: {}, //药包关联的工单信息
        currentCode: "", //当前小扫描的科室二维码
        currentData: {}, //当前小扫描的工单对象
        // 填写交接人工号弹窗model
        hosModels: {
          disjunctor: false,
        },
        grabOrders: null,
        gotoFlag: true,
      };
    },
    components: {
      selectAccount,
    },
    computed: {
      isShowBtn() {
        if (this.workOrder.gdState) {
          let state = this.workOrder.gdState.value;
          // 待抢单，待接单,待到达,待送达
          return state == 2 || state == 3 || state == 4 || state == 5;
        } else {
          return false;
        }
      },
    },
    methods: {
      goBack() {
        uni.navigateTo({
          url: "../receiptpage/receiptpage",
        });
      },
      // 如果不是患者陪检或患者转运或其他
      // 科室签到
      nextDeptOrder_s(data, accountObj) {
        console.log(data, accountObj);
        let ids = [];
        let id = data.id;
        ids.push(id);
        let code = "";
        let postData = {
          ids,
        };
        if (accountObj) {
          postData.handover = [accountObj.accountId];
        }
        if (this.grabOrders) {
          postData.grabOrders = this.grabOrders;
        }
        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 //科室名称
              let urlObj = {
                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))}`,
              };
              if (this.grabOrders) {
                urlObj.url += `&grabOrders=666`;
              }
              uni.navigateTo(urlObj);
            } 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}`,
              });
            }
          });
        }
      },
      // 签到
      orderSign(data, type, ress1) {
        this.currentData = data;
        console.log(data, "data");
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        //检验二维码的有效性
        post("/dept/scanning", {
          content: ress1,
          taskTypeId: data.taskType.id,
          gdState: data.gdState.id,
        }).then((result) => {
          this.currentCode = result.code;
          if (result.state == 200 || result.state == 201) {
            if (result.account) {
              if (type == 2) {
                this.nextDeptOrder_s(this.currentData, {
                  account: result.account,
                  accountName: result.name,
                  accountId: result.id,
                });
              }
            } else {
              if (type == 2) {
                this.nextDeptOrder_s(this.currentData);
              }
            }
          } else if (result.state == "0000") {
            uni.hideLoading();
            this.showSelectAccount();
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: result.info || "接口获取数据失败！",
            });
          }
        });
      },
      // 填写交接人工号-确认
      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.commonBuild(data);
      },
      // 填写交接人工号-取消
      hosCancel() {
        this.hosModels.disjunctor = false;
        this.gotoFlag = true;
      },
      // 填写交接人工号弹窗
      showSelectAccount() {
        this.hosModels = {
          disjunctor: true,
        };
      },
      // 接单---data就是交接人信息
      commonBuild(data) {
        let postData = {
          packId: this.workOrder.drugs.packid,
          orderId: this.workOrder.id,
        };
        if (data) {
          //有交接人
          postData.handover = data.accountId;
        }
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/drugBagReceiving", postData).then((ress) => {
          uni.hideLoading();
          if (ress.status == 200) {
            this.grabOrders = ress.grabOrders ? ["666"] : null;
            // this.orderSign(ress.workOrder, 2, ress.content);
            let info = {
              targetDept: ress.data.target.dept, //所属科室
              packid: ress.data.packid, //药包编码
              batchNo: ress.data.batchNo, //药包批次
              status: ress.status,
              msg: ress.msg,
            };
            if (data) {
              //有交接人
              info.accountName = data.accountName; //交接人姓名
              info.account = data.account; //交接人工号
            }
            uni.navigateTo({
              url: `../scanning_result_seimin/scanning_result_seimin?info=${encodeURIComponent(
              JSON.stringify(info)
            )}`,
            });
          } else if (ress.status == "0000") {
            uni.hideLoading();
            this.showSelectAccount();
          } else {
            uni.showToast({
              icon: "none",
              title: ress.msg || "接口获取数据失败！",
            });
          }
        });
      },
      ok() {
        this.models.disjunctor = false;
        this.commonBuild();
      },
      // 取消
      cancel() {
        this.models.disjunctor = false;
        this.gotoFlag = true;
      },
      // 接单并签到
      receiveOrderAndSign() {
        if (!this.gotoFlag) {
          return;
        }
        this.gotoFlag = false;
        if (this.workOrder.gdState) {
          let state = this.workOrder.gdState.value;
          if (state == 4 || state == 5) {
            //待到达，待送达
            this.models = {
              disjunctor: true,
              title: "提示",
              content: "您想接单的药品，工单正在执行中。请确认是否继续接单签到？",
              icon: "warn",
              operate: {
                ok: "确定",
                cancel: "取消",
              },
            };
          } else {
            // 待接单 待抢单
            this.commonBuild();
          }
        }
      },
      // 建单并签到
      buildOrderAndSign() {
        if (!this.gotoFlag) {
          return;
        }
        this.gotoFlag = false;
        uni.showModal({
          title: "提示",
          content: "请确认是否建单并签到？",
          success: (result) => {
            if (result.confirm) {
              console.log("用户点击确定");
              let postData = {
                packId: this.drugsBagDto.packid,
              };
              uni.showLoading({
                title: "加载中",
                mask: true,
              });
              post("/workerOrder/drugBagReceiving", postData).then((ress) => {
                uni.hideLoading();
                if (ress.status == 200) {
                  let info = {
                    targetDept: ress.data.target.dept, //所属科室
                    packid: ress.data.packid, //药包编码
                    batchNo: ress.data.batchNo, //药包批次
                    status: ress.status,
                    msg: ress.msg,
                  };
                  uni.navigateTo({
                    url: `../scanning_result_seimin/scanning_result_seimin?info=${encodeURIComponent(
                    JSON.stringify(info)
                  )}`,
                  });
                } else {
                  uni.showToast({
                    icon: "none",
                    title: ress.msg || "接口获取数据失败！",
                  });
                }
              });
            } else if (result.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      },
      //获取药包信息及其关联的工单信息
      queryDrugsBagForId(drugsBagId) {
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/workerOrder/queryDrugsBagForId", {
          drugsBagId,
        }).then((res) => {
          uni.hideLoading();
          if (res.status == 200) {
            this.drugsBagDto = res.drugsBagDto || {};
            this.workOrder = res.workOrder || {};
            console.log(this.workOrder)
          } else {
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        });
      },
    },
    onShow() {
      this.gotoFlag = true;
    },
    onLoad(options) {
      this.hosId = uni.getStorageSync('userData').user.currentHospital.id;
      console.log(options);
      let {
        drugsBagId,
        drugsBagBatchNo,
      } = options;
      if (drugsBagId) {
        this.queryDrugsBagForId(drugsBagId);
      }
      // this.infoDATA = JSON.parse(options.infoDATA); //详细信息
      // console.log(this.infoDATA)
      // this.res["status"] = options.status; //状态码
      // this.res["msg"] = options.msg; //返回的信息
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less" scoped>
  .pharmacyDetails {
    background-color: rgb(249, 250, 251);
    padding-top: 10%;

    .pharmacyDetails_title {
      font-size: 46rpx;
      font-weight: 550;
      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;
          max-height: 424rpx;
          padding: 0 16rpx;
          text-align: left;
          position: relative;

          .text_big {
            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;
            }
          }

          #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 {
      width: 710rpx;
      height: 68rpx;
      line-height: 68rpx;
      position: fixed;
      left: 20rpx;
      bottom: 160rpx;
      border-radius: 8rpx;
      background: linear-gradient(to right, #72c172, #3bb197);
      font-size: 36rpx;
      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>
