<template>
	<view class="myShopTour-wrap">
		<!-- 内容 -->
		<view class="myShopTour-con">
			<u-tabs-swiper ref="uTabs" :list="tabList" name="dispName" :current="current" @change="tabsChange" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			<swiper class="data-list" :current="swiperCurrent" @transition="transition" @change="swiperChange" @animationfinish="animationfinish">
				<swiper-item class="swiper-item" v-for="(tabs, index) in tabList" :key="index">
					<scroll-view scroll-y class="scroll-con" @scrolltolower="onreachBottom">
						<view class="record-con">
							<!-- 筛选日期，近7天 -->
							<view v-show="searchForm.creatTime.length != 0" class="alert-tips">
								<text>{{searchForm.creatTime[0]}} ~ {{searchForm.creatTime[1]}}</text>
							</view>
							<view class="record-item" v-if="index==current" v-for="(item,sindex) in list" :key="item.id">
								<view class="item-head">
									<text class="item-c-tit">{{item.name}}</text>
									<u-tag class="item-c-type" :text="item.status" type="primary" />
								</view>
								<view class="item-main">
									<text class="item-tit">创建时间：</text>
									<text class="item-val">{{item.startTime}}</text>
								</view>
								<view class="item-main">
									<text class="item-tit">完成时间：</text>
									<text class="item-val">{{item.endTime}}</text>
								</view>
								<view class="item-main">
									<text class="item-tit">巡店耗时：</text>
									<text class="item-val">{{item.spendTime}}秒</text>
								</view>
								<u-grid class="item-grid" :col="4" hover-class="none">
									<u-grid-item>
										<text class="grid-num text-blue">{{item.totalTargetCount}}</text>
										<text class="grid-text">检查项</text>
									</u-grid-item>
									<u-grid-item>
										<text class="grid-num text-green">{{item.passTargetCount}}</text>
										<text class="grid-text">合格</text>
									</u-grid-item>
									<u-grid-item>
										<text class="grid-num text-red">{{item.notPassTargetCount}}</text>
										<text class="grid-text">不合格</text>
									</u-grid-item>
									<u-grid-item>
										<text class="grid-num text-yellow">{{item.notWorkTargetCount}}</text>
										<text class="grid-text">不适用</text>
									</u-grid-item>
								</u-grid>
							</view>
						</view>
						<u-empty text="暂无数据" img-width="120" v-if="list.length == 0 && status != 'loading'" mode="list"></u-empty>
						<u-loadmore bg-color="#f8f8f8" v-if="list.length < total || status == 'loading'" :status="status" class="loadmore" />
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 筛选弹框 -->
		<u-popup v-model="openScreen" mode="right" length="80%" class="screenModel-wrap">
			<!-- 条件项 -->
			<view class="condition-con">
				<view class="screen-module">
					<text class="screen-tit">创建时间：</text>
					<view class="screen-time-val">
						<text @click="onShowDatePicker('range')">{{ range.length == 0 ? '请选择' : range[0] + '~' + range[1] }}</text>
						<u-icon v-show="range.length != 0" name="close-circle-fill" color="#c8c0cc" size="32" class="close-circle" @click="range=[]"></u-icon>
					</view>
				</view>
				<view class="screen-module">
					<text class="screen-tit">门店：</text>
					<view class="screen-inp-val">
						<u-input class="screen-inp" v-model="storeName" :height="52" placeholder="请输入" type="text" :border="true" />
					</view>
				</view>
			</view>
			<!-- 按钮 -->
			<view class="btns">
				<u-button type="warning" shape="circle" size="mini" class="btn reset-btn" @click="resetF">重置</u-button>
				<u-button type="error" shape="circle" size="mini" class="btn ok-btn" @click="submitF">确定</u-button>
			</view>
		</u-popup>
		<!-- 选择创建时间 -->
		<w-picker
			:visible.sync="showPicker"
			mode="range" 
			startYear="2017" 
			endYear="2030"
			:value="searchForm.creatTime"
			:current="false"
			@confirm="onSelected($event,'range')"
			@cancel="onSelected"
			ref="range"></w-picker>
	</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue"
	import { queryMyTask } from '@/api/task.js'
	export default {
		components: { wPicker },
		data() {
			return {
				tabList: [  //  tab切换  类型
					{ dispName: '全部', code:'' },
					{ dispName: '待处理', code:'0' },
					{ dispName: '进行中', code:'1' },
					{ dispName: '已完成', code:'2' },
					{ dispName: '已过期', code:'3' }
				],
				current: 0,  //  tabs组件的current值，表示当前活动的tab选项
				swiperCurrent: 0,  //  swiper组件的current值，表示当前那个swiper-item是活动的
				pageNo: 1,  //  当前页码
				pageSize: 15,  //  一页多少条
				total: 0,  //  总条数
				status: 'loading',  //  加载状态
				noDataText: '暂无数据',  //  列表数据为空时提示文字
				list: [],  //  数据列表
				openScreen: false,  //  是否打开筛选
				dateMode: 'range',  //  date单个日期  range日期范围
				searchForm: {  //  查询条件
					creatTime: [],  //  创建时间默认筛选近7天
					store: ''
				},
				showPicker: false,  //  是否选择创建时间
				range: [],  //  筛选创建时间的默认值
				storeName: '',  //  筛选的门店名称
				typeId: '',  //  记录状态
				action:'swiperChange', // 操作类型，上划分页，或左右滑动
			}
		},
		onLoad() {
			this.resetPage()
			this.range = [ this.get7day(-6), this.get7day(0) ]
			this.searchForm.creatTime = [ this.get7day(-6), this.get7day(0) ]
			uni.$on("updatePointTaskList",()=>{
				this.resetPage()
			})
		},
		onUnload() {
			uni.$off("updatePointTaskList")
		},
		methods: {
			// 查询列表
			pageInit(pageNo){
				if (pageNo) {
					this.pageNo = pageNo
				}
				let params = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					beginDate: this.searchForm.creatTime[0],
					endDate: this.searchForm.creatTime[1],
					storeName: this.searchForm.store,
					status: this.typeId==''?'':this.typeId // 任务状态
				}
				this.status = 'loading'
				queryMyTask(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'
					
				})
			},
			// 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.pageInit()
				}
				// 左右切换tab
				if(this.action == 'swiperChange'){
					this.list = []
					this.pageInit(1)
				}
			},
			//  选择日期范围
			onSelected(e){
				this.showPicker = false
				if(e) {
					this.range = e.value
				}
			},
			//  显示日期选择
			onShowDatePicker(type){
				this.showPicker = true
				this.searchForm.creatTime = this.range
			},
			//  获取近几天的数据
			get7day(day){
				let today = new Date()
				let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
				today.setTime(targetday_milliseconds)  //注意，这行是关键代码
				let tYear = today.getFullYear()
				let tMonth = today.getMonth()
				let tDate = today.getDate()
				tMonth = this.doHandleMonth(tMonth + 1)
				tDate = this.doHandleMonth(tDate)
				return tYear + "-" + tMonth + "-" + tDate
			},
			//  月份
			doHandleMonth(month){
			    let m = month
			    if(month.toString().length == 1){
					m = "0" + month
			    }
			    return m
			},
			//  打开筛选条件
			onNavigationBarButtonTap(e) {
				this.resetF()
				this.openScreen = true
			},
			//  重置筛选条件
			resetF(){
				this.range = this.searchForm.creatTime
				this.storeName = this.searchForm.store
			},
			//  确定筛选
			submitF(){
				this.searchForm.creatTime = this.range
				this.searchForm.store = this.storeName
				this.openScreen = false
				this.pageInit()
			},
		}
	}
