<template>
	<view class="notice_main" :style="{height: screenHeight + 'px'}">
		<z-paging ref="paging" v-model="listData" @query="queryList" style="padding-top: 100rpx;">
			<template slot="top">
				<nav-bar title='维修服务订单'></nav-bar>
				
				<view class="search_nav">
					<van-form label-width="70">
						<view style="display: flex;">
							<van-field clearable style="flex: 1;" :border="false" readonly clickable name="datetimePicker" :value="formData.runTime" label="开始时间" placeholder="选择开始时间" @click="selectTime('runTime')"/>
							<van-field clearable style="flex: 1;" :border="false" readonly clickable name="datetimePicker" :value="formData.stopTime" label="结束时间" placeholder="选择结束时间" @click="selectTime('stopTime')"/>
						</view>
						
						
						<van-popup v-model="showPicker" position="bottom">
						  <van-datetime-picker v-model="currentDate" :min-date="minDate" :max-date="maxDate" type="datetime" @confirm="onConfirm" :formatter="formatter"  @cancel="showPicker = false"/>
						</van-popup>
						
						<view style="display: flex;align-items: center;">
							<van-field style="flex: 1;" maxlength="5" clearable :border="false" v-model="formData.name" label="用户名" placeholder="用户名"/>
							<view style="flex: 1;display: flex;justify-content: space-between;">
								<van-button style="width: 150rpx;" type="info" @click="searchClick()">搜索</van-button>
								<van-button style="width: 150rpx;margin-right: 24rpx;" type="info" @click="resetClick()">重置</van-button>
							</view>
						</view>
					</van-form>
				</view>
			</template>
			
			<view class="card_item" v-for="(item,index) in listData" :key="index">
				<view class="card_item_tips">
					<view class="tips_name">申请用户</view>
					<view class="tips_con">{{item.name}}</view>
				</view>
				<view class="card_item_tips">
					<view class="tips_name">订购品种</view>
					<view class="tips_con">{{item.serviceName}}</view>
				</view>
				<view class="card_item_tips">
					<view class="tips_name">订购价格</view>
					<view class="tips_con">{{item.usePrice}}元</view>
				</view>
				<view class="card_item_tips">
					<view class="tips_name">订单编号</view>
					<view class="tips_con">{{item.ordno}}</view>
				</view>
				<view class="card_item_tips">
					<view class="tips_name">订单时间</view>
					<view class="tips_con">{{item.orderTime}}</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	import {greenorderList} from "@/common/api.js"
	import {formatDate} from "@/common/util.js"
	import navBar from "@/components/navBar/index.vue";
	import {
		callbackRequest
	} from '@/common/util.js';
	import {
		noticeList
	} from '@/common/api.js';
	export default {
		components: {
			navBar
		},
		data() {
			return {
				listData: [],
				formData: {
					name: '',
					timeType: '',
					runTime: '',
					stopTime: ''
				},
				showPicker: false,
				screenHeight: '',
				minDate: new Date(2024, 0, 1),
				maxDate: '',
				currentDate: new Date(),
			}
		},
		onLoad() {
			let oneYearInMs = 365 * 24 * 60 * 60 * 1000;
			this.maxDate = new Date(new Date().getTime() + oneYearInMs)
			this.screenHeight = uni.getSystemInfoSync().safeArea.height
		},
		methods: {
			queryList(pageNo, pageSize) {
				let params = {
					method: greenorderList,
					currentPage: pageNo,
					isDeleted: "0",
					limit: pageSize,
					timeType: this.formData.timeType,
					name: this.formData.name,
					beginTime: this.formData.runTime,
					endTime: this.formData.stopTime,
					userIdShow: true
				}
				callbackRequest(params).then((res) => {
					if (res.returnCode == 1) {
						res.returnData.records.forEach(item => {
							item.serviceName = ''
							if(item.greenOrderInfoList && item.greenOrderInfoList.length > 0) {
								item.greenOrderInfoList.forEach(chid => {
									item.serviceName += chid.greenName + ','
								})
							}
							item.serviceName = item.serviceName.slice(0, -1)
						})
						if(pageNo * pageSize >= res.returnData.total) {
							this.$refs.paging.completeByNoMore(res.returnData.records,true);
							return
						}else{
							this.$refs.paging.completeByNoMore(res.returnData.records,false);
						}
					} else {
						uni.showToast({title: res.returnMsg,icon: 'none'});
					}
				})
			},
			
			selectTime(type) {
				this.type = type
				this.showPicker = true
			},
			
			onConfirm(e) {
				console.log(this.type,formatDate(e,'m_Time'))
				let oneYearInMs = 365 * 24 * 60 * 60 * 1000;
				if(this.type == 'runTime') {
					if(this.formData.stopTime) {
						if(new Date(`${formatDate(e,'m_Time')}:00`).getTime() > new Date(this.formData.stopTime).getTime()) {
							uni.showToast({title: '开始时间不能大于结束时间',icon: 'none'});
							return
						}
						if( (new Date(this.formData.stopTime).getTime() - new Date(`${formatDate(e,'m_Time')}:00`).getTime()) > oneYearInMs) {
							uni.showToast({title: '请时间间隔小于一年',icon: 'none'});
							return
						}
					}
					this.formData.runTime = `${formatDate(e,'m_Time')}:00`
				}
				if(this.type == 'stopTime') {
					if(this.formData.runTime) {
						if(new Date(`${formatDate(e,'m_Time')}:59`).getTime() < new Date(this.formData.runTime).getTime()) {
							uni.showToast({title: '结束时间不能小于开始时间',icon: 'none'});
							return
						}
						if( (new Date(`${formatDate(e,'m_Time')}:59`).getTime() - new Date(this.formData.runTime).getTime()) > oneYearInMs) {
							uni.showToast({title: '请时间间隔小于一年',icon: 'none'});
							return
						}
					}
					this.formData.stopTime = `${formatDate(e,'m_Time')}:59`
				}
				this.showPicker = false
			},
			
			searchClick() {
				if(this.formData.runTime) {
					if(!this.formData.stopTime) {
						uni.showToast({title: '请选择完整时间段',icon: 'none'});
						return
					}
				}
				
				if(this.formData.stopTime) {
					if(!this.formData.runTime) {
						uni.showToast({title: '请选择完整时间段',icon: 'none'});
						return
					}
				}
				
				if(this.formData.runTime && this.formData.stopTime) {
					this.formData.timeType = '0'
				}
				
				this.$nextTick(() => {
					this.$refs.paging.reload()
				})
			},
			
			resetClick() {
				this.formData.name = ''
				this.formData.runTime = ''
				this.formData.stopTime = ''
				this.formData.timeType = ''
				this.$nextTick(() => {
					this.$refs.paging.reload()
				})
			},
			
			formatter(type, val) {
				if (type === 'year') {
					return val + '年';
				}
				if (type === 'month') {
					return val + '月';
				}
				if (type === 'day') {
					return val + '日';
				}
				if (type === 'hour') {
					return val + '时';
				}
				if (type === 'minute') {
					return val + '分';
				}
				return val;
			}
		}
	}
