<template>
  <view class="incidentList">
    <view class="head">
      <mSearch class="search" :mode="2" button="inside" placeholder="请输入关键字" @input="changeInp" @search="doSearch()" v-model="keyword" searchButtonType="icon" searchButtonName="newicon-saoma"></mSearch>
    </view>
    <view class="body" v-if="dataInfo.list.length">
      <view class="body_item" v-for="data in dataInfo.list" :key="data.id" @click="toDetail(data)">
        <view class="body_item_head">
          <text>
            <text class="sign signRed">{{ data.careLevel ? data.careLevel.name : '' }}<template v-if="data.careLevel && data.illnessState">、</template>{{ data.illnessState ? data.illnessState.name : '' }}</text> {{ data.patientName }}<text v-if="data.bedNum">({{ data.bedNum }})</text>
          </text>
          <text>{{ data.state ? data.state.name : '' }}</text>
        </view>

        <view class="body_item_content">
          <view class="body_item_content_p">
            <text class="name ellipsis">住院号：{{data.patientCode}}</text>
            <text>{{(data.patientDTO && data.patientDTO.gender) ? data.patientDTO.gender.name : ''}}<text v-if="data.age !== undefined" class="ml24">{{data.age}}<text v-if="data.age !== undefined">岁</text></text></text>
          </view>
          <view class="body_item_content_p">
            <text class="name ellipsis">患者科室：{{ data.deptDTO ? data.deptDTO.dept : '' }}</text>
            <text>{{data.tripTypeDTO ? data.tripTypeDTO.inspectMode : ''}}</text>
          </view>
          <view class="body_item_content_p">
            <text class="name ellipsis">签到信息：{{data.signDeptDTO ? data.signDeptDTO.dept : ''}}</text>
            <text>{{data.signTime | formatDate('MM-dd hh:mm')}}</text>
          </view>
        </view>

        <view class="body_item_foot" v-if="stationaryTransfer || stationaryReturn">
          <view class="btns pt0">
            <button @click.stop="stationaryTransferFun(data)" type="default" class="primaryButton btn" v-if="stationaryTransfer && data.reAssign == 1">转派</button>
            <button @click.stop="stationaryReturnFun(data)" type="default" class="primaryButton btn" v-if="stationaryReturn && data.state.value != 5">发起回科</button>
          </view>
        </view>
      </view>
    </view>
    <view class="zanwu" v-else>
      <text class="newicon newicon-zanwu"></text>
    </view>
  </view>
</template>

<script>
  import { get, post, SM, deleteIt, webHandle } from "../../http/http.js";
  import debounce from 'lodash-es/debounce';
  import mSearch from "@/components/mehaotian-search-revision/mehaotian-search-revision.vue";
  export default {
    onShow() {
      this.SMFlag = true;
    },
    components: {
      //引用mSearch组件，如不需要删除即可
      mSearch,
    },
    data(){
      return {
        stationaryTransfer: false,//转派
        stationaryReturn: false,//发起回科
        options: {},
        keyword: '',
        SMFlag: true,
        loginUser: uni.getStorageSync('userData').user,
        menuList: uni.getStorageSync('userData').menu,
        hosId: uni.getStorageSync('userData').user.currentHospital.id,
        dataInfo: {
          list: [],//工单列表
          idx: 0,//页码
          hasMore: true,//是否有更多数据
        }
      }
    },
    methods: {
      handleSearch: debounce(function(keyword) {
        this.getList(0);
      }, 500),
      //防抖搜索
      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.keyword = content;
          this.getList(0);
        }).catch(err => {
          this.SMFlag = true;
        });
      },
      // 初始化
      onLoadFn(){
        this.getList(0);
      },
      // 获取列表信息
      getList(idx){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        this.dataInfo.idx = idx === undefined ? this.dataInfo.idx : idx;
        if(this.dataInfo.idx === 0){
          this.dataInfo.list = [];
        }

        let postData = {
            "idx": this.dataInfo.idx,
            "sum": 20,
            "patientInspectLog": {
              "hosId": this.hosId,
              "cascadeSignDept": +this.options.dutyDeptId,
              "patientKey": this.keyword || undefined,
              "reAssign": 1, //是否可以转派
              "state": {
                value: '3,4'
              }
            }
        }

        post("/simple/data/fetchDataList/patientInspectLog", postData).then(res => {
          uni.hideLoading();
          uni.stopPullDownRefresh();
          if(res.status == 200){
            let list = res.list || [];
            if(list.length){
              this.dataInfo.hasMore = true;
              this.dataInfo.list = this.dataInfo.idx === 0 ? list : this.dataInfo.list.concat(list);
            }else{
              this.dataInfo.hasMore = false;
            }
          }else{
            uni.showToast({
              icon: 'none',
              title: res.msg || '请求数据失败！'
            });
          }
        })
      },
      // 详情
      toDetail(data){
        uni.navigateTo({
          url: `/pages/patientInspectLog/patientInspectLogDetail?patientInspectLogId=${data.id}`
        })
      },
      // 转派
      stationaryTransferFun(data){
        uni.navigateTo({
          url: `/pages/transferWorkOrder/transferWorkOrderUser?selectPatientId=${data.patientId}`,
        });
      },
      // 发起回科
      stationaryReturnFun(data){
        uni.navigateTo({
          url: `/pages/newSendBack/sendBackPatient/sendBackPatient?patientId=${data.patientId}&patientName=${data.patientName}&patientResidenceNo=${data.patientDTO ? data.patientDTO.residenceNo : ''}&patientCode=${data.patientCode}`,
        });
      },
    },
    onLoad(options){
      console.log('options', options)
      this.options = options || {};
      this.stationaryTransfer = this.menuList.some(v => v.link == 'stationaryTransfer');
      this.stationaryReturn = this.menuList.some(v => v.link == 'stationaryReturn');
      this.onLoadFn();
    },
    onTabItemTap(){
      this.onLoadFn();
    },
    onPullDownRefresh(){
      this.getList(0);
    },
    onReachBottom(){
      this.dataInfo.idx += 1;
      if (this.dataInfo.hasMore) {
        this.getList(); // 当触底时加载更多数据
      }
    },
  };
