<template>
  <div class="visitRecord">
    <NavBar/>
    <div class="page-container">
      <van-search v-model="query.userName" class="van-hairline--bottom" placeholder="请输入人员姓名"  @change="refreshData"/>
      <div class="search-flex">
        <select-cell
          style="border-right: 1px solid #f5f5f5;"
          title="出入类型"
          :isAll="true"
          :border="false"
          v-model="query.type"
          :data-list="getDictItem('out_in_type')"
          @change="refreshData"/>
        <date-cell title="日期"  v-model="query.arrivalTime" date-type="date" @change="refreshData"/>
      </div>
      <div class="card-list">
        <Scroll
          ref="Scroll"
          @refresh="refreshData"
          @loadMore="getDataList"
          :pullup="pullup">
          <empty  v-if="!dataList || dataList.length === 0"/>
          <card v-else v-for="(v,i) in dataList" :key="v.id">
            <van-cell
            :title-style="{color:v.type == 3?'#D7000F':''}"
            :title="getDictLabel(v.type,'out_in_type')"
            @click="clickItem(v.id)">
              <template #extra>
                <van-button v-if="!v.departureTime" type="info" size="mini">登记离开时间</van-button>
              </template>
            <template #label>
              <div class="info-box">
                <div class="info-item">
                  <div class="item-label">人员姓名：</div>
                  <div class="item-value"> {{v.userName}}</div>
                </div>
                <div class="info-item">
                  <div class="item-label">证件号码：</div>
                  <div class="item-value">{{v.idCard}}</div>
                </div>
                <div class="info-item">
                  <div class="item-label">到达时间：</div>
                  <div class="item-value">
                    {{v.arrivalTime}}
                  </div>
                </div>
                <div class="info-item">
                  <div class="item-label">离开时间：</div>
                  <div class="item-value">
                    {{v.departureTime || '暂无'}}
                  </div>
                </div>
                <div class="info-item">
                  <div class="item-label">出入事由：</div>
                  <div class="item-value">
                    {{v.reasons}}
                  </div>
                </div>
              </div>
            </template>
          </van-cell>
          </card>
        </Scroll>
      </div>
    </div>
    <drag-button @btnClick="clickAdd"></drag-button>
  </div>
</template>
<script>
import NavBar from '@/components/NavBar/index.vue'
import OrgTree from '@/components/orgTree/index.vue'
import Scroll from '@/components/scroll/scroll.vue'
import Card from '@/components/card/index.vue'
import dateCell from '@/components/dateCell/index.vue'
import selectCell from '@/components/selectCell/index.vue'
import {dataList} from './api'
import {mapGetters} from "vuex";
import {formatDate} from "@/filters/filter";
import DragButton from "@/components/DragButton/index.vue";
export default {
  components: {
    DragButton,
    NavBar,
    OrgTree,
    Scroll,
    Card,
    dateCell,
    selectCell,
  },
  data() {
    return {
      query:{
        arrivalTime:`${formatDate(new Date(),'YYYY-MM-DD')}`,
        userName:null,
        pageSize:10,
        pageNum:1,
      },
      dataList:[],
      pullup:false,
      dicts:['out_in_type']
    }
  },
  beforeRouteEnter(to,from,next){
    next(vm=>{
      if(to.params.event === 'refresh'){
        vm.refreshData();
      }
    })
  },
  mounted() {
    this.initData();
  },
  computed:{
    ...mapGetters(['orgId','id','dictionary']),
  },
  methods: {
    clickAdd(){
      this.$router.push({
        path:'/visitUserRecord',
      });
    },
    refreshData(){
      this.pullup = true;
      this.query.pageNum = 1;
      this.total = 0;
      this.dataList = [];
      this.getDataList();
    },
    //初始化数据
    initData(){
      this.query.arrivalTime = formatDate(new Date(),'YYYY-MM-DD');
      this.getDataList();
    },
    //获取数据列表
    getDataList(){
      if( this.dataList.length !== 0 && this.dataList.length >= this.total) {
        this.pullup = false;
        this.$toast('已加载完毕');
        return;
      }
      let data = {
        ...this.query,
        orgId:this.orgId
      }
      dataList(data).then(res=>{
        if(res.total === '0'){
          this.pullup = false;
          this.$toast('已加载完毕');
          return
        }
        this.total = res.total;
        if(this.dataList.length < res.total) {
          this.dataList = [...this.dataList,...res.rows] ;
          this.pullup = true;
          this.query.pageNum++;
          this.$refs.Scroll.refresh();
        }
      })
    },
    clickItem(id){
      this.$router.push({
        path:'/visitRecordDetail',
        query:{id}
      });
    }
  }
}
</script>
<style lang="scss">
.visitRecord{
  .card {
    >.van-cell{
      padding: 0;
    }
  }
  .van-cell-group{
    margin-bottom: 20px;
  }
  .van-cell-group:last-child{
    margin-bottom: 0;
  }
  .vue-table-root{
    tr,td,th{
      font-size: 25px!important;
      color:#666!important;
    }
  }
}
</style>
<style lang="scss" scoped>
.problem-item{

}
.app-container{

}
.card-list{
  padding:0 20px 20px 20px;
  height: calc(100vh - 426px);
  overflow:  auto;
}
.card-num{
  display: flex;
  align-items: center;
  font-size: 28px;
  color: #009dff;
}
.search-flex{
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #f5f5f5;
  >div{
    width: 50%;
  }
}
.btn-box{
  width: 100%;
  height: 100%;
  padding: 30px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.info-box{
  color:#555;
}
.info-desc{
  padding-top: 10px;
  min-height: 50px;
  line-height: 36px;
  max-height: 250px;
  display: -webkit-box;
  -webkit-line-clamp: 3; /* 限制显示为3行 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  >span{
    color:#999;
  }
}
.info-item{
  display: flex;
  justify-content: space-between;
  font-size: 26px;
  .item-label{
    flex:.25;
    text-align: left;
    color:#333;
  }
  .item-value{
    flex:.75;
    color:#666;
    min-height: 50px;
    line-height: 36px;
    max-height: 250px;
    display: -webkit-box;
    -webkit-line-clamp: 3; /* 限制显示为3行 */
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>
