<template>
	  <view class="content">
		   
	  </view>
</template>

<script>
	import { queryPromoProductByPage } from '@/api/video.js'
	import { purchaseSave } from '@/api/purchase.js'
	export default {
	        data() {
	            return {
					extraData:{
						id: 'promoProductList',
						// 添加产品
						chooseProduct:(item)=>{
							this.chooseProduct(item)
						}
					},
					promoActiveSn: null,
					chooseList: [],
					screenWidth: 0,
					showChoosePopu: false,
					queryWord: ''
	            };
	        },
			onLoad(opts) {
				 uni.setNavigationBarTitle({
				 	title: opts.title||'促销活动产品'
				 })
				 this.screenWidth = uni.getSystemInfoSync().windowWidth
				 this.$store.state.vuex_tempData = null
				 this.promoActiveSn = opts.promoActiveSn
				 // 购物车是否有缓存
				 const cacheList = this.$store.state.vuex_cartList.find(k => k.sn == this.promoActiveSn)
				 this.chooseList = cacheList ? JSON.parse(JSON.stringify(cacheList.list)) : []
			},
			onShow() {
				this.showChoosePopu=false
			},
			onUnload() {
				// 存储已选列表值
				const hasCache = this.$store.state.vuex_cartList.find(k => k.sn == this.promoActiveSn)
				if(hasCache){
					hasCache.list = this.chooseList
				}else{
					this.$store.state.vuex_cartList.push({sn:this.promoActiveSn,list: this.chooseList})
				}
				this.$u.vuex('vuex_cartList', this.$store.state.vuex_cartList)
				
				// 清空数据
				this.chooseList = null
			},
	        methods: {
				// 查询列表
				searchList(){
					this.$refs.paging.reload()
				},
				// @query所绑定的方法不要自己调用！！需要刷新列表数据时，只需要调用this.$refs.paging.reload()即可
	            getRow(pageNo, pageSize) {
					console.log('pageNo',pageNo)
					console.log('pageSize',pageSize)
					let _this = this
					let params = {
					  pageNo: pageNo,
					  pageSize: pageSize,
					  queryWord: this.queryWord,
					  promoActiveSn: this.promoActiveSn
					}
					queryPromoProductByPage(params).then(res => {
						if (res.status == 200) {
						  const list = res.data.list||[]
						  const ret = []
						  if(list.length%2!=0){
							  list.push(null)
						  }
						  for(let i=0;i<list.length;i=i+2){
							  const a = list[i]
							  const hasAChecked = this.chooseList.find(s=>s.id == a.id)
							  const b = list[i+1]
							  const hasBChecked = this.chooseList.find(s=>s.id == b.id)
							  // 如果超出
							  if(b){
								  ret.push({list:[this.formatData(a,hasAChecked),this.formatData(b,hasBChecked)]})
							  }else{
								  ret.push({list:[this.formatData(a,hasAChecked)]})
							  }
						  }
						  _this.$refs.paging.complete(ret);
						} 
					})
	            },
				// 格式化数据
				formatData(k,hasChecked){
					return {
						id: k.id,
						cost: k.cost,
						checked: !!hasChecked,
						qty: !!hasChecked ? hasChecked.qty : 0,
						productCode: k.productCode,
						productSn: k.productSn,
						productImage: k.productImage,
						productName: k.productName,
						productOrigCode: k.productOrigCode,
						promoActiveSn: k.promoActiveSn,
						promoRuleSn: k.promoRuleSn,
						promoRuleValue: k.promoRuleValue,
						promoRuleType: k.promoRuleType,
						show: false
					}
				},
				// 添加产品
				chooseProduct(item){
					
				}
	        },
	    };
</script>

<style lang="less" scoped>
.content{
	width: 100%;
	height: 100vh;
}
.tab-search{
	padding: 15rpx 20rpx;
	background: #fff;
}
</style>