</script>

<style lang="scss" scoped>
.search{
  padding: 10rpx 24rpx;
}
.toolbar {
  position: fixed;
  left: 0;
  bottom: var(--window-bottom);
  z-index: 99;
  width: 100%;
  height: 88rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  border-radius: 4rpx;
  background-color: #E5E8ED;

  .toolbar-icon {
    font-size: 52rpx;
    margin-right: 16rpx;
    color: #49B856;
  }

  .toolbar-sao {
    font-size: 36rpx;
    color: #333;
  }
}
page{
  height: calc(100vh - var(--window-bottom));
}
.incidentList{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .head{
    height: 88rpx;
    display: flex;
    align-items: center;
    position: fixed;
    z-index: 99;
    width: 100%;
    background-color: #fff;
    font-size: 30rpx;
    .tab{
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      border-bottom: 4rpx solid transparent;
      &.active{
        color: #49b856;
        border-color: #49b856;
      }
    }
    .filter{
      width: 84rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      .newicon-shaixuan{
        font-size: 36rpx;
        color: #2C2C2C;
      }
    }
  }
  .body{
    margin-bottom: calc(var(--window-bottom) + 88rpx);
    margin-top: 88rpx;
    border-top: 6rpx solid #EBEBEB;
    .body_item{
      border-bottom: 8rpx solid #EBEBEB;
      .body_item_head{
        word-break: break-all;
        text-align: justify;
        text-align: left;
        margin: 24rpx;
        font-size: 30rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .sign{
          margin-right: 16rpx;
          color: #49B856;
          &.signRed{
            color: #FF0000;
          }
        }
      }
      .body_item_content{
        border-top: 1rpx solid #D8D8D8;
        padding: 24rpx;
        .body_item_content_p{
          color: #6A6A6A;
          font-size: 26rpx;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 24rpx;
          &:last-of-type{
            margin-bottom: 0;
          }
          .name{
            flex: 1;
          }
          .status{
            padding: 4rpx 10rpx;
            border-radius: 20rpx;
            background-color: #DBE8FE;
            font-size: 22rpx;
            color: #006CF9;
          }
          .icon_all{
            .mic-filled,
            .image-filled
            {
              margin-left: 16rpx;
            }
          }
        }
      }
      .body_item_foot{
        border-top: 1rpx solid #D8D8D8;
        font-size: 26rpx;
        padding: 24rpx;
        .foot_info{
          display: flex;
          justify-content: space-between;
          align-items: center;
          .phone-filled{
            margin-left: 5rpx;
          }
        }
        .btns{
          display: flex;
          button{
            flex: 1;
            margin-right: 24rpx;
            &:last-of-type{
              margin-right: 0;
            }
          }
        }
      }
    }
  }
  .zanwu{
    box-sizing: border-box;
    margin-bottom: calc(var(--window-bottom) + 88rpx);
    margin-top: 88rpx;
    border-top: 6rpx solid #EBEBEB;
    height: calc(100vh - var(--window-bottom) - 176rpx);
    display: flex;
    justify-content: center;
    background-color: #F7F7F7;
    .newicon-zanwu{
      font-size: 256rpx;
      color: #D6D6D6;
      margin-top: 140rpx;
    }
  }
}
</style>
