<template>
	<view class="hzIndex">
		<hzHeader title="线上超市"></hzHeader>
		<view class="container hzContent" v-if="loading">
			<view class="main">
				<!-- <view class="tab-box">
					<view class="tab-item" @click="currentTabIndex = 1">所有订单</view>
				</view> -->
				<view>
					<view class="totle">
						<view class="num_wrap">
							<view class="flex-text">
								超市订单概览
							</view>
							<view class="flex-num-box">
								<view class="num-box">
									<view class="num">
										<text>{{statisticsDataHistory.num}}</text>
									</view>
									<view class="text">
										<text>订单数量</text>
									</view>
								</view>
								<view class="num-box">
									<view class="num">
										<text>{{statisticsDataHistory.price}}</text>
									</view>
									<view class="text">
										<text>销售金额</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="search-box">
						<view class="form-item searchv">
							<picker class="time-data" mode="date" @change="startDateChange">
								<view class="">{{ startDate }}</view>
							</picker>
							<text class="timecenter">至</text>
							<picker class="time-data" mode="date" @change="endDateChange">
								<view class="">{{ endDate }}</view>
							</picker>
						</view>
						<view class="barber">
							<!-- <view style="font-size: 14px;color:#666">取货地点：</view> -->
							<view class="barber-box">
								<view class="barber-item" v-for="(item, index) in takeFoodPlace"
									:class="takeFoodPlaceIndex == index ? 'barberactive' : ''" :key="index"
									@click="choosePlace(index)">
									{{item}}
								</view>
							</view>
						</view>
					</view>
					<!-- <view class="list" v-if="ordersHistory.length > 0">
						<view class="main" v-for="(item, index1) in ordersHistory" :key="index1">
							<view class="top">
								<view class="dining">{{item.buyer_name+" "}} <text @click="callPhone(item.buyer_phone)"
										style="color: #00C277;">{{item.buyer_phone}}</text></view>
								<view class="status" v-if='item.order_status == 1'>预订成功</view>
								<view class="status " style="color: #666;" v-if='item.order_status == 0'>已取消</view>
								<view class="status " style="color: #666;" v-if='item.order_status == 3'>已备货</view>
							</view>
							<view style="font-size: 28upx;color: #666;padding:10upx 20upx">
								<view style="padding: 6upx 0upx;">
									预定时间：
									<text>{{ item.order_time }}</text>
								</view>
							</view>
							<view class="list-item" v-for="(it, index2) in item.orderDetails" :key="index2">
								<view class="img-box">
									<image :src="formatImgUrl(it.goods_icon)"
										@error="imageError2($event, index2,index1)" mode=""
										@click="vbPicture(new Array(formatImgUrl(it.goods_icon)))"></image>
								</view>
								<view class="fr-content">
									<view class="name">
										{{ it.goods_name }}
									</view>
									<view class="price">
										<view style="float: left;">&yen;{{ it.price }}</view>
										<view style="float: right;color: #666;padding-right: 20upx;">x{{ it.goods_num }}
										</view>
									</view>
								</view>
							</view>
							<view style="border-top: 1px solid #eee;height:100upx">
								<view style="float: left;font-size: 28upx;margin-top: 32upx;margin-left: 20upx;">
									总金额：&yen;{{ item.pay_price }}</view>
								<view class="comment-btn" v-if="item.order_status==1" @click="marketPickUp(item)">取货通知
								</view>
							</view>
						</view>
					</view>
					<content-none v-else :padTop="20"></content-none> -->
					<view class="list_box">
						<van-pull-refresh v-model="refreshing" @refresh="onRefresh">
							<van-empty :description="descriptionVal" v-if="ordersHistory.length==0 "></van-empty>
							<view class="item_card" v-for="(i,index) in ordersHistory" :key="index">
								<view class="card_top">
									<view class="card_top_left">
										<img :src="require('@/static/takeaway/rest_img.png')" alt="">
										<view class="st">{{i.take_food_place}}</view>
										<view class="order_num">
											{{i.buyer_name + ' ' + i.buyer_phone}}
										</view>
									</view>
									<view v-if='i.order_status == 0' class="card_top_right yd">已取消</view>
									<view v-if='i.order_status == 3' class="card_top_right qx">已备货</view>
									<view v-if='i.order_status == 1' class="card_top_right yd">预订成功</view>
								</view>
								<view class="card_con">
									<view class="foodbox">
										<view v-for="(k,dex) in i.orderDetails" class="onefood" :key="dex">
											<image class="food_img" :src="k.goods_icon" alt="">
												<view class="flex_name">
													<view class="food_name">{{k.goods_name}}</view>
													<view class="spec">
														<view class="s-text">&yen; <text
																class="s-t-price">{{ k.price }}</text>/{{ k.unit }}
														</view>
														<view class="s-num">x{{ k.goods_num }}</view>
													</view>
												</view>
										</view>
										<!-- <view v-else class="morefood">
											<view v-for="(k,dex) in i.orderDetails" :key="dex">
												<img class="food_img" :src="k.goods_icon" alt="">
												<view class="food_name">{{k.goods_name}}</view>
											</view>
										</view> -->
									</view>
									<!-- <view class="allNum">共{{i.allNum}}件</view> -->
								</view>
								<view class="card_bon">
									<view>
										{{i.order_time}}
									</view>
									<view>
										<span>总金额：￥ </span><span class="pic">{{i.pay_price}}</span>
									</view>
								</view>
								<view class="flex-btn-box">
									<view class="comment-btn" v-if="i.order_status==1" @click="marketPickUp(item)">取货通知
									</view>
								</view>

							</view>
							<view v-if="pageHistory>1 && ordersHistory.length==0 && isNull"
								style="text-align: center;padding: 5px 0;font-size: 15px;color: #a8a8a8;">
								没有更多了
							</view>
						</van-pull-refresh>
					</view>
				</view>
			</view>

			<load-more :loadingType="loadingTypeHistory" :contentText="contentTextHistory"></load-more>
			<uni-popup ref="popup" type="center" style="white-space: pre-wrap;">
				<view
					style="background: white;width: 600upx;position: relative;border-radius: 2upx;overflow-y: scroll;">
					<view class="box box-between" style="border-bottom: 1px solid #E5E5E5;">
						<view
							style="width: 33%;text-align: center;font-size: 32upx;border-right: 1upx solid #E5E5E5;padding: 20upx 0upx;">
							菜品</view>
						<view
							style="width: 34%;text-align: center;font-size: 32upx;border-right: 1upx solid #E5E5E5;padding: 20upx 0upx;">
							数量</view>
						<view style="width: 33%;text-align: center;font-size: 32upx;padding: 20upx 0upx;">单位</view>
					</view>
					<scroll-view scroll-y="true" style="height: 660upx;">
						<view v-for="(item,f) in allFoodList" :key="f" class="box box-between"
							style="border-bottom: 1px solid #E5E5E5;">
							<view
								style="width: 33%;text-align: center;font-size: 28upx;border-right: 1upx solid #E5E5E5;padding: 14upx 0upx;">
								{{item.food_name}}
							</view>
							<view
								style="width: 34%;text-align: center;font-size: 28upx;border-right: 1upx solid #E5E5E5;padding: 14upx 0upx;">
								{{item.food_num}}
							</view>
							<view style="width: 33%;text-align: center;font-size: 28upx;padding: 10upx 0upx;">
								{{item.unit}}
							</view>
						</view>
					</scroll-view>
				</view>
			</uni-popup>

		</view>
	</view>
