<template>
	<view class="total-page page-box">
		<Nav :title="'领取任务'" :genre="1" :fixedHeight="110"></Nav>
		<view class="task-tabs m-t30">
			<TaskImgTab @setProductId="setProductId"></TaskImgTab>
		</view>
		<!-- <EnButton is_both></EnButton> -->
		<EnScroll ref="scroll" :navHeight="110" class="main" @onRefresh="onRefresh" @onScrollBottom="onScrollBottom">
			<TaskItem :type="2" :task-list="taskList" is_bottom @takeTask="setTakeTask"></TaskItem>
		</EnScroll>
	</view>
</template>

<script>
	import TaskItem from "@/common/task/task-item.vue"
	import TaskImgTab from "@/page_task/module/task_ima_tab.vue"
  import {getTaskReceiving, takeTask} from "@/api/task";
  import tools from "@/service/tools";

	export default {
		components: {
			TaskImgTab,
			TaskItem
		},
		data() {
			return {
        taskList: [],
        page: 1,
        totalNum: 9999,
        isAjax: false,
        productId:0
			}
		},
    mounted() {
      this.getTaskReceiving()
    },
    methods: {
      setProductId(productId){
        console.log('productId:'+productId)
        this.productId=productId
        this.startList()
      },
			// 下拉刷新
      startList() {
        this.taskList = [];
        this.page = 1
        this.totalNum = 99999
        this.getTaskReceiving()
      },
      async getTaskReceiving() {
        if (this.totalNum <= this.taskList.length) {
          return;
        }
        const res = await getTaskReceiving({'page':this.page,'productId':this.productId})
        if (res.code === 1) {
          this.totalNum=res.data.totalNum
          this.taskList.push(...res.data.items)
          ++this.page
        }
      },
      setTakeTask(index){
        console.log('index:'+index)
        this.taskList.splice(index, 1)
        uni.$emit('updateMemberInfo')
      },

			onRefresh() {
				uni.showLoading({
					title: '数据加载中'
				})
				setTimeout(() => {
					uni.showToast({
						title: '加载完成',
						icon: 'none'
					})
					this.$refs.scroll.onEndPulling()
          this.startList()
				}, 1000)
				console.log("下拉刷新");
			},
			// 滚动到底部
			onScrollBottom() {
				uni.showLoading({
					title: '数据加载中'
				})
        this.getTaskReceiving()
				setTimeout(() => {
					uni.showToast({
						title: '加载完成',
						icon: 'none'
					})
				}, 1000)
				console.log("到底部了");
			},
		}
	}
</script>

<style>

</style>