<template>
  <view class="pharmacy">
    <view class="pharmacy_header">
      <view class="page_tab_bar pharmacy_changeDept active">
        <view class="tab_dept">
          <text @click="changeDept">{{ currenDept.dept}}</text>
          <button type="default" size="mini" class="changeDept" @click="showHandViewDrugsBag">输入发药单号</button>
        </view>
      </view>
      <view class="scanning_btn" hover-class="seimin-btn-hover" @click="drugsScanning()">
        <text class="newicon newicon-saoma"></text>
      </view>
      <view class="scanFont">扫一扫</view>
    </view>
    <view class="pharmacy-heng">
      <view class="pharmacy-heng-tab" :class="{ 'cube-tab_active': selectedLabelSlots === item.id }"
        v-for="(item, i) in tabs" :key="i" @click="clickHandler(item.id)">
        <view class="pharmacy-heng-label">{{ item.label }}</view>
      </view>
    </view>
    <view>
      <scroll-view scroll-y :refresher-enabled="scroll_refresher_enabled" :refresher-triggered="triggered"
        @refresherrefresh="refresherrefresh" @refresherrestore="refresherrestore" @scroll="scroll"
        @scrolltolower="scrolltolower" :scroll-top="scroll_top" @refresherabort="refresherabort">
        <view class="page_scrollIt">
          <view class="page_item_wrap" v-for="row in list" :key="row.id" @click="getDetails(row)">
            <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">请领单：{{ row.packid }}</text>
                  </view>
                  <view class="page_item_top_R">
                    <text class="L_iocn">{{
                      row.drugsState && row.drugsState.name
                    }}</text>
                  </view>
                </view>
              </view>
              <view class="page_item_cont">
                <!-- <view class="page_item_cont_T">
                  <view class="page_item_cont_title">
                    <text>批次号</text>
                    <text class="text_big">{{ row.batchNo }}</text>
                  </view>
                </view> -->
                <view class="page_item_cont_B">
                  <view class="page_item_cont_title">
                    <text>记账时间</text>
                    <text class="text_big">
                      <text>{{ row.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>{{ row.target && row.target.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 v-if="list.length == 0" class="zwsj">
            <image class="zwsj_img" mode="widthFix" src="../../static/img/zanwushuju.png"></image>
            <view class="zwsj_txt">暂无数据</view>
          </view>
        </view>
      </scroll-view>
    </view>
    <!-- 输入发药单号弹窗 -->
    <handViewDrugsBag v-if="drugModels.disjunctor" :title="drugModels.title" :disjunctor="drugModels.disjunctor"
      @ok="drugOk" @cancel="drugCancel">
    </handViewDrugsBag>
  </view>
</template>
<script>
  import {
    get,
    post,
    SM,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        // 输入发药单号弹窗model
        drugModels: {
          disjunctor: false,
        },
        SMFlag:true,
        triggered: false, //下拉刷新状态
        freshing: false, //上拉加载开关
        scroll_top: 0, //距离顶部的距离
        scroll_refresher_enabled: true, //是否开启自定义下拉刷新
        selectedLabelSlots: 4,
        list: [],
        idx: 0,
        totalNum: -1,
        tabs: [{
            id: 4,
            label: "待配药",
          },
          {
            id: 5,
            label: "配药中",
          },
          {
            id: 6,
            label: "核对中",
          },
        ],
        hosId: uni.getStorageSync("userData").user.currentHospital.id, //当前院区
        loginUser: uni.getStorageSync("userData"),
        currenDept: {}, //当前登录人所属科室
      };
    },
    methods: {
      // 输入发药单号-确认
      drugOk(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.drugModels.disjunctor = false;
        this.getDetails(data);
      },
      // 输入发药单号-取消
      drugCancel() {
        this.drugModels.disjunctor = false;
      },
      // 输入发药单号弹窗
      showHandViewDrugsBag() {
        console.log('点击')
        this.drugModels = {
          title: '填写发药单号',
          disjunctor: true,
        }
      },
      // tab切换
      clickHandler(id) {
        this.selectedLabelSlots = id;
        this.idx = 0;
        this.totalNum = -1;
        this.scroll_top = Math.random();
        //4为微信待配药、5为微信配药中、6为微信核对中
        this.getPharmacyListData(id, 0);
      },
      // 请求列表数据
      getPharmacyListData(type, idx) {
        if (this.list.length == this.totalNum) {
          uni.showToast({
            icon: "none",
            title: "没有更多数据了！",
          });
          this.freshing = true;
          return;
        }
        let data = {
          idx,
          sum: 20,
          drugsBag: {
            searchType: type,
            launch: {
              id: this.currenDept.id
            }
          },
        };
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/api/fetchDataList/drugsBag", data).then((res) => {
          uni.hideLoading();
          if (res.status == 200) {
            this.triggered = false;
            this.freshing = true;
            this.totalNum = res.totalNum;
            if (idx === 0) {
              this.list = res.list;
            } else {
              this.list.push(...res.list);
            }
          } else {
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        });
      },
      // 扫一扫
      drugsScanning() {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((ress) => {
          uni.navigateTo({
            url: "../pharmacyDetails/pharmacyDetails?qrcode=" + ress,
          });
        }).catch(err => {
          this.SMFlag = true;
        });
      },
      //刷新
      refresherrefresh() {
        if (this.triggered) {
          return;
        }
        this.triggered = true;
        this.idx = 0;
        this.totalNum = -1;
        this.getPharmacyListData(this.selectedLabelSlots, 0);
      },
      // 滚动
      scroll(e) {
        if (e.detail.scrollTop < 20) {
          this.scroll_refresher_enabled = true;
        } else {
          this.scroll_refresher_enabled = false;
        }
      },
      // 下拉刷新复位
      refresherrestore() {
        this.triggered = false;
        console.log("下拉刷新复位");
      },
      //下拉刷新中止
      refresherabort() {
        this.triggered = false;
        console.log("下拉刷新中止");
      },
      //分页,上拉加载
      scrolltolower() {
        if (this.freshing) {
          this.freshing = false;
          this.getPharmacyListData(this.selectedLabelSlots, ++this.idx);
        }
      },
      // 详情页面
      getDetails(drugBug) {
        uni.navigateTo({
          url: `../pharmacyDetails/pharmacyDetails?id=${drugBug.id}&packid=${drugBug.packid}`,
        });
      },
      // 阻止浏览器滑动
      stop(e) {
        e.preventDefault();
      },
      // 切换科室
      changeDept() {
        uni.navigateTo({
          url: `../search/search?type=pharmacy`,
        });
      },
      // 更新用户所在科室
      updateUser(dept) {
        let dataObj = {
          user: {
            dept: {
              id: dept.id,
            },
            id: this.loginUser.user.id,
          },
        };
        post("/data/updData/user", dataObj).then((res) => {
          if (res.status == 200) {
            this.getCurrentUserNow();
          }
        })
      },
      // 重新获取用户信息
      getCurrentUserNow() {
        get("/user/data/getCurrentUser").then((res) => {
          if (res.status == 200) {
            // this.loginUser.dept = this.currenDept;
            this.loginUser.user = res.data;
            uni.setStorageSync('userData', this.loginUser);
          }
        })
      }
    },
    onLoad(options) {
      //#ifdef H5
      console.log(options.login, "login");
      if (options.login == 1) {
        // uni.setStorageSync('type', 'pharmacy')
        uni.redirectTo({
          url: "../homePage/homePage?type=pharmacy",
        });
      }
      //#endif
      if (options.id && options.dept) {
        let {
          id,
          dept
        } = options;
        this.currenDept.id = id;
        this.currenDept.dept = dept;
        this.updateUser(this.currenDept)
      } else {
        this.currenDept = uni.getStorageSync("userData").user.dept;
      }
      // 请求列表
      this.getPharmacyListData(this.selectedLabelSlots, 0);
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
    onShow() {
      this.SMFlag = true;
      // #ifdef H5
      document.body.addEventListener("touchmove", this.stop, {
        passive: false
      });
      // #endif
    },
    onUnload() {
      // #ifdef H5
      document.body.removeEventListener("touchmove", this.stop, {
        passive: false,
      });
      // #endif
    },
  };
</script>
<style lang="less">
  // 覆盖cube
  .cube-tab-bar {
    display: block !important;
  }

  .pharmacy /deep/ .cube-tab-bar-slider {
    display: none !important;
  }

  .pharmacy {
    background-color: rgb(249, 250, 251);
		position: fixed;
		width: 100%;

    // padding-top: 36rpx;
    .pharmacy_header {
      position: relative;

      .pharmacy_changeDept {
        padding: 16rpx 0;

        &.page_tab_bar {
          flex: 1;
          font-size: 36rpx;
          background: #fff;
          display: flex;
          justify-content: center;
          align-items: center;
          position: relative;

          &:after {
            content: "";
            position: absolute;
            left: 0;
            bottom: 0;
            height: 2rpx;
            width: 100%;
            background-color: transparent;
          }

          .tab_dept {
            position: relative;

            .changeDept {
              white-space: nowrap;
              margin: 0;
              position: absolute;
              right: 0;
              top: 50%;
              transform: translate(105%, -50%);
              padding: 0 0.5em;
              line-height: 2;
            }
          }

          &.active {
            color: #49b856;

            &:after {
              background-color: #49b856;
            }
          }
        }
      }
    }

    .scanning_btn {
      margin: 16rpx auto;
      width: 138rpx;
      height: 138rpx;
      background: #72c172;
      border-radius: 50%;
      /*偏移*/
      line-height: 200rpx;
      color: #fff;
      font-size: 36rpx;
      display: flex;
      justify-content: center;
      align-items: center;

      text {
        font-size: 48rpx;
      }
    }

    .zwsj {
      // margin-top: 180rpx;
      padding-top: 10%;
      display: flex;
      flex-direction: column;
      align-items: center;

      .zwsj_txt {
        font-size: 36rpx;
        font-weight: 700;
        margin-top: 20rpx;
      }

      .zwsj_img {
        width: 582rpx;
      }
    }

    .page_item_wrap {
      position: relative;
      margin-bottom: 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;
              align-items: center;
            }
          }

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

        #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;
      }
    }

    .pharmacy-heng {
      height: 90rpx;
      display: flex;
      background-color: #fff;
      margin-bottom: 32rpx;

      .pharmacy-heng-tab {
        flex: 1 !important;
        display: flex;
        align-items: center;

        .pharmacy-heng-label {
          flex: 1;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 36rpx;
          width: 204rpx;
          height: 60rpx;
          border-radius: 30rpx;
          background-color: #fff;
          color: rgb(102, 102, 102);
        }

        &.cube-tab_active {
          .pharmacy-heng-label {
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 36rpx;
            width: 204rpx;
            height: 60rpx;
            border-radius: 30rpx;
            background-color: rgb(229, 233, 237);
            color: rgb(51, 51, 51);
          }
        }
      }
    }

    // 滚动区域
    .page_scrollIt {
      height: calc(100vh - 380rpx);
    }

    // “扫一扫”文字
    .scanFont {
      font-size: 36rpx;
      font-weight: 700;
      margin-top: 16rpx;
      margin-bottom: 16rpx;
      text-align: center;
    }
  }
</style>
