<template>
  <vxe-modal v-model="mShow" width="70%" height="80%" title="扫描日志">
    <template #default>
      <moduleView :propConfig="config" ref="view" v-loading="load" @pagination="pagination" @onRefresh="getList" @resert="queryList" @search="queryList"/>
    </template>
  </vxe-modal>
</template>

<script setup>
  import {scanLog} from '@/api/base/home'
  import { computed,getCurrentInstance,ref,markRaw,shallowRef,defineEmits } from "vue";
  const { proxy } = getCurrentInstance();

  const mShow = ref(false)
  const config = ref({
    attr:{
      calculateH:true
    },
    search:{
      attr:{
        size:'mini'
      },
      columns:[
        [{
          span:14,
          labelWidth:100,
          label:'扫描时间',
          prop:'time',
          component:'by-date',
          compConfig:{
            attr:{
              startPlaceholder:'开始时间',
              endPlaceholder:'结束时间',
              type:'datetimerange'
            }
          }
        }]
      ]
    },
    tool:{
      tools:{
        export:true,
        search:true,
        refresh:true
      }
    },
    table:{
      attr:{
        size:'mini',
        seq:true,
        align:'center',
      },
      columns:[
      {
        title:'商品名称',
        field:'goodsName'
      },
      {
        title:'设备名称',
        field:'equipmentName'
      },
      {
        title:'位置',
        field:'addrInfo'
      },
      {
        title:'扫描用户',
        field:'userName'
      },
      {
        title:'扫描时间',
        field:'time',
        width:160
      }
      ]
    }
  })
  const timeNum = ref(0);
  const load = ref(false);
  const isSearch = ref(false)

  const setShow = v => {
    mShow.value = v;
    getList();
  }

  const pagination = () => {
    if(isSearch.value){
      queryList();
    }else{
      getList()
    }
  }
  //列表请求（包含分页和搜素条件）
  const queryList = () => {
    isSearch.value = true;
    let data = proxy.$refs.view.getQuery();
    requestList(data);
  }
  const getList = () => {
    if(!proxy.$refs.view){
      if(timeNum.value > 5){
        return
      }
      setTimeout(()=>{
        getList()
      },500) 
      timeNum.value ++;
      return
    }
    timeNum.value = 0;
    isSearch.value = false;
    let data = proxy.$refs.view.getPage();
    requestList(data);
  }
  const requestList = data => {
    load.value = true;
    data.pageNum = data.pageNo;
    delete data.pageNo;
    if(data.time && data.time.length > 0) {
      data.startTime = data.time[0]
      data.endTime = data.time[1]
    }
    delete data.time;
    scanLog(data).then(res => {
      load.value = false;
      proxy.$refs.view.setTableValue(res.data.records);
      let page = {
        pageNo: res.data.current, //当前页
        pageSize: data.pageSize, //每页条数
        total: res.data.total //总条数
      };
      proxy.$refs.view.setPage(page)

    }).catch(()=>{
      load.value = false;
    })
  }

  defineExpose({
    setShow
  })


</script>
