<template>
  <div class="print-count">
    <module-view :propConfig="config" ref="view" v-loading="load" @pagination="pagination" @onRefresh="getList" @resert="queryList" @search="queryList"/>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { query} from '@/api/printCount'
@Component({components:{}})
export default class PrintCount extends Vue {
  load=false;
  isSearch=false
  timeNum = 0;
  config:any={
    attr:{
      calculateH:true
    },
    search:{
      attr:{
        size:'small'
      },
      columns:[
        [{
          span:8,
          label:'模板名称',
          prop:'templateName',
          component:'by-input',
          compConfig:{
            attr:{
              clearable:true
            }
          }
        },{
          span:8,
          label:'外部用户标识',
          prop:'userName',
          component:'user-modal'
        }]
      ]
    },
    tool:{
      tools:{
        search:true,
        refresh:true
      }
    },
    table:{
      attr:{
        size:'mini',
        seq:true,
        checkbox:true
      },
      columns:[{
        title:'外部用户标识',
        field:'userName',
        width:130
      },{
        title:'模板名称',
        field:'templateName'
      },{
        title:'打印次数',
        field:'printCount',
        width:130
      },{
        title:'最后一次打印时间',
        field:'lastTime',
        width:160
      }]
    }
  }
  mounted(){
    this.getList();
  }
  //分页
  pagination(){
    if(this.isSearch){
      this.queryList();
    }else{
      this.getList()
    }
  }
  //列表请求（只有分页，不包含搜素条件）
  getList(){
    if(!this.$refs.view){
      if(this.timeNum > 5){
        return
      }
      setTimeout(()=>{
        this.getList()
      },500) 
      this.timeNum ++;
      return
    }
    this.isSearch = false;
    let data = (this.$refs.view as any).getPage();
    delete data.total;
    this.requestList(data);
  }
  //列表请求（包含分页和搜素条件）
  queryList(){
    this.isSearch = true;
    let data = (this.$refs.view as any).getQuery();
    delete data.total;
    this.requestList(data);
  }
  requestList(data:any){
    this.load = true;
    query(data).then((res:any) => {
      this.load = false;
      (this.$refs.view as any).setTableValue(res.data.records);
      let page = {
        pageNo: res.data.current, //当前页
        pageSize: res.data.size, //每页条数
        total: res.data.total //总条数
      };
      (this.$refs.view as any).setPage(page)

    }).catch(()=>{
      this.load = false;
    })
  }
}
</script>

<style lang="scss" scoped>
.print-count{
  width: 100%;
  height: 100%;
  overflow-y: hidden;
}
</style>