<template>
	<view class="pagesCons">
		<view class="tab-body">
			<view class="uTabs"><u-tabs-swiper ref="uTabs" :list="tabList" name="dispName" :current="current" @change="tabsChange" :is-scroll="false" swiperWidth="750"></u-tabs-swiper></view>
			<swiper class="check-list" :current="swiperCurrent" @transition="transition" @change="swiperChange" @animationfinish="animationfinish">
				<swiper-item 
				class="swiper-item" 
				style="height: 100%;width: 100%;overflow: hidden;" 
				v-for="(tabs, index) in tabList" 
				:key="index">
					<scroll-view scroll-y style="height: 100%;width: 100%;overflow: auto;" @scrolltolower="onreachBottom">
						<view class="check-order-list" 
						v-if="index==current"
						v-for="(item, sindex) in list" 
						:key="item.id">
							<view class="check-row" @click="viewRow(item)">
								<view>{{ item.storeName }}</view>
								<u-tag size="mini" mode="plain" shape="circle" class="item-c-type" :text="item.statusDictValue" :type="item.status | statusCls" />
							</view>
							<view class="check-row" @click="viewRow(item)">
								<view>
									<view style="font-size: 30upx;margin-bottom: 15upx;">{{ item.name }}</view>
									<view class="createDate">{{ item.createDate }}</view>
								</view>
								<view>
									<text v-if="item.status=='PENDING'" style="color:#333;">开始点检</text>
									<text v-if="item.status=='FINISHED'" style="color:#333;">查看结果</text>
									<u-icon v-if="item.status!='EXPIRED'" name="icon-xian-11" custom-prefix="xd-icon" size="28" color="#888888"></u-icon>
								</view>
							</view>
						</view>
						<u-empty style="height: auto;padding-top: 10vh;" :text="noDataText" img-width="120" v-if="list.length == 0 && status != 'loading'" mode="list"></u-empty>
						<view v-if="index==current" style="padding: 20upx;"><u-loadmore v-if="total > pageSize || status == 'loading'" :status="status" /></view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
