// pages/mine/product/productmanage/productlist.js
const app = getApp()
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		CustomBar: app.globalData.CustomBar,
		productList: [],
		isRefreshing: false,
		currentPage: 1,
		totalSize: 0,
		shopId: '',
		isAddPage: false,
		isCatalogPage: false,
		activeNames: ['0'],
		baseImageUrl: app.urls.baseImgUrl,
		currentCatalogId: '',
		curIndex: 0,
		active: 1,
		curGoodsList: [],
		contentHeight: app.globalData.windowHeight - app.globalData.CustomBar,
		isLoadMore: false,
		hasMore: true,
		selGoods: [], //选中的商品列表
		isFirst: false
	},
	/**
	 * 切换类目
	 */
	switchRightTap(e) {
		let curindex = parseInt(e.currentTarget.dataset.curindex);
		this.data.productList[curindex].goodsList = []
		this.setData({
			currentPage: 1,
			hasMore: true,
			isLoadMore: false,
			curIndex: curindex
		})
		let item = this.data.productList[curindex]
		//根据类目的类型来判断添加商品还是优惠券 2是优惠券
		if (item.coupon == '2') {
			this.getCouponListByCid(item.categoryId)
		} else {
			this.getGoodsListByCId(item.categoryId)
		}
	},
	//获取优惠券列表
	getCouponListByCid(id) {

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			shopId: options.shopId,
			isFirst: true
		})
		this.getShopCatalogList(this.data.shopId)
	},
	//获取店铺所有商品类目
	getShopCatalogList(id) {
		let _self = this
		wx.showLoading({
			title: '加载中...',
		})
		app.http.get(app.urls.getCatalogThirdList, {
				header: {
					token: app.globalData.token
				}
			})
			.then(res => {
				wx.hideLoading({})
				_self.setData({
					isRefreshing: false
				})
				_self.buildDatas(res.data)
			})
			.catch(err => {
				wx.hideLoading({})
			})
	},
	/**
	 * 获取商品列表
	 */
	getGoodsList(shopId) {
		wx.showLoading({
			title: '加载中...',
		})
		app.htt.get(app.urls.getShopGoodsList.format({
				shopId: shopId
			}), {
				header: {
					token: app.globalData.token
				}
			})
			.then(res => {
				wx.hideLoading({})
				this.setData({
					isRefreshing: false
				})
				this.buildDatas(res.data)
			})
			.catch(err => {
				wx.hideLoading({})
			})
	},
	//获取商品的index
	getIndex(item) {
		for (var i = 0; i < this.data.selGoods.length; i++) {
			if (this.data.selGoods[i].goodsId == item.goodsId) {
				return i
			}
		}
		return -1;
	},
	//选择商品
	selGoods(e) {
		var _self = this
		var item = e.currentTarget.dataset.item
		_self.data.productList[_self.data.curIndex].goodsList.forEach(it => {
			if (it.goodsId == item.goodsId) {
				it.isSel = !item.isSel
			}
		})
		if (item.isSel) {
			//取消
			var index = _self.getIndex(item)
			_self.data.selGoods.splice(index, 1)
		} else {
			//添加
			_self.data.selGoods.push(item)
		}
		_self.setData({
			productList: _self.data.productList,
			selGoods: _self.data.selGoods
		})
	},
	//构建数据
	buildDatas(data) {
		let _self = this
		if (data.length > 0) {
			data.forEach(el => {
				el.goodsList = []
				_self.data.productList.push(el)
			})
			_self.setData({
				curIndex: 0,
				productList: _self.data.productList
			})
			_self.getGoodsListByCId(_self.data.productList[0].categoryId)
		}
	},
	//刷新页面
	dorefreshList() {
		this.setData({
			currentPage: 1,
			isLoadMore: false,
			hasMore: true,
			productList: []
		})
		this.getShopCatalogList(this.data.shopId)
	},
	//根据类目ID获取商品
	getGoodsListByCId(id) {
		let _self = this
		wx.showLoading({
			title: '加载中...',
		})
		app.http.get(app.urls.getGoodsByCatalogAndShop.format({
				categoryId: id
			}), {
				header: {
					token: app.globalData.token
				},
				data: {
					page: _self.data.currentPage,
					shopId: _self.data.shopId
				}
			})
			.then(res => {
				wx.hideLoading({})
				_self.setData({
					isLoadMore: false
				})
				if (res.data.rows.length > 0) {
					res.data.rows.forEach(it => {
						it.isSel = false
					})
					_self.data.selGoods.forEach(it => {
						res.data.rows.forEach(ii => {
							if (ii.goodsId == it.goodsId) {
								ii.isSel = true
							}
						})
					})
					_self.data.productList.forEach(el => {
						if (el.categoryId == id) {
							el.goodsList = el.goodsList.concat(res.data.rows)
						}
					})
					_self.setData({
						productList: _self.data.productList
					})
				} else {
					_self.setData({
						hasMore: false
					})
				}
			})
			.catch(err => {
				wx.hideLoading({})
				_self.setData({
					isLoadMore: false
				})
			})
	},
	onChange(e) {
		let _self = this
		let item = e.currentTarget.dataset.item
		this.setData({
			activeNames: e.detail,
			currentCatalogId: item.categoryId
		})
		if (e.detail.length > 1) {
			//展开
			if (item.goodsList || item.goodsList.length <= 0) {
				_self.getGoodsListByCId(item.categoryId)
			}
		}
	},
	//加载更多
	doLoadMore(e) {
		let _self = this
		if (_self.data.hasMore) {
			if (_self.data.isLoadMore) {
				return
			}
			let item = _self.data.productList[_self.data.curIndex]
			_self.setData({
				isLoadMore: true,
				currentPage: ++_self.data.currentPage
			})
			_self.getGoodsListByCId(item.categoryId)
		} else {
			wx.showToast({
				title: '暂无更多数据',
				icon: 'none',
				duration: 500
			})
		}
	},
	confirm() {
		var _self = this
		if (_self.data.selGoods.length <= 0) {
			wx.showToast({
				title: '请选择商品',
			})
		} else {
			let arr = getCurrentPages()
			let lastPage = (arr.length >= 2) ? arr[arr.length - 2] : undefined
			lastPage.setData({
				goodsList: []
			})
			lastPage.setData({
				goodsList: _self.data.selGoods
			})
			wx.navigateBack({})
		}
	}
})