<template>
	<view class="myShopTour-wrap">
		<!-- 自定义标题 -->
		<u-navbar :is-back="false" :border-bottom="false" :background="background" id="navbar">
			<view class="slot-wrap">
				<view class="back-icon" @click="goBack">
					<u-icon name="arrow-left" color="#fff" size="32"></u-icon>
				</view>
				<view class="left-icon">我的巡店</view>
				<view class="right-icon">
					<u-icon name="more-dot-fill" color="#fff" size="28" @click="openScreen=true"></u-icon>
				</view>
			</view>
		</u-navbar>
		<!-- 内容 -->
		<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">
							<view v-show="searchForm.creatTime.length != 0" class="alert-tips">{{searchForm.creatTime[0]}} ~ {{searchForm.creatTime[1]}}</view>
							<view class="record-item" v-for="(item,index) in list" :key="index">
								<view class="item-head">
									<text class="item-c-tit">{{item.name}}</text>
									<u-tag class="item-c-type" :text="item.orderStatus" type="primary" />
								</view>
								<view class="item-main">
									<text class="item-tit">创建时间：</text>
									<text class="item-val">{{item.creatTime}}</text>
								</view>
								<view class="item-main">
									<text class="item-tit">完成时间：</text>
									<text class="item-val">{{item.completionTime}}</text>
								</view>
								<view class="item-main">
									<text class="item-tit">巡店耗时：</text>
									<text class="item-val">{{item.timeConsuming}}</text>
								</view>
								<u-grid class="item-grid" :col="4" hover-class="none">
									<u-grid-item>
										<text class="grid-num text-blue">18</text>
										<text class="grid-text">检查项</text>
									</u-grid-item>
									<u-grid-item>
										<text class="grid-num text-green">13</text>
										<text class="grid-text">合格</text>
									</u-grid-item>
									<u-grid-item>
										<text class="grid-num text-red">3</text>
										<text class="grid-text">不合格</text>
									</u-grid-item>
									<u-grid-item>
										<text class="grid-num text-yellow">2</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="60%" class="screenModel-wrap">
			<!-- 条件项 -->
			<view class="condition-con">
				<view class="condition-item">
					<text class="condition-tit">创建时间</text>
					<view class="condition-val">
						<text @click="onShowDatePicker('range')">{{range[0] ? range[0] : '请选择'}} ~ {{range[1] ? range[1] : '请选择'}}</text>
						<u-icon v-show="range.length != 0" name="close-circle" color="#999" size="28" class="close-circle" @click="range=[]"></u-icon>
					</view>
				</view>
				<view class="condition-item">
					<text class="condition-tit">门店</text>
					<view class="condition-val">
						<text @click="openStore=!openStore">{{storeName ? storeName : '全部'}}</text>
						<u-icon v-show="storeName" name="close-circle" color="#999" size="28" class="close-circle" @click="clearStore"></u-icon>
					</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>
		<!-- 选择创建时间 -->
		<mx-date-picker class="choose-date-wrap" :show="showPicker" :type="type" :value="searchForm.creatTime" :show-tips="true" :begin-text="'开始'" :end-text="'结束'" :show-seconds="true" @confirm="onSelected" @cancel="onSelected" />
		<!-- 选择门店 -->
		<u-select v-model="openStore" :default-value="defaultStoreValue" :list="storeList" @confirm="confirmStore" class="choose-store-wrap"></u-select>
	</view>
</template>

