<template>
  <view class="pharmacy">
    <view class="pharmacy_header">
      <view
        class="scanning_btn"
        hover-class="seimin-btn-hover"
        @click="drugsScanning()"
      >
        <text class="icon_transport transport-saoma"></text>
      </view>
      <view class="scanFont">扫一扫</view>
      <picker v-if="depts[index]" @change="bindPickerChange" :value="index" range-key="dept" :range="depts" class="pharmacy_changeDept">
          <view class="pharmacy_input">{{depts[index].dept}}</view>
      </picker>
    </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.id)"
          >
            <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>
  </view>
</template>
<script>
import { get, post, SM, webHandle } from "../../http/http.js";
export default {
  data() {
    return {
      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: "核对中",
        },
      ],
      depts: [],//下拉框选项
      index: 0,//下拉框索引
      hosId: uni.getStorageSync("userData").user.currentHospital.id,//当前院区
    };
  },
  methods: {
    // 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.depts[this.index].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: "请求失败！",
          });
        }
      });
    },
    // 扫一扫
    drugsScanning() {
      SM().then((ress) => {
        uni.navigateTo({
          url: "../pharmacyDetails/pharmacyDetails?qrcode=" + ress,
        });
      });
    },
    //刷新
    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(id) {
      uni.navigateTo({
        url: `../pharmacyDetails/pharmacyDetails?id=${id}`,
      });
    },
    // 阻止浏览器滑动
    stop(e) {
      e.preventDefault();
    },
    //选择药房科室
    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.target.value)
        this.index = e.target.value;
        // 待配药
        this.getPharmacyListData(4, 0);
    },
    // 获取药房类型的科室
    getDeptByPharmacy(){
      let postData = {
          "idx": 0,
          "sum": 10,
          "department": {
              "hospital": {
                  "id": this.hosId
              },
              "type": {
                  "id": "386"
              },
          }
      };
      uni.showLoading({
        title: "加载中",
        mask: true,
      });
      post("/data/fetchDataList/department", postData).then((res) => {
        uni.hideLoading();
        if (res.status == 200) {
          this.depts = res.list.map(v=>({id:v.id,dept:v.dept}));
          if(this.depts.length){
            this.getPharmacyListData(this.selectedLabelSlots, 0);
          }
        } else {
          uni.showToast({
            icon: "none",
            title: "请求失败！",
          });
        }
      });
    }
  },
  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
    // 获取药房类型的科室
    this.getDeptByPharmacy();
    // #ifdef APP-PLUS
    webHandle("no", "app");
    // #endif
    // #ifdef H5
    webHandle("no", "wx");
    // #endif
  },
  onShow() {
    // #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);
  padding-top: 36rpx;
  .pharmacy_header{
    position: relative;
    .pharmacy_changeDept{
      position: absolute;
      top: 50%;
      right: 52rpx;
      transform: translateY(-50%);
      .pharmacy_input{
        background-image: linear-gradient(to right, #72c172, #3bb197);
        border-radius: 8rpx;
        color: #fff;
        font-size: 36rpx;
        font-weight: 700;
        padding: 8rpx 16rpx;
      }
    }
  }

  .scanning_btn {
    margin: 0 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: 32rpx;
    text-align: center;
  }
}
</style>