</template>

<script>
	import {
		marketOrderList,
		getAllFoodList,
		marketPickUp
	} from '@/common/api.js';
	import {
		callbackRequest,
		getStorage,
		alertTip,
		callPhone,
		formatImgUrl
	} from '@/common/util.js';
	import loadMore from '@/components/loadMore.vue';
	import contentNone from '@/components/contentNone.vue';
	import UniPopup from '@/components/uni-popup/uni-popup2.vue';
	export default {
		data() {
			return {
				callPhone: callPhone,
				formatImgUrl: formatImgUrl,
				loading: false,
				userId: getStorage('userInfo').id, //用户id
				pageHistory: 1,
				pageSizeHistory: 10,
				loadingTypeHistory: 0,
				contentTextHistory: {
					contentdown: "上拉加载更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				},
				statisticsData: {
					num: 0,
					price: 0
				}, //统计数据
				statisticsDataHistory: {
					num: 0,
					price: 0
				},
				orders: [], //订单列表
				takeDay: "",
				currentTabIndex: 0,
				ordersHistory: [], //历史订单列表
				takeFoodPlace: ['全部', '梨园', '科技楼'],
				takeFoodPlaceIndex: 0,
				allFoodList: [],
				startDate: this.getDay(-30),
				endDate: this.getDay(0),
				// 新样式 新增
				descriptionVal: '暂无商品',
				activeKey: 0, //左边菜品类型key值
				finished: false,
				refreshing: false,
			}
		},
		components: {
			loadMore,
			contentNone,
			UniPopup
		},
		onLoad(option) {
			this.initData();
		},
		methods: {
			imageError2(e, index2, index1) {
				this.ordersHistory[index1]['orderDetails'][index2]['goods_icon'] = '/static/icons/wutu.jpg';
			},
			initData() {
				let params = {};
				params = {
					"method": marketOrderList,
					data: {
						userId: this.userId,
						pageSize: this.pageSizeHistory,
						pageNum: this.pageHistory,
						takeFoodPlace: this.takeFoodPlaceIndex == 0 ? '' : this.takeFoodPlace[this.takeFoodPlaceIndex],
						queryType: "1",
						startDate: this.startDate,
						endDate: this.endDate
					}
				}
				callbackRequest(params)
					.then(res => {
						if (res.data.returnCode == 1) {
							let result = res.data.returnData || {};
							result.orderList = result.orderList || [];
							if (this.pageHistory == 1) {
								this.ordersHistory = result.orderList;
								if (result.numList.length > 0) {
									this.statisticsDataHistory = result.numList[0];
								} else {
									this.statisticsDataHistory = {
										num: 0,
										price: 0
									}
								}
							} else {
								if (result.orderList.length > 0) {
									this.ordersHistory = [...this.ordersHistory, ...result.orderList];
									this.loadingTypeHistory = 0;
								} else {
									this.loadingTypeHistory = 2;
								}
							}
							this.loading = true;
						} else {
							alertTip(res.data.returnMsg);
						}
					})
			},
			marketPickUp(item) {
				let params = {
					"method": marketPickUp,
					data: {
						userId: this.userId,
						orderId: item.id
					}
				}
				callbackRequest(params)
					.then(res => {
						if (res.data.returnCode == 1) {
							item.order_status = '3';
							alertTip(res.data.returnMsg);
						} else {
							alertTip(res.data.returnMsg);
						}
					})
			},
			circulation() {
				this.getAllFoodList();
				this.$refs.popup.open();
				//this.closeButPopup();
			},
			closeCirculation() {
				this.$refs.popup.close()
			},
			getAllFoodList() {
				let params = {
					"method": getAllFoodList,
					data: {
						userId: this.userId,
						takeFoodPlace: this.takeFoodPlaceIndex == 0 ? '' : this.takeFoodPlace[this.takeFoodPlaceIndex],
						queryType: "0",
					}
				}
				callbackRequest(params)
					.then(res => {
						if (res.data.returnCode == 1) {
							this.allFoodList = res.data.returnData;
							console.log(this.allFoodList);
						} else {
							alertTip(res.data.returnMsg);
						}
					})
			},
			choosePlace(index) {
				this.takeFoodPlaceIndex = index;
				this.pageHistory = 1;
				this.loadingTypeHistory = 0;
				this.initData();
			},
			getDay(i) {
				var date1 = new Date(new Date().getTime() + i * 24 * 60 * 60 * 1000);

				var year = date1.getFullYear();
				var month = date1.getMonth() + 1;
				if (month < 10) {
					month = "0" + month;
				}
				var day = date1.getDate();
				if (day < 10) {
					day = "0" + day;
				}
				return year + '-' + month + '-' + day;
			},
			startDateChange: function(e) {
				this.pageHistory = 1;
				this.loadingTypeHistory = 0;
				this.startDate = e.target.value;
				this.ordersHistory = [];
				this.initData();
			},
			endDateChange: function(e) {
				this.pageHistory = 1;
				this.loadingTypeHistory = 0;
				this.endDate = e.target.value;
				this.ordersHistory = [];
				this.initData();
			},
		},
		onReachBottom() {
			if (this.loadingTypeHistory !== 0) {
				return;
			}
			this.loadingTypeHistory = 1;
			this.pageHistory = this.pageHistory + 1;
			this.initData();
		}
	}
</script>

<style lang="scss">
	@import '@/static/css/common.scss';

	// .hzIndex {
	// 	background-color: #f0f5fa;
	// }

	// 搜索
	.search-box {
		margin-top: 36upx;
		padding: 24upx;
		background-color: #fff;
		border-radius: 20upx;

		.form-item {
			overflow: hidden;
		}

		.searchv {
			text-align: center;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.timecenter {
				font-size: 26upx;
				margin-left: 10upx;
				margin-right: 10upx;
				line-height: 30upx;
			}

			.time-data {
				width: 300upx;
				height: 60upx;
				font-size: 28upx;
				display: inline-block;
				border: 1px solid #0DB0A7;
				border-radius: 8upx;
				color: #555555;
				line-height: 60upx;
			}
		}

		.barber {
			margin-top: 32upx;
			// margin-left: 24upx;
			// border-bottom: 1px solid #eee;

			.barber-box {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-top: 24upx;

				.barber-item {
					width: 100*2upx;
					height: 38*2upx;
					background: #F2F6FA;
					border-radius: 4*2upx;
					border: 1px solid #F2F6FA;
					line-height: 38*2upx;
					color: #555555;
					text-align: center;
					font-size: 28upx;
					margin-left: 28upx;
				}

				.barber-item:first-child {
					margin-left: 0;
				}

				.barberactive {
					background: #EDFCF7;
					border: 1px solid #00ADA6;
					color: #0DB0A7;
				}
			}
		}

	}

	.box {
		display: flex;
		display: -webkit-flex;
	}

	.box-row-reverse {
		flex-direction: row;
	}

	/*水平排列*/
	.box-column {
		flex-direction: column;
	}

	/*上下排列*/
	.box-left {
		justify-content: flex-start;
	}

	/*居右*/
	.box-right {
		justify-content: flex-end;
	}

	/*居左*/
	.box-center {
		justify-content: center;
	}

	.box-between {
		justify-content: space-between;
	}

	.box-align-center {
		align-items: center;
	}

	.box-wrap {
		flex-wrap: wrap;
		margin-left: 28upx;
	}

	.container {
		position: relative;
		min-height: 100vh;
		overflow: hidden;

		.main {
			margin: 24upx;
			position: relative;
			z-index: 10;

		}
	}

	.totle {
		background: #FFFFFF;
		border-radius: 20upx;
		overflow: hidden;

		.num_wrap {
			width: 327*2upx;
			height: 99*2upx;
			background-image: url('@/static/haircut/mkbg.png');
			background-repeat: no-repeat;
			background-size: cover;
			margin: 24upx;
			position: relative;
			overflow: hidden;

			.flex-text {
				font-weight: bold;
				font-size: 16*2upx;
				color: #FFFFFF;
				margin-left: 24upx;
				margin-top: 24upx;
			}

			.flex-num-box {
				width: 100%;
				position: absolute;
				bottom: 24upx;

				.num-box {
					float: left;
					width: 50%;
					height: 80upx;
					text-align: center;
					color: #fff;
				}

				.num {
					text-align: center;

					text {
						font-weight: bold;
						line-height: 40upx;
						font-size: 32upx;
					}
				}

				.text {
					text-align: center;

					text {
						font-size: 28upx;
					}
				}
			}
		}
	}

	.list-item {
		overflow: hidden;
		padding: 10upx 10upx 20upx 5upx;

		.img-box {
			border-radius: 10upx;
			float: left;
			width: 120upx;
			height: 120upx;

			image {
				display: block;
				width: 100%;
				height: 100%;
				border-radius: 6px;
				margin-left: 20upx;
			}
		}

		.fr-content {
			margin-left: 160upx;

			.name {
				font-size: 32upx;
				color: #333;
				@include overstepOne;
				line-height: 40upx;
				margin-bottom: 6upx;
			}

			.num {
				font-size: 26upx;
				color: #666;
				line-height: 40upx;
				margin-bottom: 6upx;
			}

			.quota-num {
				min-height: 50rpx;
			}

			.buy-num {
				display: inline-block;
				border: 1px solid #ff0000;
				font-size: 24upx;
				color: #ff0000;
				border-radius: 8upx;
				line-height: 32upx;
				padding: 0 10upx;
				vertical-align: top;
			}

			.choose-num {
				float: right;

				image {
					width: 40upx;
					height: 40upx;
					vertical-align: top;
				}

				text {
					display: inline-block;
					font-size: 28upx;
					color: #666;
					width: 80upx;
					text-align: center;
					line-height: 40upx;
					vertical-align: top;
				}

				.fl {
					float: right;
					padding: 10upx 20upx;
					font-size: 24upx;
					color: $assistColor;
					line-height: 32upx;
					border: 1px solid $assistColor;
					border-radius: 8upx;
					margin-left: 10upx;
				}
			}

			.price {
				font-size: 28upx;
				margin-top: 52upx;
			}

			.ct {
				margin-left: 20upx;
				color: #ff00009c;
			}
		}
	}

	// 新 预定列表 样式
	.list_box {
		margin-top: 24upx;
	}

	.item_card {
		background-color: #fff;
		border-radius: 20upx;
		padding: 24upx;
		margin-bottom: 24upx;
	}

	.card_top {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.card_top_left {
		display: flex;
		align-items: center;
	}

	.card_top_left>img {
		width: 0.427*37*2upx;
		height: 0.427*37*2upx;
		margin-right: 0.2*37*2upx;
	}

	.st {
		font-size: 16px;
		font-weight: 600;
		color: #0e1a24;
	}

	.card_top_left>span:nth-of-type(1) {
		font-size: 0.427*37*2upx;
		font-weight: 600;
	}

	.order_num {
		font-size: 14*2upx;
		color: #555555;
		margin-left: 36upx;
	}

	.card_top_right {
		width: 160upx;
		height: 56upx;
		font-size: 28upx;
		text-align: center;
		line-height: 56upx;
		border-radius: 30upx;
	}

	.yd {
		background-color: #E0F6F5;
		color: #00ADA6;
	}

	.qx {
		background-color: #FFF3E7;
		color: #FB8107;
	}

	.card_con {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		margin-bottom: 24upx;
		justify-content: space-between;
	}

	.foodbox {
		width: 100%;
	}

	.food_img {
		display: flex;
		flex-shrink: 0;
		width: 66*2upx;
		height: 66*2upx;
		border-radius: 10*2upx;
		// background: #97A5B1;
	}

	.flex_name {
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		margin-left: 18upx;

		.food_name {
			font-size: 0.374*37*2upx;
			color: #0e1a24;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			width: 2.35*37*2upx;
		}

		.spec {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			font-size: 10*2upx;

			.s-text {
				font-size: 12*2upx;

				.s-t-price {
					font-weight: bold;
					font-size: 16*2upx;
					color: #555555;
				}
			}

			.s-num {
				font-size: 14*2upx;
			}
		}
	}

	.allNum {
		font-size: 0.32*37*2upx;
		color: #4b5b6b;
		display: flex;
		align-items: center;
	}

	.onefood {
		display: flex;
		margin-top: 24upx;
		// align-items: center;
	}

	.onefood>img {
		margin-right: 0.32*37*2upx;
	}

	.morefood {
		display: flex;
	}

	.morefood>view {
		margin-right: 0.2*37*2upx;
	}

	.card_bon {
		display: flex;
		align-items: flex-end;
		font-size: 28upx;
		justify-content: space-between;
	}

	.card_bon>view:first-child {
		font-size: 0.32*37*2upx;
		color: #999999;
	}

	.flex-btn-box {
		display: grid;

		.comment-btn {
			margin-top: 24upx;
			width: 84*2upx;
			height: 32*2upx;
			border-radius: 16*2upx;
			border: 1px solid #0DB0A7;
			font-size: 28upx;
			color: #0DB0A7;
			line-height: 32*2upx;
			text-align: center;
			min-width: 140upx;
			justify-self: end;
		}
	}

	.pic {
		font-size: 18*2upx;
		color: #333333;
		font-weight: 600
	}

	.van-tab {
		color: #4b5b6b;
	}

	.van-tab--active {
		color: #04aea5 !important;
		font-weight: 600;
	}
</style>