import { Component, Prop, Vue, Watch } from 'vue-property-decorator'
import VueViews from '@/benyun/compVue/VueViews'
import lodash from 'lodash' //导入深度拷贝
export default class ModuleViewHandle extends VueViews{
  searchID=this.getUuid(); //搜索id
  toolID=this.getUuid(); // 工具栏id
  tableID=this.getUuid(); //表格id
  viewID=this.getUuid();
  formID=this.getUuid();
  hideSearch=false  // 隐藏/显示搜索
  load=false
  time:any;
  timeNum = 0;

  constructor() {
    super()
  }

  //显示/隐藏搜索
  toggleSearch(){
    this.hideSearch = !this.hideSearch
  }

  getUuid(){
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }

  //获取查询值
  getQuery(){
    let query:any = {};
    let page:any = {};
    if(this.$refs[this.searchID]){
      query = (this.$refs[this.searchID] as any).getValue();
    }
    if(this.$refs[this.tableID]){
      page = (this.$refs[this.tableID] as any).getPage();
    }
    query.pageNo = page.pageNo;
    query.pageSize = page.pageSize;
    return query;
  }

  //获取数据列表
  getList(){
    if(!this.requestConfig || !this.requestConfig.url){
      return
    }
    if(!this.$refs[this.tableID]){
      if(this.timeNum > 4){
        return
      }
      setTimeout(() => {
        this.getList()
      },500)
      this.timeNum ++;
      return
    }
    clearInterval(this.time)
    let query = this.getQuery();
    this.$emit("listAfter",query);
    this.load = true;
    this.requestHandle({
      url: this.requestConfig.url+'/list',
      method: 'get',
      params:query,
      success:(res:any) => {
        this.load = false;
        const data:Array<any>=res.rows?res.rows:res.data?res.data:[];
        if(this.$refs[this.tableID]){
          (this.$refs[this.tableID] as any).setValue(data);
          (this.$refs[this.tableID] as any).setPage({total:res.total})
        }
      },
      fail:(err:any) => {
        this.load = false;
        this.failHandle(err);
      }
    })
  }

  //删除
  // onDelete(){
  //   let data = (this.$refs[this.tableID] as any).getSelectData();
  //   if(!data || data.length == 0){
  //     this.$message('请选择数据！')
  //     return
  //   }
  //   let id = '';
  //   data.forEach((item:any) => {
  //     if(item.id){
  //       id = id ? id + ',' + item.id : item.id
  //     }
  //   });
  //   this.requestHandle({
  //     url: this.requestConfig.url+'/'+id,
  //     method: 'delete',
  //     success:(res:any) => {
  //       this.load = false;
  //       this.getList();
  //     },
  //     fail:(err:any) => {
  //       this.load = false;
  //       this.failHandle(err);
  //     }
  //   })
  // }

  //刷新
  onRefresh(){
    (this.$refs[this.tableID] as any).setPage({pageNo:1,total:0});
		this.$emit('onRefresh')
  }
  // onRefresh(){
  //   (this.$refs[this.tableID] as any).setPage({pageNo:1,total:0})
  //   this.getList();
	// 	this.$emit('onRefresh')
  // }

  //搜索
  searchHandle(){
    (this.$refs[this.tableID] as any).setPage({pageNo:1,total:0})
    this.getList();
  }
  //重置
  resertHandle(){
    (this.$refs[this.searchID] as any).clearValue({});
    (this.$refs[this.tableID] as any).setPage({pageNo:1,total:0})
    this.$emit('resert')
    // this.getList();
  }

  //导出
  onExport(){
    if(this.requestConfig?.url){
      let urlArr = this.requestConfig.url.split('/');
      let query = this.getQuery();
      (this as any).$download(this.requestConfig.url + '/export',{
        ...query
      },urlArr[urlArr.length - 1] + `_${new Date().getTime()}.xlsx`)
    }
    
  }

  failHandle(err:any){
    let msg = err.msg ? err.msg :'运行错误！';
    this.$message.error(msg)
  }

}