</script>

<style scoped lang="scss">
	.notice_main {
		width: 100vw;
		background-color: #F2F6FA;
	}
	.search_nav {
		margin-top: 24rpx;
		padding: 10rpx 0rpx;
		box-sizing: border-box;
		background-color: #FFFFFF;
		.van-cell {
			padding: 16px 10px !important;
		}
	}
	.notice_item {
		width: calc(100% - 48rpx);
		background: #FFFFFF;
		border-radius: 18rpx;
		margin-left: 50%;
		transform: translateX(-50%);
		margin-top: 24rpx;
		padding: 26rpx 24rpx;
		box-sizing: border-box;
		.title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #0E1A24;
			text-align: left;
			margin-bottom: 24rpx;
		}
		.tips {
			display: flex;
			align-items: center;
			margin-top: 12rpx;
			image {
				width: 30rpx;
				height: 30rpx;
				display: block;
				margin-right: 12rpx;
			}
			.left {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #4B5B68;
				margin-right: 24rpx;
			}
			.right {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #0E1A24;
			}
		}
	}
	.card_item {
		margin-top: 24rpx;
		width: calc(100% - 48rpx);
		min-height: 200rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 24rpx;
		box-sizing: border-box;
		margin-bottom: 24rpx;
		margin-left: 24rpx;
		.card_item_top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 28rpx;
			.top_left {
				display: flex;
				align-items: center;
				image {
					width: 48rpx;
					height: 48rpx;
					display: block;
					margin-right: 16rpx;
				}
				font-weight: bold;
				font-size: 36rpx;
				color: #0E1A24;
			}
			.top_right {
				width: 152rpx;
				height: 56rpx;
				background: #E0F6F5;
				border-radius: 30rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #00ADA6;
				text-align: center;
				line-height: 56rpx;
			}
		}
		.card_item_tips {
			display: flex;
			margin-bottom: 10rpx;
			.tips_name {
				width: 180rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #97A5B1;
			}
			.tips_con {
				flex: 1;
				font-weight: 400;
				font-size: 28rpx;
				color: #4B5B68;
			}
		}
	}
</style>