<template>
	<view class="content">
		<u-navbar title="" @leftClick="leftClick" :autoBack="true" :placeholder="true" >
			<view slot="center">
				<u-search placeholder="商品名、单号、下单人" :showAction="false" v-model="order_name" @search="search"></u-search>
			</view>
		</u-navbar>
		<view class="tabs">
			<u-tabs :list="tabList" @click="changeTab" :scrollable="true" :current="status - 1" lineWidth="0" :activeStyle="{
				fontSize: '30rpx',color: '#222222'
			}" :inactiveStyle="{
				fontSize: '30rpx',color: '#888888'
			}"></u-tabs>
		</view>
		<view class="center" v-if="pageList.length>0">
			<block v-for="(item,index) in pageList" :key="index">
				<view class="box" @click="toDetail(item.id)">
					<view class="top hflex acenter jbetween">
						<view class="order-no">订单号：<span style="color: #333;font-weight: 600;00">{{item.order_no}}</span></view>
						<view class="text_blue" v-if="item.status == 0">已取消</view>
						<view class="text_blue" v-if="item.status == 1">等待买家付款</view>
						<view class="text_blue" v-if="item.status == 2">等待买家付款</view>
						<view class="text_blue" v-if="item.status == 3">等待买家付款</view>
						<view class="text_blue" v-if="item.status == 4">买家已付款</view>
						<view class="text_blue" v-if="item.status == 5">等待买家收货</view>
						<view class="text_blue" v-if="item.status == 6">已完成</view>
					</view>
					<view class="time">下单时间：{{item.create_at}}</view>
					<view class="good">
						<block v-for="(item2,index2) in item.goods_item" :key="index2">
							<view class="hflex acenter" style="margin-top: 20rpx;">
								<image :src="item2.goods_cover" class="img"></image>
								<view class="good_center">
									<view class="name text_hide">{{item2.goods_name}}</view>
									<view class="spec text_hide">{{item2.goods_spec}}</view>
								</view>
								<view class="vflex jend">
									<view class="price">￥<span style="font-size: 30rpx;">{{item2.price_selling}}</span></view>
									<view class="num">X{{item2.stock_sales}}</view>
								</view>
							</view>
						</block>
						<view class="hflex jend">
							<view class="good_bottom">共{{item.goods_item.length}}件商品 实付款￥<span class="bottom_price">{{item.payment_amount}}</span></view>
						</view>
					</view>
					<view class="box_bottom hflex jend">
						<view class="btn1 hflex acenter jcenter" @click.stop="chat(index)">联系买家</view>
						<view class="btn1 btn2 hflex acenter jcenter" v-if="item.status==4" @click.stop="send(item.id)">立即发货</view>
					</view>
				</view>
			</block>
		</view>
		<view class="center" v-else>
			<view class="vflex acenter jcenter" style="height: 80vh;">
				<image src="@/static/images/no_list.png" mode="widthFix" style="width: 350rpx;"></image>
				<view class="text">暂时没有数据^^</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				order_name: '',
				status: 1,
				tabList: [
					{
						name: '全部',
						status: 1,
						badge: {
							value: 0,
							shape: 'horn',
						}
					},
					{
						name: '待付款',
						status: 2,
						badge: {
							value: 0,
							shape: 'horn',
						}
					},
					{
						name: '待发货',
						status: 3,
						badge: {
							value: 0,
							shape: 'horn',
						}
					},
					{
						name: '待收货',
						status: 4,
						badge: {
							value: 0,
							shape: 'horn',
						}
					},
					{
						name: '已完成',
						status: 5,
						badge: {
							value: 0,
							shape: 'horn',
						}
					}
				],
				page: 1,
				limit: 10,
				total: 1,
				pageList: [],
			}
		},
		onLoad(options) {
			that = this
			that.status = Number(options.status)
			that.getNum()
			uni.startPullDownRefresh();
		},
		onShow() {
			that.getNum()
			that.getList()
		},
		onPullDownRefresh() {
			that.page = 1
			that.pageList = []
			that.getNum()
			that.getList()
		},
		methods: {
			leftClick() {
			},
			search(e) {
				that.order_name = e
				that.page = 1
				that.pageList = []
				that.getList()
			},
			getNum() {
				$api.req({
					url: '/data/api.business.Order/order_count'
				}, function(res) {
					if(res.code == 1) {
						for(var i=0;i<that.tabList.length;i++) {
							that.tabList[i].badge.value = res.data[i]
						}
					}
				})
			},
			getList() {
				$api.req({
					url: '/data/api.business.Order/order_list',
					method: 'POST',
					data: {
						page: that.page,
						order_name: that.order_name,
						status: that.status
					}
				}, function(res) {
					if(res.code == 1) {
						for(var i=0;i<res.data.list.length;i++) {
							for(var j=0;j<res.data.list[i].goods_item.length;j++) {
								if( res.data.list[i].goods_item[j].goods_cover) {
									var temp = res.data.list[i].goods_item[j].goods_cover.split('|')
									res.data.list[i].goods_item[j].goods_cover = temp[0]
								}
								
							}
						}
						for(var i=0;i<res.data.list.length;i++) {
							for(var j=0;j<res.data.list[i].goods_item.length;j++) {
								res.data.list[i].goods_item[j].goods_spec = $api.resetspec(res.data.list[i].goods_item[j].goods_spec)
							}
						}
						if(that.page == 1) {
							that.pageList = res.data.list
						} else {
							that.pageList = that.pageList.concat(res.data.list)
						}
						that.total = res.data.page.total
						that.limit = res.data.page.limit
						uni.stopPullDownRefresh();
					}
				})
			},
			changeTab(e) {
				that.status = e.status
				that.page = 1
				that.pageList = []
				that.getList()
			},
			toDetail(id) {
				$api.jump('/pages/order/orderDetail?id=' + id)
			},
			chat(index) {
				var my = uni.getStorageSync("myUsername");
				var nameList = {
				  myName: my,
				  your: that.pageList[index].huanxinID,
				};
				uni.navigateTo({
				  url: "/pages/chatroom/chatroom?username=" + JSON.stringify(nameList),
				});
			},
			send(id) {
				$api.jump('/pages/order/send?id=' + id)
			},
			onReachBottom() {
				if (Number(that.page) * Number(that.limit) >= Number(that.total)) {
					$api.info("没有更多了")
				} else {
					that.page++
					that.getList(that.cid)
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		.u-search {
			width: 500rpx;
		}
		.tabs {
			.u-tabs__wrapper__nav__item {
				min-width: 20%;
				position: relative;
				white-space: nowrap;
				.u-badge {
					position: absolute;
					top: 18rpx;
					right: 14rpx;
					// width: 28rpx;
					height: 20rpx;
				}
			}
			
		}
		.center {
			width: 100%;
			box-sizing: border-box;
			padding: 0 30rpx;
			min-height: calc(100vh - 160rpx);
			background: #F4F4F4;
			.box {
				background: #FFFFFF;
				border-radius: 20px;
				width: 100%;
				margin: 20rpx 0 0;
				box-sizing: border-box;
				padding: 0 20rpx;
				.top {
					padding: 24rpx 0 14rpx;
					.order-no {
						font-size: 24rpx;
						font-weight: 400;
						color: #333333;
						line-height: 30rpx;
					}
					.text_blue {
						font-size: 24rpx;
						font-weight: 500;
						color: #506DFF;
						line-height: 34rpx;
					}
				}
				.time {
					font-size: 24rpx;
					font-weight: 400;
					color: #555555;
					line-height: 34rpx;
					padding: 0 0 24rpx;
				}
				.good {
					width: 100%;
					padding: 24rpx 0 20rpx;
					border-top: 1rpx solid #F4F4F4;
					border-bottom: 1rpx solid #F4F4F4;
					.img {
						width: 180rpx;
						height: 180rpx;
						border-radius: 16rpx;
					}
					.good_center {
						margin: 0 20rpx;
						width: 338rpx;
						.name {
							font-size: 30rpx;
							font-weight: 400;
							color: #222222;
							line-height: 42rpx;
							padding-bottom: 20rpx;
						}
						.spec {
							width: max-content;
							max-width: 288rpx;
							background: #F5F5F5;
							border-radius: 12rpx;
							font-size: 20rpx;
							font-weight: 400;
							color: #888888;
							line-height: 28rpx;
							box-sizing: border-box;
							padding: 12rpx;
						}
					}
					.price {
						font-size: 20rpx;
						font-weight: 400;
						color: #222222;
						line-height: 24rpx;
						padding: 0 0 16rpx;
					}
					.num {
						font-size: 24rpx;
						font-weight: 500;
						color: #888888;
						line-height: 28rpx;
					}
					.good_bottom {
						font-size: 22rpx;
						font-weight: 400;
						color: #272727;
						line-height: 32rpx;
						padding: 26rpx 0 0;
					}
					.bottom_price {
						font-size: 32rpx;
						color: #222222;
						font-weight: bold;
						line-height: 24px;
						line-height: 24px;
					}
				}
				.box_bottom {
					padding: 24rpx 0;
					.btn1 {
						width: 180rpx;
						height: 60rpx;
						border-radius: 36rpx;
						border: 1rpx solid #787878;
						font-size: 28rpx;
						font-weight: 400;
						color: #5D5D5D;
						line-height: 40rpx;
						margin-left: 24rpx;
					}
					.btn2 {
						background: #506DFF;
						border: none;
						color: #FFFFFF;
					}
				}
			}
		}
	}
</style>