<template>
  <view class="showModel" v-show="disjunctor">
    <view class="showModel__wrap">
      <view class="showModel__header">
        科室二维码 <text v-if="userData.user.dutyDeptDTO">({{ refreshQRCodeTime }}s)</text>
      </view>
      <view class="showModel__article">
        <view class="showModel__content" v-if="userData.user.dutyDeptDTO">
          <image :src="qrCode" mode="widthFix" style="width: 100%;"></image>
          <view class="page_item_btn" @click="bindDept()"> 切换科室 </view>
          <view class="page_item_btn mt8" @click="toStationaryPatient()"> 驻点患者 </view>
        </view>
        <view class="showModel__content" v-else>
          <view class="page_item_btn" @click="bindDept()"> 绑定科室 </view>
        </view>
      </view>
      <view class="showModel__footer">
        <view class="showModel__know" @click="know" hover-class="seimin-btn-hover">关闭</view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    get,
    post,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        timer: null,
        refreshQRCodeTime: 0, //刷新时间间隔
        qrCode: '',//二维码
        userData: uni.getStorageSync('userData'),//当前用户
      };
    },
    props: {
      // 显示隐藏
      disjunctor: {
        type: Boolean,
        default: false,
      },
    },
    methods: {
      // 关闭
      know() {
        this.$emit("know");
        clearInterval(this.timer);
        this.timer = null;
      },
      // 绑定科室
      bindDept(){
        this.know();
        uni.navigateTo({
          url: `../search/search?type=showDepartmentQrcode`,
        });
      },
      // 驻点患者
      toStationaryPatient(){
        this.know();
        uni.navigateTo({
          url: `/pages/stationaryPatient/stationaryPatient?dutyDeptId=${this.userData.user.dutyDeptDTO.id}`,
        });
      },
      // 获取我的二维码
      getQrCode(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/dept/deptCodes/0", [this.userData.user.dutyDeptDTO.id])
          .then((result) => {
            uni.hideLoading();
            if (result.status == 200) {
              this.qrCode = result.data[0].base64;
              this.refreshQRCodeTime = result.data[0].refreshQRCodeTime;
              
              clearInterval(this.timer);
              this.timer = setInterval(() => {
                this.refreshQRCodeTime = Math.max(--this.refreshQRCodeTime, 0);
                if (this.refreshQRCodeTime === 0) {
                  clearInterval(this.timer);
                  this.getQrCode();
                }
              }, 1000);
            } else {
              uni.showToast({
                icon: "none",
                title: result.msg || "接口获取数据失败！",
              });
            }
          })
      },
    },
    mounted() {
      this.userData = uni.getStorageSync('userData');
      console.log(this.userData.user, 'userData');
      // 有责任科室则查询动态二维码
      this.userData.user.dutyDeptDTO && this.getQrCode();
    },
  };
</script>

<style lang="less" scoped>
  .showModel {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.2);
    z-index: 999999;
    
    .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;
    }

    .showModel__wrap {
      width: 560rpx;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      border-radius: 12rpx;

      .showModel__header {
        font-size: 36rpx;
        color: #000;
        height: 84rpx;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .showModel__article {
        color: #000;
        margin: 0 auto 25rpx;
        width: 488rpx;
        background-color: rgb(249, 250, 251);
        border: 2rpx solid rgb(229, 233, 237);
        border-radius: 12rpx;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        &.p0 {
          padding: 0;
        }

        &.p1 {
          text-align: left;
        }

        .showModel__icon {
          font-size: 138rpx;
          margin-bottom: 32rpx;

          &.showModel__icon--success {
            color: rgb(52, 179, 73);
          }

          &.showModel__icon--warn {
            color: rgb(245, 165, 35);
          }

          &.showModel__icon--error {
            color: rgb(255, 58, 82);
          }
        }

        .showModel__content {
          font-size: 36rpx;
          word-break: break-all;
          width: 100%;
        }

        .showModel__info {
          font-size: 32rpx;
          color: rgb(102, 102, 102);
        }

        .specialCloseFlag {
          width: 90%;
          height: 100%;
          padding: 16rpx;
        }

        .radio-wrap {
          .radio-item {
            margin-top: 16rpx;

            /deep/ .uni-radio-input-checked {
              background-color: #49b856 !important;
              border-color: #49b856 !important;
            }
          }
        }
      }

      .showModel__footer {
        box-sizing: border-box;
        height: 100rpx;
        border-top: 2rpx solid rgb(229, 233, 237);
        display: flex;
        align-items: center;

        view {
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 36rpx;
          color: rgb(102, 102, 102);
          position: relative;

          &:nth-of-type(2)::before {
            content: "";
            position: absolute;
            left: 0;
            bottom: 0;
            width: 2rpx;
            height: 87rpx;
            background-color: rgb(229, 233, 237);
          }
        }

        .showModel__ok {
          flex: 1;
          color: rgb(73, 184, 86);
        }

        .showModel__cancel {
          flex: 1;
        }

        .showModel__know {
          flex: 1;
          color: rgb(73, 184, 86);
        }
      }
    }
  }
</style>
