let WxParse = require('../../lib/wxParse/wxParse')

let util = require('../../utils/util')
let api = require('../../config/api')

Page({
  data: {
		id: 0, // 商品id
		productId: 0, // 规格id
		shareId: 0, // 分享id
		shareUrl: '', // 分享图片二维码

    show_spec: false, // 是否显示 商品规格弹出框
		
    swiper_index: 0, // 当前图片index
		goodsData: null,
		
		collectId: 0, // 收藏id
  },

  onLoad: function({ scene, obj }) {
		let id = 0
		let productId = 0
		let shareId = 0
		if (scene) { // 分享
			// 商品id,商品类型,商品活动id,分享用户id
			[id, shareId] = decodeURIComponent(scene).split(',')
		} else {
			const data = JSON.parse(obj)
			id = data.id
			productId = data.productId ? data.productId : 0
		}
		this.setData({ id, productId, shareId })
		if(wx.getStorageSync('token')) this.setBrowse(id)
	},
	
	onShow() {
		this.getGoodsDetail()
	},

	// 足迹
	setBrowse(goodsId) {
		util.showLoad('加载中')
    util.request(api.AddBrowse, { goodsId },'POST').then(res => {
      util.hideLoad()
      if (res.errno != 0) util.showErrorToast(res.errmsg)
    }).catch(() => {
      util.hideLoad()
      util.showErrorToast('网络连接失败')
    })
	},

  getGoodsDetail() {
		const { id, productId } = this.data

		util.showLoad('加载中')
    util.request(api.GoodsDetail, { id }).then(res => {
      util.hideLoad()
      if (res.errno === 0) {
				const { info, productList, collectId } = res.data
				const products = productList.map(p => {
					p.stock = p.number
					return p
				})

				
				WxParse.wxParse('goodsDetail', 'html', info.detail, this)
				const picUrl = info.picUrl.split(',')

				const pro_i = productId ? products.findIndex(p => p.id == productId) : 0

				const goodsData = {
					...info,
					detail: undefined,
					products,
					curSpec: products[pro_i],
					picUrl
				}
				delete goodsData.detail

				this.setData({ goodsData, collectId: collectId || 0 })
      } else util.showErrorToast(res.errmsg)
    }).catch(() => {
      util.hideLoad()
      util.showErrorToast('网络连接失败')
    })
	},

  prevent() {
    return false
  },

  // 设置当前图片index
  setPicIndex({ detail }) {
    this.setData({ swiper_index: detail.current })
  },

  // 预览商品图片
	previewImage({ currentTarget }) {
		const { current, urls } = currentTarget.dataset
		wx.previewImage({ current, urls })
	},

	// 收藏取消收藏
	toggleCollect() {
		const { collectId, id } = this.data
		util.showLoad('加载中')
		if (collectId) { // 取消收藏
			util.request(api.CollectDelete, [collectId], 'POST').then(res => {
				util.hideLoad()
				if (res.errno === 0) {
					util.showToast('已取消收藏')
					this.setData({ collectId: 0 })
				} else util.showErrorToast(res.errmsg)
			}).catch(() => {
				util.hideLoad()
				util.showErrorToast('网络连接失败')
			})
		} else {
			util.request(api.CollectAdd, id, 'POST').then(res => {
				util.hideLoad()
				if (res.errno === 0) {
					util.showToast('已添加收藏')
					this.setData({ collectId: res.data.id })
				} else util.showErrorToast(res.errmsg)
			}).catch(() => {
				util.hideLoad()
				util.showErrorToast('网络连接失败')
			})
		}
	},

  // 显示规格弹出框
  showSpec() {
		this.setData({ show_spec: true })
	},
	
  // 隐藏规格弹出框
  hideSpec() {
		this.setData({ show_spec: false })
	},

	onShareAppMessage({ from }) {
		const { id, productId, goodsData } = this.data
		const obj = { id, productId }

		const promise = new Promise(resolve => {
			resolve({
				title: '富玖铭商户',
				path: `/packageGoods/detail/detail?obj=${JSON.stringify(obj)}`
			})
		})
		return from == "button" ? { title: goodsData.name, imageUrl: goodsData.picUrl[0] } : promise
	},

	onShareTimeline() {}
})