<template>
  <moduleView :propConfig="config" ref="view" v-loading="load" @pagination="pagination" @onRefresh="getList" 
    @resert="queryList" @search="queryList" @detail="detail" />
</template>

<script setup>
  import {list} from '@/api/base/adPlan';
  import rangTime from './rangTime.vue';
  import { computed,getCurrentInstance,ref,markRaw,shallowRef } from "vue";
  const { proxy } = getCurrentInstance();
  const load = ref(false)
  const isSearch = ref(false)
  const time= ref(null);
  const timeNum = ref(0);
  const config = ref({
    attr:{
      calculateH:true
    },
    search:{
      columns:[
        [{
          span:8,
          labelWidth:1,
          prop:'text',
          component:'by-input',
          compConfig:{ 
            attr:{
              placeholder:'请输入平台来源、申请商户',
              prefixIcon:'vxe-icon-search'
            }
          }
        },
        {
          span:8,
          // labelWidth:16,
          prop:'time',
          label:'推送时间段',
          component:'by-date',
          compConfig:{
            attr:{
              startPlaceholder:'开始时间',
              endPlaceholder:'结束时间',
              // format:'yyyy-MM-dd',
              type:'datetimerange'
            }
          }
        }]
      ]
    },
    tool:{
      tools:{
        search:true,
        refresh:true
      }
    },
    table:{
      attr:{
        size:'mini',
        seq:true,
        align:'center'
      },
      columns:[{
        title:'广告名称',
        field:'planName',
        width:150
      },{
        title:'来源平台',
        field:'appName',
      },
      {
        title:'商品信息',
        field:'goodsName',
      },
      {
        title:'申请商户',
        field:'commercialName'
      },
      {
        title:'推广设备数',
        field:'equipmentCount'
      },
      {
        title:'推送时间段',
        field:'time',
        width:150,
        component:shallowRef(rangTime)
      },
      {
        title:'状态',
        field:'state'
      },
      {
        title:'申请时间',
        width:140,
        field:'planCreateTime'
      },
      {
        title:'审核时间',
        field:'auditTime',
        width:150
      },
      // {
      //   title:'操作',
      //   action:true,
      //   plugins:[{
      //     name:'人工审核',
      //     audit:'',
      //     event:{
      //       click:item => {
      //         console.log('该行数据：',item)
      //       }
      //     }
      //   }]
      // }
    ]
    },
    modal:{
      tool:{
        tools:{
          return:true
        }
      },
      form:{
        attr:{
          width:'1000px',
          size:'small',
          showType:'desc',
          labelWidth:'130px',
        },
        columns:[
          [{
            label:'推送海报',
            prop:'appName',
          }],
          [{
            label:'来源平台',
            prop:'appName'
          }],
          [{
            label:'商品信息',
            prop:'goodsName'
          }],
          [{
            label:'申请商户',
            prop:'commercialName'
          }],
          [{
            label:'申请状态',
            prop:'state'
          }],
          [{
            label:'申请时间',
            prop:'planCreateTime'
          }],
          [{
            label:'推广设备',
            prop:'planCreateTime'
          }]
        ]
      }
    }
  })

  const detail = () => {
    config.value.modal.tool.tools.save = true;
    delete config.value.modal.tool.tools.add;
    proxy.$refs.view.initFormTool();
  }
  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
    }
    isSearch.value = false;
    let data = proxy.$refs.view.getPage();
    requestList(data);
  }
  const requestList = data => {
    load.value = true;
    data.state = 2;
    data.pageNum = data.pageNo;
    delete data.pageNo;
    if(data.time && data.time.length > 0) {
      data.startTime = data.time[0]
      data.endTime = data.time[1]
    }
    list(data).then(res => {
      load.value = false;
      let datalist = []
      let page ={}
      if(res.data){
        datalist = res.data.records?res.data.records:[];
        page = {
          pageNo: res.data.current, //当前页
          pageSize: data.pageSize, //每页条数
          total: res.data.total //总条数
        };
      }else{
        page = {
          pageNo: 1, //当前页
          pageSize: data.pageSize, //每页条数
          total: 0 //总条数
        };
      }
      proxy.$refs.view.setTableValue(datalist);
      proxy.$refs.view.setPage(page)
    }).catch(()=>{
      load.value = false;
    })
  }
  const calculateTable = () => {
    nextTick(() => {
      proxy.$refs.view.initTable()
    })
  }
  defineExpose({
    calculateTable
  })
  nextTick(()=>{
    getList()
  })
</script>

<style lang="scss" scoped>

</style>