<template>
  <view class="transferWorkOrder">
    <mSearch class="search" :mode="2" button="inside" placeholder="请搜索人员名称、工号" @input="changeInp" @search="doSearch()" v-model="keyword" searchButtonType="icon" searchButtonName="newicon-saoma"></mSearch>
    <scroll-view scroll-y class="list">
      <view class="item" v-for="item in list" :key="item.id" @click="selectItem(item)">{{item.name}}（{{item.account}}）</view>
    </scroll-view >
      <view class="footer">
        <view class="btn" hover-class="seimin-btn-hover" @click="goBack()">返回</view>
      </view>
  </view>
</template>

<script>
  import { get, post, SM, webHandle } from "../../http/http.js";
  import debounce from 'lodash-es/debounce'
  import mSearch from "@/components/mehaotian-search-revision/mehaotian-search-revision.vue";
  export default {
    onLoad(options) {
      console.log('options', options);
      this.options = options || {};
      this.getList();
    },
    onShow() {
      this.SMFlag = true;
    },
    components: {
      //引用mSearch组件，如不需要删除即可
      mSearch,
    },
    data() {
      return {
        options: {},
        keyword: '',
        hosId: uni.getStorageSync('userData').user.currentHospital.id,
        list: [],
        SMFlag: true,
      };
    },
    methods:{
      goBack() {
        uni.navigateBack();
      },
      //防抖搜索
      changeInp(event) {
        this.inputChange(event);
      },
      //监听输入
      inputChange(event) {
        let keyword = event.detail ? event.detail.value : event;
        this.handleSearch(keyword);
      },
      doSearch(){
        // 扫码
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((content) => {
          this.scanQrcode(content);
        }).catch(err => {
          this.SMFlag = true;
        });
      },
      //扫码
      scanQrcode(content){
        let userHandoverCode = '';
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        //检验二维码的有效性(扫码前必须验证)
        post("/dept/scanning", {
            content,
          })
          .then((result) => {
            try{
              userHandoverCode = JSON.parse(result.code);
            }catch(e){
              userHandoverCode = '';
            }
        
            this.SMFlag = true;
            uni.hideLoading();
            // 200检测通过，201没有有效期也通过。
            if (result.state == 200 || result.state == 201) {
              if(Object.prototype.toString.call(userHandoverCode) === '[object Object]' && userHandoverCode.name){
                uni.redirectTo({
                  url: `/pages/transferWorkOrder/transferWorkOrderPatient?userId=${userHandoverCode.id}&userName=${userHandoverCode.name}&userAccount=${userHandoverCode.account}&selectPatientId=${this.options.selectPatientId}`,
                });
              }else{
                uni.showToast({
                  icon: "none",
                  title: "请扫描正确的二维码！",
                });
              }
            } else {
              uni.hideLoading();
              uni.showToast({
                icon: "none",
                title: result.info || "接口获取数据失败！",
              });
            }
          })
      },
      selectItem(user){
        // let userList = uni.getStorageSync('transferWorkOrderUserList') || [];
        // // 移除
        // userList = userList.filter(v => v.id != user.id);
        // // 添加
        // userList = [user, ...userList];
        // userList = userList.map(v => ({...v, timeStamp: +new Date()}))
        // uni.setStorageSync('transferWorkOrderUserList', userList);
        
        uni.redirectTo({
          url: `/pages/transferWorkOrder/transferWorkOrderPatient?userId=${user.id}&userName=${user.name}&userAccount=${user.account}&selectPatientId=${this.options.selectPatientId}`,
        });
      },
      handleSearch: debounce(function(keyword) {
        this.getList(keyword);
      }, 500),
      getList(keyword){
        // if(!keyword){
        //   let userList = uni.getStorageSync('transferWorkOrderUserList') || [];
        //   userList = userList.filter(v => +new Date() - v.timeStamp < 4 * 60 * 60 * 1000);//4小时内选择过的用户
        //   uni.setStorageSync('transferWorkOrderUserList', userList);
        //   this.list = userList;
        //   return;
        // }
        uni.showLoading({
          title: "加载中",
        });
        post("/simple/data/fetchDataList/user", {
          idx: 0,
          sum: 25,
          user: {
            hosId: this.hosId,
            name: keyword || undefined,
            online: true,
          }
        }).then((result) => {
          uni.hideLoading();
          if (result.status == 200) {
            this.list = result.list || [];
          } else {
            uni.showToast({
              icon: "none",
              title: result.msg || "接口获取数据失败！",
            });
          }
        });
      },
    }
  }
</script>

<style lang="less" scoped>
.transferWorkOrder{
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  .search{
    padding: 10rpx 24rpx;
  }
  .list{
    flex: 1;
    min-height: 0;
    background-color: #fff;
    .item{
      height: 100rpx;
      line-height: 100rpx;
      text-align: center;
      font-size: 26rpx;
      border-bottom: 1rpx solid #D5D5D5;
    }
  }
  .footer{
    margin-top: 24rpx;
    margin-bottom: 24rpx;
    line-height: 80rpx;
    height: 80rpx;
    background: #fff;
    display: flex;
    align-items: center;
    .btn {
      height: 80rpx;
      flex: 1;
      margin: 0 24rpx;
      background-color: #49B856;
      color: #fff;
      border-radius: 8rpx;
      font-size: 34rpx;
      text-align: center;
    }
  }
}

</style>
