<template>
	<view class="content">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}
				<template v-if="item.ordertype">
					<template v-if="shopOrderDetail[item.ordertype]>0">
					(<text v-if="shopOrderDetail[item.ordertype]>99">99+</text>
					<text v-else>{{shopOrderDetail[item.ordertype]}}</text>)
					</template>
				</template>
			</view>
		</view>

		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content padding-t-30" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>

					<!-- 订单列表 -->
					<view @click="goToOrderDetail(item)" v-for="(item, index) in tabItem.orderList" :key="index"
						class="order-item position-relative">
						<view class="i-top b-b flex">
							<view class="order-code flex">
								<view class="no">
									订单编号:
								</view>
								<view>
									{{ item.order_id }}
								</view>
							</view>
							<text class="font-size-sm"
								:style="{ color: item.stateTipColor }">{{ item.status_name }}</text>
						</view>

						<scroll-view v-if="item._info.length > 0" class="goods-box" scroll-x>
							<view v-for="(goodsItem, goodsIndex) in item._info" :key="goodsIndex" class="goods-item">
								<image class="goods-img" :src="goodsItem.cart_info.productInfo.image" mode="aspectFill">
								</image>
								<view class="good-name clamp">
									{{goodsItem.cart_info.productInfo.suk}}*{{goodsItem.cart_info.cart_num}}
								</view>
							</view>
						</scroll-view>
						<view class="info-info">
							<view class="js">
								共{{item._info.length}}件
							</view>
							<view class="hj">
								合计:￥{{item.pay_price}}
							</view>
						</view>
						<!-- 客户信息 -->
						<view class="kh-info">
							<view class="">
								客户信息：<text>{{item.user_address_info.real_name}} {{item.user_address_info.phone}}</text>
							</view>
							<view class="">
								配送地址：<text>{{item.user_address_info.locate_address}}</text>
							</view>
						</view>
						<!-- 底部操作栏 -->
						<view class="btm-btn-wrap flex">
							<view class="btm-left">
								下单时间：{{item.add_time.split(' ')[0].replace(/-/g,'/')}}
							</view>
							<view class="btm-right flex">
								<view class="btm-btn" @click.stop="toCall(item.user_address_info.phone)">
									联系客户
								</view>
								<view class="btm-btn ksps" @click.stop="actionItem = item"
									v-if=" item.status_name=='未发货' &&shopOrderDetail.is_main_store">
									<picker mode="selector" :range="kflist" range-key="staff_name" @change="changeKf">
										<view>开始配送</view>
									</picker>
								</view>
								<view class="btm-btn ksps" v-if=" item.status_name=='待收货'"  @click.stop="shopNext(item)">
									导航
								</view>
								<view class="btm-btn ksps" v-if=" item.status_name=='未支付'&&shopOrderDetail.is_main_store"  @click.stop="payMoney(item)">
									确认支付
								</view>
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import {
		adminkf,
		GetAdminOrderList,
		adminStatistics,
		deliveryKeep,
		offlinePay
	} from '@/api/water.js';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				kflist: [], //客服列表
				actionKf: {
					staff_name: '',
					id: ''
				}, //选中的客服
				actionItem: {}, //保存当前配送对象
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '待付款',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						ordertype: 'unpaid_count'
					},
					{
						state: 1,
						text: '待发货',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						ordertype: 'unshipped_count'
					},
					{
						state: 2,
						text: '待收货',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						ordertype: 'received_count'
					},
					{
						state: 3,
						text: '待评价',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
					},
					{
						state: 4,
						text: '已完成',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
					}
				]
			};
		},
		computed: {
			...mapState('shop', ['shopOrderDetail'])
		},

		onLoad(options) {
			/**
			 * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
			 * 替换onLoad下代码即可
			 */
			this.tabCurrentIndex = +options.state;
			// #ifndef MP
			this.loadData();
			// #endif
			// #ifdef MP
			if (options.state == 0) {
				this.loadData();
			}
			// #endif
			// 获取客服
			this.getadminkf();
			if (!this.shopOrderDetail.unpaid_count) {
				this.adminStatistics()
			}
		},
		// #ifdef APP-PLUS || H5
		onBackPress(e) {
			uni.switchTab({
				url: '/pages/home/user',
			});
			return true;
		},
		// #endif
		methods: {
			...mapMutations('shop', ['setShopOrder']),
			// 重新加载数据
			reload(){
				this.navList[this.tabCurrentIndex].orderList=[]
				this.navList[this.tabCurrentIndex].page=1
				this.navList[this.tabCurrentIndex].loadingType='more'
				this.navList[this.tabCurrentIndex].loaded=false
				this.changeKf()
				this.loadData()
			},
			// 确认支付
			payMoney(item){
				const that = this;
				uni.showModal({
					title: '支付',
					content: '是否确认用户已支付？',
					cancelText: '关闭',
					confirmText: '确认',
					success: res => {
						uni.showLoading({
							title: '支付中',
							mask: true
						});
						if(res.confirm){
							offlinePay({
								order_id:item.order_id
							}).then((e) => {
								uni.hideLoading()
								uni.showToast({
									title:'支付成功'
								})
								that.reload();
							}).catch((e) => {
								uni.hideLoading()
								console.log(e, 'sj');
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
				
			},
			// 获取订单数量信息
			adminStatistics() {
				adminStatistics({}).then((e) => {
					this.setShopOrder(e.data)
				}).catch((e) => {
					console.log(e, 'sj');
				})
			},
			// 客服变更
			changeKf(item) {
				const that = this;
				that.actionKf = that.kflist[item.detail.value]
				uni.showModal({
					title: '送货',
					content: `是否让员工${that.actionKf.staff_name}(${that.actionKf.id})送货？`,
					success: res => {
						if (res.confirm) {
							deliveryKeep({
								delivery_kf: that.actionKf.id,
								delivery_type: 'send',
								order_id: that.actionItem.order_id,
								delivery_name: that.actionKf.staff_name,
								delivery_id: that.actionKf.phone
							}).then((res) => {
								uni.showToast({
									title: res.msg
								});
								that.adminStatistics();
								that.navList[that.tabCurrentIndex].page = 1;
								that.navList[that.tabCurrentIndex].orderList = [];
								that.navList[that.tabCurrentIndex].loadingType = 'more';
								that.loadData();
							}).catch((res) => {

							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 获取客服列表
			getadminkf() {
				adminkf({}).then((res) => {
					this.kflist = res.data;
					console.log(res, 'kf');
				}).catch((res) => {
					console.log(res);
				})
			},
			// 前往客户
			shopNext(item) {
				// 打开地图导航
				uni.openLocation({
					latitude: +item.user_address_info.latitude,
					longitude: +item.user_address_info.longitude,
					fail(e) {
						console.log(e);
					}
				})
			},
			// 联系用户
			toCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			//跳转到订单详情
			goToOrderDetail(e) {
				uni.navigateTo({
					url: '/pages/order/orderDetail?type=2&id=' + e.order_id
				});
			},
			//获取订单列表
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				GetAdminOrderList({
						status: state,
						page: navItem.page,
						limit: navItem.limit,
						shopping_type: 1 //1-送货，2自提
					})
					.then(({
						data
					}) => {
						let arr = data.map(e => {
							let b = this.orderStateExp(e.status);
							e.stateTipColor = b.stateTipColor;
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);
						// console.log(navItem.orderList);
						navItem.page++;
						if (navItem.limit == data.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			//订单状态文字和颜色
			orderStateExp(state) {
				let stateTipColor = '#3C82E6';
				switch (+state) {
					case 0:
						//'待付款';
						stateTipColor = '#3C82E6'
						break;
					case 1:
						//'待发货';
						stateTipColor = '#3C82E6'
						break;
					case 2:
						//'待收货';
						break;
					case 3:
						//'待评价';
						break;
					case 4:
						//'已完成';
						stateTipColor = '#999999';
						break;
					case 9:
						//'订单已关闭';
						stateTipColor = '#909399';
						break;

						//更多自定义
				}
				return {
					stateTipColor
				};
			}
		}
	};
</script>

<style lang="scss">
	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.swiper-box {
		height: calc(100% - 40px);
	}

	.list-scroll-content {
		height: 100%;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;

			&.current {
				color: $base-color;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}

	.order-item {
		padding-left: 30rpx;
		padding-right: 30rpx;
		margin: 0 30rpx;
		margin-bottom: 30rpx;
		border-radius: 30rpx;
		background: #fff;
		color:$font-color-light;

		.i-top {
			height: 80rpx;

			.order-code {
				font-size: $font-sm;

				.no {
					padding: 10rpx 10rpx;
					background-color: #FFEAE5;
					color: #FD5B23;
					line-height: 1;
					border-top-right-radius: 10rpx;
					border-bottom-left-radius: 10rpx;
					margin-right: 10rpx;

				}
			}

			.del-btn {
				padding: 10rpx 0 10rpx 36rpx;
				font-size: $font-lg;
				position: relative;

				&:after {
					content: '';
					width: 0;
					height: 30rpx;
					border-left: 1px solid $border-color-dark;
					position: absolute;
					left: 20rpx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}

		.info-info {
			width: 215rpx;
			height: 200rpx;
			position: absolute;
			top: 80rpx;
			right: 35rpx;
			display: flex;
			flex-direction: column;
			justify-content: flex-end;
			align-items: flex-end;
			font-size: 30rpx;
			font-weight: 500;

			.hj {
				padding-top: 40rpx;
				color: $font-color-dark;
				padding-bottom: 20rpx;
			}

			.js {
				color:$font-color-light;
			}
		}

		.kh-info {
			font-size: 24rpx;
			font-weight: 500;
			color:$font-color-light;
			padding-top: 20rpx;
			border-top: 1px solid $border-color-base;

			view {
				padding: 5rpx 0;
			}

			text {
				font-weight: bold;
			}

		}

		/* 多条商品 */
		.goods-box {
			height: 200rpx;
			padding: 20rpx 0;
			white-space: nowrap;
			padding-right: 250rpx;

			.goods-item {
				width: 120rpx;
				height: 180rpx;
				display: inline-block;
				margin-right: 24rpx;
			}

			.goods-img {
				display: block;
				width: 120rpx;
				height: 120rpx;
			}

			.good-name {
				font-size: 24rpx;
				text-align: center;
				width: 120rpx;
				padding-top: 10rpx;
				margin-right: 0;
				color: $font-color-dark;
			}
		}

		.btm-btn-wrap {
			height: 90rpx;
			width: 100%;
			font-size: 24rpx;
			font-weight: 500;
			color:$font-color-light;
			line-height: 1;

			.btm-right {
				justify-content: flex-end;
			}

			.btm-btn {
				width: 144rpx;
				padding: 10rpx 0;
				border: 2px solid $border-color-base;
				border-radius: 28rpx;
				font-size: 26rpx;
				font-weight: 500;
				text-align: center;
				margin-left: 10rpx;
				color: $font-color-light;
			}

			.ksps {
				border-color:$base-color;
				color: $base-color;
			}
		}
	}

	.buttomBox {
		border-radius: 20rpx;
		margin: 0 $page-row-spacing;
		margin-bottom: 30rpx;
		color: $font-color-dark;
		background-color: #FFFFFF;
		overflow: hidden;

		.send {
			text-align: center;
		}

		.item {
			line-height: 100rpx;
			height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			color: $base-color;

			&.qx {
				color: $color-red;
			}
		}

		.border_b {
			border-bottom: 1px solid $page-color-light;
		}
	}
</style>
