const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
import header from './data'
import { Router } from '../../utils/router.js'

//获取应用实例
Router({
  data: {
    showSearch: false,
    activeType:-1,
    header: header,
    noRecord:'',
    hideMask: false,
    inputShowed: false,
    keyword:'',
    url:'https://www.4dkankan.com/eShopMobile.html?m=',
    totalPages: 1,
    currentPage: 1,
    size: 10,
    loading: false,
    brandList:[]
  },
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh(){
    this.setData({
      brandList: this.data.brandList.slice(0,10),
      currentPage: 1
    });
    this.getBrandList(1,false,true);
    getApp().onPullDownRefresh()
  },

  onReachBottom() {
    if (!this.data.loading) {
      this.loadMore();
      console.log('reach down')
    }
  },

  loadMore: function () {
    if (this.data.currentPage<this.data.totalPages) {
      console.log(this.data.currentPage + 1)
      this.getBrandList(this.data.currentPage + 1);
    } else {
      return;
    }
  },

  getBrandList(page,isSearch=false,refresh=false){
    
    this.setData({
      loading: true
    })
    let tempContent = this.data.brandList ?
      this.data.brandList :
      [];
    let { activeType, size, keyword,} = this.data
    
    util.request(api.IndexList, { type: activeType, page, name:keyword, size })
      .then(res=> {
        if (isSearch){
          // if (res.data.data.length <= 0) {
          //   this.setData({
          //     noRecord: true
          //   });
          // }
          // else {
            wx.pageScrollTo({
              scrollTop: 0
            })
            this.setData({
              brandList: res.data.data,
              currentPage: res.data.currentPage,
              totalPages: res.data.totalPages,
              loading: false,
              noRecord: false,
              showSearch: false
            });
          // }
        }
        else if(refresh){
          this.setData({
            brandList: res.data.data,
            currentPage: res.data.currentPage,
            totalPages: res.data.totalPages,
            loading: false
          });
        }
        else {
          this.setData({
            brandList: tempContent.concat(res.data.data),
            currentPage: res.data.currentPage,
            totalPages: res.data.totalPages,
            loading: false
          });
        }

        

      });
  },

  inputChange: function (e) {
    let val = e.detail.value
    this.setData({
      keyword:val,
      noRecord: false
    })

    this.search()
  },

  handleScroll(ev) {
    this.setData({isFixed: ev.detail.isFixed})
  },
  search:function(){
    let {keyword} = this.data
 
    this.getBrandList(1, true)
    // if (this.brandList.length <= 0) {
    //   this.setData({
    //     noRecord: true
    //   })
    // }
    // else {
    //   this.setData({
    //     activeType: -1,
    //     noRecord: false,
    //     showSearch: false
    //   })
    // }
  },

  inputFocus: function () {

  },

  tapHeaderBar(e){
    let { id } = e.currentTarget.dataset
    this.setData({
      activeType: id,
      showSearch: false,
      keyword: '',
      brandList: [],
      currentPage: 1
    })
    wx.pageScrollTo({
      duration: 0,
      scrollTop: 0
    })
    this.getBrandList(1)
  },

  tabShow(){
    this.setData({
      showSearch: true
    })
  },


  tabHide() {
    this.setData({
      showSearch: false,
      noRecord:false,
      keyword:''
    })
  },


  

  onLoad: async function (options) {
    getApp().checkNetStatu();
    // this.checkNetStatu()
    this.setData({
      imgServer: util.imgServer
    })

    if (options.id !== 'search') {
      this.setData({activeType: options.id})
    } else {
      this.setData({inputShowed: true})
    }
    this.getBrandList(1)
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function (options) {
    if (global.type) {
      this.tapHeaderBar({
        currentTarget: {
          dataset: this.data.header[0]
        }
      })
      global.type = null
    }
    if(typeof this.getTabBar === 'function' && this.getTabBar()){
      this.getTabBar().setData({
        selected:0
      })
    }

    getApp().updateCardCount()
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  gotoWV: function (event){
    let { id } = event.currentTarget.dataset
    
    wx.navigateTo({
      url: `/pages/webview/index?id=${id}`,
      // url: `/pages/share/share?id=${id}`,
    })
  }
})