<script>
	import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue"
	export default {
		components: { MxDatePicker },
		data() {
			return {
				background: {
					// 渐变色
					backgroundImage: 'linear-gradient(45deg, rgb(85, 170, 255), rgb(21, 102, 223))'
				},
				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',  //  加载状态
				status: 'loadmore',  //  加载状态
				noDataText: '暂无数据',  //  列表数据为空时提示文字
				list: [  //  数据列表
					{ name: '常青二店', creatTime: '2020-09-08 09:16', orderStatus: 'haveInHand', completionTime: '2020-09-08 09:16', timeConsuming: '5小时18分钟', inspectionItems: 18, qualified: 14, unqualified: 2, notApplicable: 2 },
					// { name: '常青二店', creatTime: '2020-09-08 09:16', orderStatus: 'invalid', completionTime: '2020-09-08 09:16', timeConsuming: '5小时18分钟', inspectionItems: 18, qualified: 14, unqualified: 2, notApplicable: 2 },
					// { name: '常青二店', creatTime: '2020-09-08 09:16', orderStatus: 'haveInHand', completionTime: '2020-09-08 09:16', timeConsuming: '5小时18分钟', inspectionItems: 18, qualified: 14, unqualified: 2, notApplicable: 2 },
					// { name: '常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店常青二店', creatTime: '2020-09-08 09:16', orderStatus: 'completed', completionTime: '2020-09-08 09:16', timeConsuming: '5小时18分钟', inspectionItems: 18, qualified: 14, unqualified: 2, notApplicable: 2 },
					{ name: '常青二店', creatTime: '2020-09-08 09:16', orderStatus: 'completed', completionTime: '2020-09-08 09:16', timeConsuming: '5小时18分钟', inspectionItems: 18, qualified: 14, unqualified: 2, notApplicable: 2 },
				],
				openScreen: false,  //  是否打开筛选
				dateMode: 'range',  //  date单个日期  range日期范围
				searchForm: {  //  查询条件
					creatTime: [],  //  创建时间默认筛选近7天
					store: ''
				},
				openStore: false,  // 选择门店
				storeList: [
					{ value: '1', label: '江' },
					{ value: '2', label: '河' }
				],
				showPicker: false,  //  是否选择创建时间
				range: [],  //  筛选创建时间的默认值
				type: 'range',  //  日期时间类型
				storeName: '',  //  筛选门店名称
				defaultStoreValue: [],  //  门店默认选中下标
			}
		},
		onLoad() {
			this.pageInit()
		},
		methods: {
			//  返回
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			pageInit(){
				this.range = [ this.get7day(-6), this.get7day(0) ]
				this.searchForm.creatTime = [ this.get7day(-6), this.get7day(0) ]
				this.total = 0
				this.pageNo = 1
			},
			// tabs通知swiper切换
			tabsChange(index) {
				this.swiperCurrent = index
				this.list = []
				this.status = "loading"
			},
			swiperChange(event){
				this.list = []
				this.status = "loading"
			},
			// 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
				if(current != this.current){
					this.$refs.uTabs.setFinishCurrent(current)
					this.swiperCurrent = current
					this.current = current
					this.pageInit()
				}
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				if(this.list.length < this.total){
					this.pageNo += 1
					this.pageInit()
				}else{
					this.status = "nomore"
				}
			},
			//  显示日期选择
			onShowDatePicker(type){
				this.type = type
				this.showPicker = true
				this.searchForm.creatTime = this[type]
			},
			//  日期选择
			onSelected(e) {
				this.showPicker = false
				if(e) {
					this[this.type] = e.value
				}
			},
			//  获取近几天的数据
			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
			},
			//  选择门店
			confirmStore(e){
				this.storeName = e[0].label
				this.defaultStoreValue = [this.storeList.findIndex(item => item.value == e[0].value)]
			},
			//  情空  筛选门店
			clearStore(){
				this.storeName = ''
				this.defaultStoreValue = []
			},
			//  重置筛选条件
			resetF(){
				this.range = this.searchForm.creatTime
				let ind = [this.storeList.findIndex(item => item.value == this.searchForm.store)]
				if(ind == -1){
					this.defaultStoreValue = []
					this.storeName = ''
				}else{
					this.defaultStoreValue = [ind]
					this.storeName = this.storeList[ind].label
				}
			},
			//  确定筛选
			submitF(){
				this.searchForm.creatTime = this.range
				if(this.storeName){
					this.searchForm.store = this.storeList[this.defaultStoreValue[0]].value
				}else{
					this.searchForm.store = ''
				}
				this.openScreen = false
			},
		}
	}
</script>

<style lang="scss" scoped>
page {
	background-color: #f8f8f8;
	height: calc(100vh - 44px);
}
.myShopTour-wrap {
	height: 100%;
	/* 自定义导航栏样式 */
	.slot-wrap {
		display: flex;
		align-items: center;
		/* 如果您想让slot内容占满整个导航栏的宽度 */
		flex: 1;
		/* 如果您想让slot内容与导航栏左右有空隙 */
		padding: 0 30rpx;
		color: #fff;
		font-size: 28upx;
		.left-icon {
			flex-grow: 1;
			font-size: 32upx;
			margin-right: 10upx;
			text-align: center;
		}
		.right-icon {
			padding-left: 50upx;
		}
		.uni-icons {
			margin-right: 10upx;
		}
	}
	// 内容
	.myShopTour-con{
		.data-list{
			height: calc(100vh - 84px);
			.swiper-item{
				.scroll-con{
					height: 100%;
					// 列表
					.record-con{
						padding-top: 20upx;
						.alert-tips{
							font-size: 24upx;
							padding: 4upx 10upx;
							margin: 0upx 30upx 10upx;
							border: 1upx solid #fcbd71;
							background-color: #fdf6ec;
							border-radius: 6upx;
						}
						.record-item{
							background-color: #fff;
							margin: 0upx 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: 10upx;
								}
								.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;
			.condition-item{
				.condition-tit{
					display: block;
					font-size: 26upx;
					margin: 20upx 0;
				}
				.condition-val{
					display: block;
					font-size: 24upx;
					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;
					}
				}
			}
		}
		.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;
			}
		}
	}
	.choose-date-wrap{
		z-index: 999999;
	}
}
</style>