import { getTasksList } from '@/api/task.js';
import { findStoreVsDevice } from '@/api/store.js'
import { clzConfirm } from '@/libs/tools.js';
export default {
	data() {
		return {
			status: 'loadmore',
			noDataText: '暂无数据',
			tabList: [],
			typeId:'1',
			current: 0,
			swiperCurrent: 0,
			pageNo: 1,
			pageSize: 10,
			list: [],
			total: 0,
			action:'swiperChange', // 操作类型，上划分页，或左右滑动
		};
	},
	onLoad() {
		// 获取任务状态   对象深度克隆
		this.tabList = this.$u.deepClone(this.$store.state.vuex_spotCheckStatus)
		this.tabList.unshift({dispName: '全部', code: ''})
		// 加载列表
		this.resetPage();
		uni.$on("updatePointTaskList",()=>{
			this.resetPage();
		})
	},
	onUnload() {
		uni.$off("updatePointTaskList")
	},
	filters: {
		// 状态颜色标记
		statusCls(val){
			let str = ''
			if(val == 'PENDING'){
				str = 'error'
			}else if(val == 'RUNNING'){
				str = 'primary'
			}else if(val == 'FINISHED'){
				str = 'success'
			}else if(val == 'EXPIRED'){
				str = 'warning'
			}
			return str
		},
	},
	methods: {
		message(title) {
			uni.showToast({
				icon: 'none',
				title: title
			});
		},
		// tabs通知swiper切换
		tabsChange(index) {
			this.swiperCurrent = index;
		},
		swiperChange(e) {
			this.action = 'swiperChange'
			this.status = 'loading';
			this.typeId = this.tabList[e.detail.current].code
		},
		// swiper-item左右移动，通知tabs的滑块跟随移动
		transition(e) {
			let dx = e.detail.dx;
			this.$refs.uTabs.setDx(dx);
		},
		// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
		// swiper滑动结束，分别设置tabs和swiper的状态
		animationfinish(e) {
			let current = e.detail.current;
			let isCurtab = this.current == current
			if(this.status!="nomore"){
				let loadData = this.action == 'swiperChange' ? !isCurtab : isCurtab;
				if(loadData){
					this.$refs.uTabs.setFinishCurrent(current);
					this.swiperCurrent = current;
					this.current = current;
					this.resetPage();
				}
			}
		},
		// scroll-view到底部加载更多
		onreachBottom() {
			this.action = 'onreachBottom'
			if (this.list.length < this.total) {
				this.pageNo += 1;
			} else {
				this.status = 'nomore';
			}
		},
		resetPage(){
			this.status = 'loading';
			// 上划分页
			if(this.action == 'onreachBottom'){
				this.getRow();
			}
			// 左右切换tab
			if(this.action == 'swiperChange'){
				this.list = [];
				this.getRow(1);
			}
		},
		// 查询列表
		getRow(pageNo) {
			let _this = this;
			if (pageNo) {
				this.pageNo = pageNo;
			}
			let params = {
				pageNo: this.pageNo,
				pageSize: this.pageSize,
				type: 'POINT_INSPECTION', // 任务类型
				status: this.typeId=='1'?'':this.typeId // 任务状态
			};
			this.status = 'loading';
			getTasksList(params).then(res => {
				if (res.status == 200) {
					if (_this.pageNo > 1) {
						_this.list = _this.list.concat(res.data.list || []);
					} else {
						_this.list = res.data.list || [];
					}
					_this.total = res.data.count || 0;
				} else {
					_this.list = [];
					_this.total = 0;
					_this.noDataText = res.message || '暂无数据';
				}
				_this.status = 'loadmore';
			});
		},
		//  开始点检
		toSpotCheck(item){
			console.log(item)
			console.log('/pages/spotCheck/spotCheck?taskId=' + item.id + '&types=video&storeId='+item.storeId+'&storeName='+item.storeName)
			// 判断当前门店是否已绑定设备
			findStoreVsDevice({storeCode:item.storeCode}).then(res=>{
				if(res.data.length){
					// 门店下的设备
					this.$u.vuex('vuex_storeVideoList',res.data)
					uni.navigateTo({
						url: '/pages/spotCheck/spotCheck?taskId=' + item.id + '&types=video&storeId='+item.storeId+'&storeName='+item.storeName
					}); 
				}else{
					uni.showToast({
						icon: 'none',
						title: '当前门店没有绑定设备，暂时不能点检巡店'
					})
				}
			})
		},
		//判断状态，待处理的跳转的开始点检页面，已完成的查看点检结果，已过期的不处理
		viewRow(item) {
			console.log(item)
			// 已完成的
			if(item.status == 'FINISHED'){
				uni.navigateTo({
					url: '/pages/spotCheckCenter/spotCheckResult?id=' + item.id
				});
			}
			// 待处理，开始点检
			if(item.status == 'PENDING'){
				this.toSpotCheck(item)
			}
		},
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
	background: #F8F8F8;
}
.pagesCons {
	height: 100%;
	display: flex;
	flex-direction: column;
	.tab-body {
		.uTabs{
			border-bottom: 1px solid #EEEEEE;
			box-shadow: 1px 1px 3px #eeeeee;
		}
		.check-list {
			height: calc(100vh - 56px);
		}
		.check-order-list {
			background: #ffffff;
			padding: 10upx 20upx;
			margin: 25upx;
			border-radius: 6upx;
			box-shadow: 1px 1px 3px #eeeeee;
			.check-row {
				display: flex;
				align-items: center;
				padding: 20upx 10upx;
				font-size: 28upx;
				&:first-child {
					border-bottom: 1px solid #f8f8f8;
				}
				&:last-child {
					border-top: 1px solid #f8f8f8;
					font-size: 26upx;
				}
				.createDate{
					color: #999;
				}
				> view {
					&:first-child {
						flex-grow: 1;
					}
				}
				.item-c-type{
					flex-shrink: 0;
				}
				.action {
					margin: 0 6rpx;
					padding: 0 20rpx;
					color: #fff;
				}
				.dcl {
					color: #f72525;
				}
				.jxz{
					color: #007AFF;
				}
				.ygq {
					color: #999;
				}
				.ywc {
					color: #10c71e;
				}
				.check-btns {
					width: '50%';
					display: flex;
					align-items: center;
					justify-content: flex-end;
					> view {
						margin-left: 35upx;
						font-size: 28upx;
					}
				}
				.uni-icons {
					margin: 0 5upx;
				}
			}
		}
	}
}
</style>
