const api = require('../../config/api')
const util = require('../../utils/util')
const check = require('../../utils/check')
let app = getApp()

Page({
  data: {
		show: false,
		nickname: "",
		mobile: "",

		banner: [],
    channel: [],
    
    categoryId: -2, // 底部商品分类id
    category: [
      { id: -2, name: '推荐' },
      { id: -1, name: '新品' }
    ],
    floorGoods: [],
    page: 1,
    totalPages: 1
	},
	
  onLoad() {
		const token = wx.getStorageSync('token'), userInfo = wx.getStorageSync('userInfo')
		if (token && userInfo && (!userInfo.mobile || userInfo.mobile.length != 11)) this.setData({ show: true })
    this.getIndexData()
  },

  onPullDownRefresh() {
    this.getIndexData()
    wx.stopPullDownRefresh()
  },

  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
			this.setData({ page: this.data.page + 1 })
      this.getfloorGoods()
    }
  },

  getIndexData() {
    util.showLoad('加载中')
    util.request(api.IndexUrl).then(res => {
      util.hideLoad()
      if (res.errno === 0) {
				res.data.floorGoodsList.forEach(g => g.picUrl = g.picUrl.split(','))
        this.setData({
					banner: res.data.banner,
          floorGoods: res.data.floorGoodsList,
          totalPages: res.data.totalPages
				})
				if (res.data.channel && res.data.channel.length) this.setData({ channel: res.data.channel.splice(0, 8) })
      } else util.showErrorToast(res.errmsg)
    }).catch(() => {
      util.hideLoad()
      util.showErrorToast('网络连接失败')
    })
  },

  // 获取底部 分类商品
  getfloorGoods(e) {
		const { page, categoryId } = this.data
    util.showLoad('加载中')
    util.request(api.GetfloorGoods, { page, categoryId }).then(res => {
      util.hideLoad()
      if (res.errno === 0) {
				res.data.goodsList.forEach(g => g.picUrl = g.picUrl.split(','))
				const floorGoods = e ? res.data.goodsList : this.data.floorGoods.concat(res.data.goodsList)
        this.setData({ totalPages: res.data.totalPages, floorGoods })
      } else util.showErrorToast(res.errmsg)
    }).catch(() => {
      util.hideLoad()
			util.showErrorToast('网络连接失败')
		})
  },

  // 切换底部分类
  changeCategory({ currentTarget }) {
		const { id } = currentTarget.dataset
		this.setData({ categoryId: id, page: 1 })
    this.getfloorGoods('reset')
	},
	
  toCatelog({ currentTarget }) {
    const { id } = currentTarget.dataset
    app.globalData.menuId = id
    wx.switchTab({ url: '/pages/catalog/catalog' })
  },

  // 商品详情
  toDetail({ currentTarget }) {
    const { item } = currentTarget.dataset
    wx.navigateTo({ url: `/packageGoods/detail/detail?obj=${JSON.stringify({ id: item.id })}` })
	},

	hidePopup() {
		this.setData({ show: false, nickname: "", mobile: "" })
	},

	nameInput({ detail }) {
    this.setData({ nickname: detail })
	},

	mobileInput({ detail }) {
    this.setData({ mobile: detail })
	},

	updateUser() {
		const { nickname, mobile } = this.data
		const { id } = wx.getStorageSync('userInfo')

		if (!check.validNickName(nickname)) return util.showToast('请输入2-10个字符')
		if (!check.validPhone(mobile)) return util.showToast('请输入正确手机号')
		
		util.showLoad('加载中')
		util.request(api.UserUpdate, { id, nickname, mobile }, 'POST').then(res => {
			util.hideLoad()
			if (res.errno === 0) {
				wx.setStorageSync('userInfo', { ...wx.getStorageSync('userInfo'), nickname, mobile })
				this.hidePopup()
			} else util.showErrorToast(res.errmsg)
		}).catch(() => {
			util.hideLoad()
			util.showErrorToast('网络连接失败')
		})
	},
	
	onShareAppMessage() {}
})
