<template>
  <view class="patientInformationList">
    <view class="page_tab">
      <view class="page_tab_bar active">
        <view class="tab_dept">{{ currenDept.dept
          }}<button type="default" size="mini" class="changeDept" @click="changeDept">
            切换科室
          </button></view>
      </view>
    </view>
    <view v-if="zxzData.length == 0" class="zwsj">
      <image class="zwsj-img" mode="widthFix" src="../../static/img/zanwushuju.png"></image>
      <view class="zwsj-txt">暂无数据</view>
    </view>
    <view v-if="zxzData.length" class="page_items">
      <scroll-view class="page_items_scroll" scroll-y :refresher-enabled="scroll_refresher_enabled"
        :refresher-triggered="triggered" @refresherrefresh="refresherrefresh" @refresherrestore="refresherrestore"
        @scrolltolower="scrolltolower" :scroll-top="scroll_top" @refresherabort="refresherabort" @scroll="scroll"
        refresher-background="transport">
        <view class="page_item_wrap" v-for="(item, index) of zxzData" :key="index">
          <view class="page_item">
            <view class="L"></view>
            <view class="R"></view>
            <view class="page_item_top">
              <view class="page_item_top_L">
                <template v-if="item.illnessState">
                  <text v-if="item.illnessState.value === '2'"
                    class="colorRed icon_transport transport-tubiao_wei"></text>
                  <text v-if="item.illnessState.value === '3'" class="colorRed icon_transport transport-zhong"></text>
                </template>
                <template v-if="item.careLevel">
                  <text v-if="item.careLevel.value === '0'" class="icon_transport transport-te1"></text>
                  <text v-if="item.careLevel.value === '1'" class="icon_transport transport-1_round_solid"></text>
                  <text v-if="item.careLevel.value === '2'" class="icon_transport transport-2_round_solid"></text>
                  <text v-if="item.careLevel.value === '3'" class="icon_transport transport-3_round_solid"></text>
                </template>
                <!-- <view class="page_item_top_R" v-show="wechatFocusSwitch == 1 && item.focusPatient == 1">
                  需要帮助
                </view>
                <view class="page_item_top_R" style="background-color: #49b856"
                  v-show="wechatFocusSwitch == 1 && !item.focusPatient">
                  无需帮助
                </view> -->
              </view>
              <view class="send_wrap">
                <view class="sendBack">
                  <button @click="scanDept(item)">送回病房</button>
                </view>
              </view>
            </view>
            <view class="page_item_cont">
              <view class="page_item_conts">
                <view>{{ item.patientName }}({{ item.residenceNo }})</view>
                <view> 床号：{{ item.bedNum || "-" }} </view>
                <view> 待检：{{ item.watingCount }} </view>
              </view>
            </view>
            <view class="page_item_btn" hover-class="seimin-btn-hover" @click="itemInfo(item.patientCode)">患者详情</view>
          </view>
          <view class="L-l"></view>
          <view class="R-l"></view>
        </view>
      </scroll-view>
    </view>
    <!-- 底部扫一扫 -->
    <bigScreen></bigScreen>
    <!-- 送回病房弹窗 -->
    <showModel :title="models1.title" :icon="models1.icon" :disjunctor="models1.disjunctor" :content="models1.content"
      :operate="models1.operate" @ok="ok1" @cancel="cancel1"></showModel>
    <!-- 送回病房建单弹窗 -->
    <showModel :title="models2.title" :icon="models2.icon" :disjunctor="models2.disjunctor" :content="models2.content"
      :operate="models2.operate" @ok="ok2" @cancel="cancel2"></showModel>
    <!-- 送回病房建单弹窗-扫描科室 -->
    <showModel :title="models3.title" :icon="models3.icon" :disjunctor="models3.disjunctor" :content="models3.content"
      :operate="models3.operate" @ok="ok3" @cancel="cancel3"></showModel>
  </view>