</script>

<style lang="scss" scoped>
page {
	background-color: #f8f8f8;
	height: calc(100vh - 44px);
}
.myShopTour-wrap {
	height: 100%;
	// 内容
	.myShopTour-con{
		.data-list{
			height: calc(100vh - 84px);
			.swiper-item{
				height: 100%;
				width: 100%;
				overflow: hidden;
				.scroll-con{
					height: 100%;
					width: 100%;
					overflow: auto;
					// 列表
					.record-con{
						padding-top: 74upx;
						position: relative;
						.alert-tips{
							position: fixed;
							top: 0upx;
							left: 0;
							width: 100%;
							z-index: 3;
							padding: 20upx 30upx 10upx;
							background-color: #fff;
							text{
								display: block;
								font-size: 24upx;
								padding: 4upx 10upx;
								border: 1upx solid #fcbd71;
								background-color: #fdf6ec;
								border-radius: 6upx;
							}
						}
						.record-item{
							background-color: #fff;
							margin: 20upx 30upx 20upx;
							padding: 14upx 30upx 20upx;
							border-radius: 12upx;
							.item-head{
								display: flex;
								justify-content: space-between;
								align-items: center;
								padding: 10upx 0;
								.item-c-tit{
									flex-grow: 1;
									padding-right: 18upx;
									font-weight: bold;
								}
								.item-c-type{
									flex-shrink: 0;
								}
							}
							.item-main{
								font-size: 26upx;
								line-height: 48upx;
								padding: 6upx 0;
								position: relative;
								.item-tit{
									display: inline-block;
									position: absolute;
									left: 0;
									top: 8upx;
								}
								.item-val{
									display: inline-block;
									padding-left: 150upx;
								}
							}
							.item-grid{
								margin-top: 10upx;
								.text-blue{
									color: #2979ff;
								}
								.text-green{
									color: #19be6b;
								}
								.text-red{
									color: #fa3534;
								}
								.text-yellow{
									color: #ff9900;
								}
								.grid-num{
									font-weight: bold;
								}
								.grid-text{
									font-size: 26upx;
									padding-top: 10upx;
									color: #666;
								}
							}
						}
					}
					.loadmore{
						padding: 30upx;
					}
				}
			}
		}
	}
	.screenModel-wrap{
		padding-bottom: 80upx;
		.condition-con{
			padding: 20upx 30upx;
			.screen-module{
				margin: 0 0 20upx;
				.screen-tit{
					color: #464646;
					font-size: 26upx;
				}
				.screen-time-val{
					font-size: 26upx;
					background-color: #f4f4f4;
					border-radius: 12upx;
					padding: 10upx 14upx;
					margin: 10upx 0;
					text-align: center;
					text{
						display: inline-block;
						width: 85%;
					}
					.close-circle{
						display: inline-block;
						padding-left: 3%;
						vertical-align: bottom;
					}
				}
				.screen-inp-val{
					margin: 10upx 0;
					.screen-inp{
						color: #464646;
						font-size: 26upx;
					}
				}
			}
		}
		.btns{
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			background-color: rgba(0,0,0,.025);
			.btn{
				width: 40%;
				margin: 20upx 5%;
				padding: 30upx 0;
			}
		}
	}
}
</style>
