<template>
	<view v-if="isLogin" class="content">
		<view>
			<u-tabs-swiper ref="uTabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange" :is-scroll="false"
				active-color="#FC6035" inactive-color="#111111" fontSize="28" :active-item-style="active"
				swiperWidth="750"></u-tabs-swiper>
		</view>
		<swiper :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish"
			style="height: calc(100vh - 180rpx)">
			<swiper-item class="swiper-item" v-for="(item, index) in tabsList" :key="index">
				<scroll-view scroll-y style="height: calc(100vh - 180rpx);width: 100%;" @scrolltolower="onreachBottom">
					<view v-for="(data,index1) in item.childList" :key='index1'
						style="margin:15rpx 20rpx;background-color: #FFFFFF;border-radius: 20rpx;padding-bottom: 28rpx;">
						<!-- 主题描述 -->
						<view @click="gotoDetails(index1)">
							<view class="flex align-center justify-between"
								style="margin-left: 30rpx;margin-right: 29rpx; padding-top: 37rpx;">
								<view class="flex align-center" style="flex: 2;">
									<view>
										<text
											style="font-size: 28rpx;font-weight: bold;color: #333333;">{{data.goodsName}}</text>
									</view>
									<view>
										<image src="../../static/arrows_right.png" style="width: 24rpx;height: 24rpx;">
										</image>
									</view>

								</view>
								<view style="flex: 1;" class="flex justify-between">
									<view></view>

									<text v-if="data.orderState=='006'"
										style="font-size: 30rpx;font-weight: bold;color: #C0C0C0;">{{data.orderStateName}}</text>
									<text v-else-if="data.orderState== '004'"
										style="font-size: 30rpx;font-weight: bold;color: #C0C0C0;">{{data.orderStateName}}</text>
									<text v-else-if="data.orderState== '005'"
										style="font-size: 30rpx;font-weight: bold;color: #FC6035;">{{data.orderStateName}}</text>
									<text v-else-if="data.orderState=='002'"
										style="font-size: 30rpx;font-weight: bold;color: #3B94E7;">{{data.orderStateName}}</text>
									<text v-else-if="data.orderState==='003'"
										style="font-size: 30rpx;font-weight: bold;color: #FCAF35;">{{data.orderStateName}}</text>
									<text v-else-if="data.orderState==='001'"
										style="font-size: 30rpx;font-weight: bold;color: #FCAF35;">{{data.orderStateName}}</text>
									<view v-else-if="data.orderState=='000'">
										<view style="font-size: 30rpx;font-weight: bold;color: #FC6035;">
											{{data.orderStateName}}
										</view>
										<view style="font-size: 24rpx;font-weight: 500;color: #111;float: right;">29:55
										</view>


									</view>
								</view>
							</view>
							<!-- 服务人员描述 -->
							<view
								style="margin-top: 31rpx;margin-right: 15rpx;border-radius: 10rpx; border-radius: 10rpx;background-color: #F6F6F6;">
								<view class="flex align-start"
									style="padding-top: 20rpx;margin-left: 20rpx;margin-right: 20rpx;padding-bottom: 20rpx;">
									<view>
										<image mode="aspectFill" :src="data.serverAvatar"
											style="width: 160rpx;height: 128rpx;border-radius: 10rpx;"></image>
									</view>
									<view style="margin-left: 18rpx;flex: 1;">
										<view class="flex align-center justify-between">
											<view class="fixed align-center">
												<text
													style="font-size: 24rpx;font-weight: bold;color: #111111;">{{data.serverName}}</text>
												<text
													style="font-size: 24rpx;font-weight: 500;color: #D09A6C;margin-left: 21rpx;">{{data.erverLevelName}}</text>
											</view>
											<view
												style="border: 1rpx solid #FF9999;padding: 8rpx 14rpx 8rpx 14rpx;font-size: 22rpx;font-weight: 500;color: #FF1414;border-radius: 8rpx;">
												好评率{{data.rate *100}}%</view>

										</view>
										<text
											style=" margin-top: 18rpx;background: linear-gradient(269deg, #E7C2A1 0%, #E7C2A1 0%, #F4EBE1 100%, #F4EBE1 100%);padding: 7rpx 11rpx;font-size: 22rpx;font-weight: 500;color: #78341C;border-radius: 8rpx;">
											{{data.serverAge}}年工龄
										</text>
										<view style="margin-top: 18rpx;">
											<text
												style="font-size: 22rpx;font-weight: 500;color: #999999;">{{data.merchantName}}</text>
										</view>
									</view>
								</view>
							</view>
							<!-- 订单内容描述 -->
							<view class="flex align-center justify-between" style="margin: 17rpx 30rpx 0rpx 0rpx;">
								<text
									style="padding:  20rpx;color: #999999;font-size: 28rpx;font-weight: 500;">上门时间</text>
								<text style="font-size: 28rpx;font-weight: 500;color: #111;">{{

							  data.serviceStartTime
							}}</text>
							</view>
							<view class="flex align-center justify-between" style="margin: 17rpx 30rpx 0rpx 0rpx;">
								<view style="padding:  20rpx;color: #999999;font-size: 28rpx;font-weight: 500;">服务地址
								</view>
								<view
									style="font-size: 28rpx;font-weight: 500;color: #111; margin-left: 20rpx;flex: 1;text-align: right;">
									{{data.address + data.detailed}}</view>
							</view>
							<view class="flex align-center justify-between" style="margin: 17rpx 30rpx 0rpx 0rpx;">
								<text
									style="padding:  20rpx;color: #999999;font-size: 28rpx;font-weight: 500;">服务单价</text>
								<view>
									<text
										style="font-size: 28rpx;font-weight: 500;color: #111;">{{data.goodsServerPrice}}元/小时</text>
									<text
										style="font-size: 28rpx;font-weight: 500;color: #999;margin-left: 10rpx;">x{{data.goodsCounts}}</text>
								</view>
							</view>
							<view class="flex align-center justify-between" style="margin: 17rpx 30rpx 0rpx 30rpx;">
								<view>
									<text style="font-size: 28rpx;font-weight: bold;color: #111;">合计：</text>
									<text style="color: #FC6035;font-size: 29rpx;font-weight: 500;">¥</text>
									<text
										style="font-size: 30rpx;font-weight: bold;color: #FC6035;margin-left: 10rpx;">{{data.totalAmount}}</text>
									<text
										style="font-size: 28rpx;font-weight: 500;color: #CCCCCC;margin-left: 10rpx;text-decoration: line-through;">¥{{data.orderAmount}}</text>
								</view>
							</view>
						</view>
						<!-- 状态判断描述 -->

						<view v-if="data.status === 1"></view>
						<view v-else-if="data.orderState === '004'">
							<view class="flex align-center justify-between"
								style="background: linear-gradient(270deg, #F9FAFB 0%, #F9FAFB 0%, #FDFEFF 100%, #FDFEFF 100%);margin-top: 20rpx;padding: 29rpx 27rpx;">
								<view>
									<text style="font-size: 26rpx;font-weight: 500;color: #999;">追加数量</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #111;margin-left: 10rpx;">1小时</text>
								</view>
								<view>
									<text style="font-size: 28rpx;font-weight: 500;color: #333333;">¥</text>
									<text
										style="font-size: 30rpx;color: #333333;font-weight: bold; margin-left: 10rpx;">305</text>
								</view>
							</view>

							<view class="flex align-center justify-between"
								style="margin-top: 20rpx;padding-left: 29rpx;padding-right: 29rpx;padding-top: 27rpx;padding-bottom: 0rpx;">
								<view>
									<text style="font-size: 26rpx;font-weight: 500;color: #999;">开始服务</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #111;margin-left: 10rpx;">{{manageDateToday(data.serviceStartTime)}}</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #3b94e7;margin-left: 10rpx;">{{manageDateTominus(data.serviceStartTime)}}</text>
								</view>
								<view
									@click="goAppraise(item)"
									style="padding: 16rpx 39rpx; border: 1rpx solid #CCCCCC;font-weight: bold;color: #333333;border-radius: 10rpx;">
									去评价
								</view>
							</view>

						</view>
						<view v-else-if="data.orderState === '002'">
							<view class="flex align-center justify-between"
								style="margin-top: 20rpx;margin: 29rpx;margin: 29rpx;padding-top: 27rpx;padding-bottom: 0rpx;border-top: 1rpx #EEEEEE solid;">
								<view>
									<text style="font-size: 26rpx;font-weight: 500;color: #999;">开始服务</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #111;margin-left: 10rpx;">{{manageDateToday(data.serviceStartTime)}}</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #3b94e7;margin-left: 10rpx;">{{manageDateTominus(data.serviceStartTime)}}</text>
								</view>

								<view
									style="padding: 16rpx 39rpx;background-color: #FC6035;font-weight: bold;color: #FFFFFF ;border-radius: 10rpx;" @click="addOtherServerClick(index1)">
									追加服务
								</view>
							</view>
						</view>
						<view v-else-if="data.orderState === '003'">
							<view class="flex align-center justify-between"
								style="margin-top: 20rpx;margin: 29rpx;margin: 29rpx;padding-top: 27rpx;padding-bottom: 0rpx;border-top: 1rpx #EEEEEE solid;">
								<view>
									<text style="font-size: 26rpx;font-weight: 500;color: #999;">服务时间</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #111;margin-left: 10rpx;">{{manageDateToday(data.serviceStartTime)}}</text>
									<text
										style="font-size: 26rpx;font-weight: 500;color: #3b94e7;margin-left: 10rpx;">{{manageDateTominus(data.serviceStartTime)}}-{{manageDateTominus(data.serviceEndTime)}}</text>
								</view>

								<view @click="sureFinish(index1)" style="padding: 16rpx 39rpx;background-color: #FC6035;font-weight: bold;color: #FFFFFF ;border-radius: 10rpx;">
									确认完成
								</view>
							</view>
						</view>

						<view v-else-if="data.orderState === '001'"></view>
						<view v-else-if="data.orderState === '000'">
							<view class="flex align-center justify-between"
								style="margin-top: 20rpx;margin: 29rpx;margin: 29rpx;padding-top: 27rpx;padding-bottom: 0rpx;border-top: 1rpx #EEEEEE solid;">
								<view></view>
								<view class="flex align-center">
									<view @click="cancelOrderClick(data)" style="padding: 16rpx 39rpx; border: 1rpx solid #CCCCCC;font-weight: bold;color: #333333;border-radius: 10rpx;">
										取消订单
									</view>

									<view @click="gotoPaymentClcik(data)" style="padding: 16rpx 39rpx;background-color: #FC6035;font-weight: bold;color: #FFFFFF ;border-radius: 10rpx;margin-left: 20rpx;">
										立即支付
									</view>
								</view>


							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
		
		<!-- 追加服务 -->
		<u-popup v-model="showAddServer" mode="bottom" border-radius="20" >
			<view style="margin-top: 57rpx;" class="flex align-center justify-between">
				<view style="font-size: 34rpx;color: #111111;font-weight: bold;margin-left: 50rpx;">服务数量</view>
				<view class="flex align-center" style="margin-right: 39rpx;">
					<view @click="minusServer"> 
						<image src="../../static/minus_icon.png" style="width: 50rpx;height: 50rpx;" mode=""></image>
					</view>	
					<view style="font-size: 28rpx;font-weight: bold;color: #111111;margin-left: 30rpx;margin-right: 30rpx;">
						{{addServerNumer}}
					</view>	
					<view @click="addServer">
						<image src="../../static/add_icon.png" style="width: 50rpx;height: 50rpx;" mode=""></image>
					</view>	
					<view style="font-size: 26rpx;font-weight: 500;color: #999999;margin-left: 20rpx;">
						{{currentAddServerUnit}}
					</view>
				</view>
				
			</view>
			<view class="flex align-center justify-between" style="margin-top: 68rpx;">
				<view></view>
				<view style="margin-right: 39rpx;" >
					<text style="font-weight: bold;font-size: 28rpx;color: #111111;">合计：</text>
					
						<text style="font-size: 28rpx;font-weight: 500;color: #FC6035;">¥</text>
					<text style="font-size: 30rpx;font-weight: bold;color: #FC6035;margin-left:8rpx;">{{addServerAllMoney}}</text>	
				
				</view>
			</view>
			
			<view class="flex align-center justify-between" style="margin-top: 160rpx;padding-bottom: 60rpx;">
				<view style="margin-left: 50rpx; font-weight: bold;color: #333333;padding: 25rpx 121rpx;background-color: #EEEEEE;border-radius: 10rpx;">取消</view>
				<view @click="gotoAddServerPayment" style="margin-right: 50rpx; font-weight: bold;color: #FFFFFF;padding: 25rpx 121rpx;background-color: #FC6035;border-radius: 10rpx;">去支付</view>
			</view>		 
		</u-popup>
		
	</view>
	<view v-else class="content">
		<cu-custom :isBack="false" style="border-bottom: 1rpx #EEEEEE solid;">
			<block slot="content">
				订单
			</block>
		</cu-custom>
		<view class="login_button" @click="gotoLogin">登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				isLogin: false,
				addServerNumer:0,
				addServerAllMoney:0,
				showAddServer:false,
				currentAddServerIndex:0,
				currentAddServerUnit:'小时',

				tabsList: [{
					name: "全部",
					state: '',
					childList: []
				}, {
					name: "待确认",
					state: '003,002',
					childList: []
				}, {
					name: "待付款",
					state: '000',
					childList: []
				}, {
					name: "待服务",
					state: '001',
					childList: []
				}, {
					name: "待评价",
					state: '004',
					childList: []
				}, {
					name: "退款",
					state: '005,006',
					childList: []
				}],
				dataSourceList: [{
					status: 0,
				}, {
					status: 1,
				}, {
					status: 2,
				}, {
					status: 3,
				}, {
					status: 4,
				}, {
					status: 5
				}],

				tabsCurrent: 0,
				swiperCurrent: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
				active: {
					fontSize: "34rpx",
					color: "#111111"
				}
			}
		},
		onLoad() {
			if (uni.getStorageSync('userInfo')) {
				this.isLogin = true
				this.getOrderList(0);
			} else {
				this.isLogin = false

			}
		},
		methods: {
			// 跳转评价
			goAppraise(item){
				console.log(item)
				uni.setStorageSync('tempAppraise',item.childList)
				uni.navigateTo({
					url: 'appraise'
				})
			},
			gotoLogin() {
				uni.navigateTo({
					url: '../codelogin/codelogin'
				})
			},
			tabsChange(index) {
				this.swiperCurrent = index
				this.getOrderList(index)
			},
			getOrderList(index) {
				var item = this.tabsList[index];
				var param = {
					// "memberId":uni.getStorageSync('userInfo').memberId,
					"memberId": '10001',
					"orderState": item.state,

				}
				var that = this
				this.$u.api.orderList(param).then(res => {
					console.log(res)
					if (res.code == 200) {
						that.tabsList[index].childList = res.rows
					}
				}).catch(error => {

				})
			},

			// 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;
				this.$refs.uTabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.tabsCurrent = current;
			},
			onreachBottom() {

			},
			//进入详情页面
			gotoDetails(index) {
				var contentObj = this.tabsList[this.tabsCurrent];
				var data = contentObj.childList[index].orderId
				var item = JSON.stringify(data)
				uni.navigateTo({
					url: './orderDetails?orderId=' + item,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			// 追加服务按钮事件
			addOtherServerClick(index){
				this.currentAddServerIndex = index
				var data = this.tabsList[this.swiperCurrent].childList[this.currentAddServerIndex];
				this.currentAddServerUnit = data.goodsUnitName
				this.showAddServer = true
			},
			// 确认完成服务
			sureFinish(index){
				var data = this.tabsList[this.swiperCurrent].childList[index];
				var param = {
					'orderId':data.orderId
				}
				var that = this
				this.$u.api.orderConfirm(param).then(res => {
					console.log(res)
					this.$api.msg(res.msg)
					if (res.code == 200) {
					 this.getOrderList(that.swiperCurrent)	
					}
				}).catch(error => {
				this.$api.msg("请求错误")
				})
			},
			// 取消订单
			cancelOrderClick(data){
				var param = {
					'orderId':data.orderId
				}
				var that = this
				this.$u.api.orderCancel(param).then(res => {
					console.log(res)
					this.$api.msg(res.msg)
					if (res.code == 200) {
					 this.getOrderList(that.swiperCurrent)	
					}
				}).catch(error => {
				this.$api.msg("请求错误")
				})
			},
			// 追加服务去支付
			gotoAddServerPayment(){
				
			},
			gotoPaymentClcik(data){
				// url:'./webView?tittle='+'客服服务'+'&url=http://jzkf.uxianxin.com/s/66424vv38'
				uni.navigateTo({
					url:'../index/payment?orderId='+data.orderId+'&totalAmount='+data.totalAmount
				})
			},
			// 减少追加服务数量
			minusServer(){
			 if(this.addServerNumer <=0){
				 return;
			 }	
			 var data = this.tabsList[this.swiperCurrent].childList[this.currentAddServerIndex];
			 var isTime = true; 
			 data.goodsUnit == "001" ? isTime = true:isTime=false
			
			 if (isTime){
				 this.addServerNumer = this.addServerNumer - 0.5
			 }else {
				 this.addServerNumer = this.addServerNumer - 1
			 }
			 this.addServerAllMoney = this.addServerNumer * data.goodsServerPrice
			},
			// 增加服务数量
			addServer(){
				var isTime = true;
				console.log(this.tabsList[this.swiperCurrent].childList)
				console.log(this.currentAddServerIndex)
				var data = this.tabsList[this.swiperCurrent].childList[this.currentAddServerIndex];
				
	
				data.goodsUnit == "001" ? isTime = true:isTime=false
				
				if (isTime){
					this.addServerNumer = this.addServerNumer + 0.5
				}else {
					this.addServerNumer = this.addServerNumer + 1
				}
				 this.addServerAllMoney = this.addServerNumer * data.goodsServerPrice
			},
			manageDateToday(dateStr) {
				if (dateStr == undefined || dateStr == null) {
					return ''
				}
				var date = dateStr.substring(0, dateStr.length);
				date = date.replace(/-/g, '/');
				console.log(date)
				var timestamp = new Date(date).getTime();
				return this.$u.timeFormat(timestamp, 'yyyy年mm月dd日')

			},

			manageDateTominus(dateStr) {
				if (dateStr == undefined || dateStr == null) {
					return ''
				}
				var date = dateStr.substring(0, dateStr.length);
				date = date.replace(/-/g, '/');
				console.log(date)
				var timestamp = new Date(date).getTime();
				return this.$u.timeFormat(timestamp, 'hh时MM分')
			}

		}

	}
</script>

<style lang="less" scoped>
	.backgound {
		font-size: 30rpx;
		color: #FF9700;
	}

	.login_button {
		margin: 200rpx 300rpx 300rpx;
		text-align: center;
		background-color: #FC6035;
		padding-top: 10rpx;
		padding-bottom: 8rpx;
		border-radius: 10rpx;
		color: #FFFFFF;
		font-weight: 500;
	}
</style>