</template>
<script>
  import bigScreen from "../../components/bigScreen/bigScreen.vue";
  import {
    get,
    post,
    SM,
    deleteIt,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        SMFlag:true,
        hosId: uni.getStorageSync("userData").user.currentHospital.id,
        // 选中的患者项
        currentItem: {},
        // 送回病房弹窗model
        models1: {
          disjunctor: false,
        },
        // 送回病房建单弹窗model
        models2: {
          disjunctor: false,
        },
        // 送回病房建单弹窗model-扫描科室
        models3: {
          disjunctor: false,
        },
        scrollYY: 0, //滚动条滚动的距离
        options: {},
        // wechatFocusSwitch: 0,
        //当前登录人所属科室
        currenDept: {},
        // 抢单的id
        qdId: "",
        //列表数据
        zxzData: [],
        //分页页码
        idx: 0,
        // 列表项总数
        totalNum: -1,
        triggered: false, //下拉刷新状态
        freshing: false, //上拉加载开关
        scroll_top: 0, //距离顶部的距离
        scroll_refresher_enabled: true, //是否开启自定义下拉刷新
        currentStartDept: {}, //扫描的起点科室
      };
    },
    components: {
      bigScreen,
    },
    methods: {
      ok2() {
        console.log(this.options)
        this.models2.disjunctor = false;
        let currentItem = JSON.parse(this.options.currentItem);
        uni.showLoading({
          mask: true,
          title: '加载中'
        })
        let userId = uni.getStorageSync("userData").user.id;
        let postData = {
          "workOrder": {
            sourceId: 4,
            "hosId": this.hosId,
            "startDept": {
              "id": this.options.did
            },
            "createDept": this.options.did,
            "patient": {
              "patientCode": this.currentItem.patientCode
            },
            "worker": {
              "id": userId
            }
          }
        };
        console.log(postData)
        // return;
        post("/workerOrder/returnSickRoom", postData).then((res) => {
          uni.hideLoading();
          if (res.status == 200) {
            uni.showToast({
              icon: 'success',
              title: '建单成功',
            });
          } else {
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        })
      },
      cancel2() {
        this.models2.disjunctor = false;
      },
      ok3() {
        console.log(this.currentItem)
        this.models3.disjunctor = false;
        uni.showLoading({
          mask: true,
          title: '加载中'
        })
        let userId = uni.getStorageSync("userData").user.id;
        let postData = {
          "workOrder": {
            sourceId: 4,
            "hosId": this.hosId,
            "startDept": {
              "id": this.currentStartDept.id
            },
            "createDept": this.currentStartDept.id,
            "patient": {
              "patientCode": this.currentItem.patientCode
            },
            "worker": {
              "id": userId
            }
          }
        };
        console.log(postData);
        // return;
        post("/workerOrder/returnSickRoom", postData).then((res) => {
          uni.hideLoading();
          if (res.status == 200) {
            uni.showToast({
              icon: 'success',
              title: '建单成功',
            });
          } else {
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        })
      },
      cancel3() {
        this.models3.disjunctor = false;
      },
      // 送回病房-建单
      sendBuild() {
        let currentItem = JSON.parse(this.options.currentItem);
        this.models2 = {
          disjunctor: true,
          title: "提示",
          content: `您选择的起点科室为【${this.options.ddept}】，您将从【${this.options.ddept}】把【${currentItem.patientName}】送回【${this.options.dept}】，如您确认请点击“确认建单”。`,
          icon: "warn",
          operate: {
            ok: "确认建单",
            cancel: "取消",
          },
        };
      },
      // 送回病房-扫描科室
      scanDept(item) {
        console.log(item)
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        this.currentItem = item;
        let code = "";
        SM().then((ress1) => {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          //检验二维码的有效性
          post("/dept/scanning", {
            content: ress1,
          }).then((result) => {
            this.SMFlag = true;
            if (result.state == 200 || result.state == 201) {
              let ress = result.code;
              if (ress) {
                code = ress;
                // 科室扫描
                let postData = {
                  "idx": 0,
                  "sum": 1,
                  "department": {
                    "hospital": {
                      "id": this.hosId
                    },
                    "qrcode": code
                  }
                };
                post("/data/fetchDataList/department", postData).then((res) => {
                  uni.hideLoading();
                  if (res.status == 200) {
                    if (res.totalNum > 0) {
                      this.currentStartDept = res.list[0];
                      this.models3 = {
                        disjunctor: true,
                        title: "提示",
                        content: `您选择的起点科室为【${this.currentStartDept.dept}】，您将从【${this.currentStartDept.dept}】把【${this.currentItem.patientName}】送回【${this.currenDept.dept}】，如您确认请点击“确认建单”。`,
                        icon: "warn",
                        operate: {
                          ok: "确认建单",
                          cancel: "取消",
                        },
                      };
                    } else {
                      uni.hideLoading();
                      uni.showToast({
                        icon: "none",
                        title: "请扫描正确的科室码！",
                      });
                    }
                  } else {
                    uni.hideLoading();
                    uni.showToast({
                      icon: "none",
                      title: "请求失败！",
                    });
                  }
                });
              }
            } else {
              uni.hideLoading();
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
        }).catch(err=>{
          this.SMFlag = true;
        });
      },
      // 送回病房-选择起点科室
      sendBack(item) {
        console.log(item)
        this.currentItem = item;
        this.models1 = {
          disjunctor: true,
          title: "提示",
          content: `您好，您是要将【${item.patientName}】送回病房吗？如确认请您点击“选择起点科室”建立工单。`,
          icon: "warn",
          operate: {
            ok: "选择起点科室",
            cancel: "取消",
          },
        };
      },
      ok1() {
        this.models1.disjunctor = false;
        uni.navigateTo({
          url: `../search/search?type=sendBackPatientList&currentItem=${JSON.stringify(this.currentItem)}&cid=${this.currenDept.id}&cdept=${this.currenDept.dept}&scrollYY=${this.scrollYY}`,
        });
      },
      cancel1() {
        this.models1.disjunctor = false;
      },
      // 切换科室
      changeDept() {
        uni.navigateTo({
          url: `../search/search?type=patientInformationList`,
        });
      },
      // 进入详情页
      itemInfo(patientCode) {
        // #ifdef H5
        document.body.removeEventListener("touchmove", this.stop, {
          passive: false,
        });
        // #endif
        uni.navigateTo({
          url: `../patientInformationInfo/patientInformationInfo?patientCode=${patientCode}`,
        });
      },
      //患者列表数据获取
      waitingOrders(idx) {
        if (this.zxzData.length == this.totalNum) {
          uni.showToast({
            icon: "none",
            title: "没有更多数据了！",
          });
          this.freshing = true;
          return;
        }
        let data = {
          idx,
          sum: 99999,
          patient: {
            department: {
              id: this.currenDept.id,
            },
          },
        };
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        // 请求列表数据
        post("/nurse/fetchDataList/patient", data).then((res) => {
          uni.hideLoading();
          if (res.status == 200) {
            this.triggered = false;
            this.freshing = true;
            this.totalNum = res.totalNum;
            if (idx === 0) {
              this.zxzData = res.list;
            } else {
              this.zxzData.push(...res.list);
            }
          } else {
            uni.showToast({
              icon: "none",
              title: "请求失败！",
            });
          }
        });
      },
      //刷新
      refresherrefresh() {
        if (this.triggered) {
          return;
        }
        console.log("下拉刷新");
        this.triggered = true;
        this.idx = 0;
        this.totalNum = -1;
        this.waitingOrders(0);
      },
      // 下拉刷新复位
      refresherrestore() {
        this.triggered = false;
        console.log("下拉刷新复位");
      },
      //下拉刷新中止
      refresherabort() {
        this.triggered = false;
        console.log("下拉刷新中止");
      },
      //分页,上拉加载
      scrolltolower() {
        if (this.freshing) {
          console.log("上拉加载");
          this.freshing = false;
          this.waitingOrders(++this.idx);
        }
      },
      // 滚动
      scroll(e) {
        this.scrollYY = e.detail.scrollTop;
        if (e.detail.scrollTop < 20) {
          this.scroll_refresher_enabled = true;
        } else {
          this.scroll_refresher_enabled = false;
        }
      },
      // 阻止浏览器滑动
      stop(e) {
        e.preventDefault();
      },
      // 获取需要帮助权限
      // getWechatFocusSwitch() {
      //   let postData = {
      //     idx: 0,
      //     sum: 1,
      //     taskType: {
      //       hosIds: this.hosId,
      //       associationType: {
      //         id: 260,
      //       },
      //     },
      //   };
      //   post("/configuration/fetchDataList/taskType", postData).then((res) => {
      //     if (res.status == 200) {
      //       this.wechatFocusSwitch = res.list[0].wechatFocusSwitch;
      //     } else {
      //       uni.showToast({
      //         icon: "none",
      //         title: "请求失败！",
      //       });
      //     }
      //   });
      // },
    },
    onLoad(options) {
      console.log(options);
      this.options = options;
      //滚动条位置设置
      if (options.scrollYY !== undefined) {
        this.scroll_top = options.scrollYY;
      }
      if (options.did && options.ddept && options.currentItem) {
        this.sendBuild();
      }
      let patientCurrentDept = uni.getStorageSync("patientCurrentDept");
      if (options.id && options.dept) {
        let {
          id,
          dept
        } = options;
        this.currenDept.id = id;
        this.currenDept.dept = dept;
      } else if (patientCurrentDept) {
        this.currenDept.id = patientCurrentDept.id;
        this.currenDept.dept = patientCurrentDept.dept;
      } else {
        this.currenDept = uni.getStorageSync("userData").user.dept;
      }
      // this.getWechatFocusSwitch();
      this.waitingOrders(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
    },
    onHide() {
      // #ifdef H5
      document.body.removeEventListener("touchmove", this.stop, {
        passive: false,
      });
      // #endif
    },
  };
</script>
<style lang="less">
  .patientInformationList {
    width: 100%;
    height: 100%;
    position: relative;

    .icon_transport {
      color: #49b856;
      font-size: 50rpx;

      &.colorRed {
        color: red;
        font-size: 40rpx;
      }
    }

    .page_tab {
      width: 100%;
      height: 96rpx;
      display: flex;
      position: fixed;
      left: 0;
      top: 0;
      z-index: 999;

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

    .zwsj {
      position: absolute;
      left: 50%;
      top: 180rpx;
      transform: translateX(-50%);

      .zwsj-img {
        width: 560rpx;
      }

      .zwsj-txt {
        font-size: 36rpx;
        font-weight: 700;
        margin-top: 20rpx;
        text-align: center;
      }
    }

    .page_items {
      height: calc(100vh - 184rpx);
      padding: 0 20rpx;
      padding-top: 96rpx;

      .page_items_scroll {
        height: 100%;

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

          .page_item {
            margin-bottom: 16rpx;
            height: 276rpx;
            background: #fff;
            border-radius: 8rpx;
            overflow: hidden;
            border: 2rpx solid #e5e9ed;
            display: flex;
            flex-direction: column;
            justify-content: space-between;

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

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

            .page_item_top {
              height: 86rpx;
              border-bottom: 2rpx dashed #e5e9ed;
              padding: 0 16rpx;
              display: flex;
              align-items: center;
              justify-content: space-between;

              .send_wrap {
                display: flex;
                justify-content: space-between;
                align-items: center;
              }

              .sendBack {
                button {
                  font-size: 28rpx;
                  height: 52rpx;
                  line-height: 52rpx;
                  margin: 0;
                  color: rgb(7, 134, 60);
                }
              }

              .page_item_top_L {
                height: 100%;
                float: left;
                display: flex;
                align-items: center;
                line-height: 88rpx;

                .L_time {
                  color: #6cc076;
                  font-size: 32rpx;
                }
              }

              .page_item_top_R {
                height: 40rpx;
                float: right;
                line-height: 40rpx;
                font-size: 24rpx;
                padding: 0 8rpx;
                background-color: red;
                color: #fff;
              }
            }

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

              .page_item_conts {
                color: rgb(102, 102, 102);
                font-size: 28rpx;
                display: flex;
                align-items: center;
                min-height: 100rpx;
                max-height: 344rpx;

                view {
                  margin-bottom: 10rpx;
                  margin-left: 40rpx;

                  &:first-of-type {
                    margin-left: 0;
                  }
                }

                text {
                  color: #49b856;
                }

                .num {
                  float: right;
                }
              }
            }

            .page_item_btn {
              height: 88rpx;
              background-image: linear-gradient(to right, #72c172, #3bb197);
              border-radius: 8rpx;
              line-height: 88rpx;
              color: #fff;
              font-size: 36rpx;
              font-weight: 700;
              text-align: center;
            }
          }

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

          .R-l {
            width: 2rpx;
            height: 40rpx;
            background: #f9fafb;
            position: absolute;
            right: 20rpx;
            top: 72rpx;
            display: none;
          }
        }
      }
    }
  }
</style>
