<template>
	<view class="">
		<mescroll-uni :up="up" :down="down" @up="upFn" :fixed="false" @down="downFn" @init="initMeScroll">
			<card :list="list" @confirmReceipt="confirmReceipt"></card>
		</mescroll-uni>
		<toast ref="toast" ></toast>
	</view>
</template>
<script>
	import mescrollUni from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-uni.vue'
	import card from './card.vue'
	export default {
		components: {
			mescrollUni,
			card
		},
		props: {
			type: Number,
			i: Number,
			item: Object
		},
		data() {
			return {
				isInit: false, // 是否初始化
				list: [], // 列表数据
				mescroll: null, // mescroll 对象
				// 上拉配置参数
				up: {
					noMoreSize: 5,
					auto: false,
					page: {
						page: 0,
						size: 10
					}
				},
				// 下拉配置参数
				down: {
					use: false,
					auto: false
				}
			}
		},
		created() {

		},
		watch: {
			type(val) {
				if (!this.isInit && val === this.i) {
					this.mescroll.resetUpScroll()
				}
			}
		},
		mounted() {
			if (!this.isInit && this.i === 0) {
				this.mescroll.resetUpScroll()
			}
		},
		methods: {
			confirmReceipt(id){
				this.$dialog.showModal('确定收货?',true,this.vuex_theme.bgColor).then(res=>{
					let params={
						id,
						goodsStatus:'已完成'
					}
					this.$api.goodsorder.submit(params).then(res=>{
						if (res.data.success) {
							this.$refs.toast.info('操作成功')
							this.mescroll.resetUpScroll()
						}
					})
				})
			},
			//初始化组件
			initMeScroll(mescroll) {
				this.mescroll = mescroll
			},
			//上拉回调
			upFn(mescroll) {
				let params = {
					userId: this.vuex_userId,
					current: mescroll.num,
					size: mescroll.size,
				}
				if (this.item.value!='全部') {
					params.goodsStatus=this.item.value
				}
				
				try {
					this.$api.goodsorder.list(params).then(res => {
						let data = res.data.data.records
						let length = data.length
						let total = res.data.data.total
						mescroll.endBySize(length, total);
						if (mescroll.num == 1) this.list = []; //如果是第一页需手动制空列表
						this.list = this.list.concat(data); //追加新数据
					})
				} catch (e) {
					mescroll.endErr();
				}
			},
			//下拉回调
			downFn(mescroll) {
				setTimeout(() => {
					this.mescroll.resetUpScroll()
				}, 1500)
			},
		}
	}
</script>

<style lang="scss" scoped>
	view {
		box-sizing: border-box;
	}
</style>
