<template>
	<view class='ordercontainer'>
		<!-- <view>
			<u-navbar :custom-back="toMe" :is-back="true" :is-fixed='true' title-color="black" :border-bottom="false" title="我的订单"
				:background="{'background':'#FFFFFF'}">
			</u-navbar>
		</view> -->
		<view class="tabsnav">
			<u-tabs :list="list" :is-scroll="true" bg-color="#f0efed" bar-width="24" bar-height="24" gutter="53"
				font-size="24" inactive-color="#000" :active-item-style="{fontSize:'30rpx',color:'#ba8b45'}"
				:bar-style="{ right:'-36rpx',position:'relative',bottom:'32rpx',background:'#5b5b5a'}"
				:current="current" @change="change"></u-tabs>
		</view>

		<view class="orderlist">
			<!-- 1条数据开始 -->
			<view v-if="showData" class="nodata">
				<u-empty mode="data"></u-empty>
			</view>
			<block v-else>
				<view class="orderitem" v-for="(item,index) in orderList">
					<view class="ordertop">
						<text>运单号：{{item.express_number?item.express_number:'-'}}</text>
						<text>{{item.status==0?'待发货':'已发货'}}</text>
					</view>
					<view class="orderinfo">
						<view class="orderbrief" @click="toDetail(item)">
							<view class="orderimg">
								<block v-if="item.sku">
									<image
										:style="item.sku.image_url?'width:200rpx;height:120rpx':'width:150rpx;height:150rpx'"
										:src="item.sku.image_url?item.sku.image_url:require('@/static/logo-xietaitai.png')">
									</image>
								</block>
								<block v-else>
									<image
										:style="'width:150rpx;height:150rpx'"
										:src="require('@/static/logo-xietaitai.png')">
									</image>
								</block>
								
								<!-- <image
									:src="item.sku?(item.sku.image_url?item.sku.image_url:require('@/static/logo-xietaitai.png')):require('@/static/logo-xietaitai.png')"
									></image> -->
							</view>
							<view class="orderright">
								<view class="ordertitle">{{item.sku?item.sku.name:''}}</view>
								<view class="ordersmalltext">{{item.sku?item.sku.specs:''}}</view>
							</view>
						</view>
						<view class="ordertime" @click="toDetail(item)">
							<text>预计发货时间</text>
							<view>{{item.send_date?$moment(item.send_date).format("M月DD日"):''}}<text
									class="orderweek">{{item.send_date?'['+getWeek(item.send_date)+']':''}}</text>
									<!-- {{item.hh?item.hh:''}} -->
							</view>
						</view>
						<view class="ordertab">
							<!--  -->
							<text class="deleteorder" @click="copyText(item)" v-if="item.status==1">物流查询</text>
							<text class="deleteorder" @click="edit(item)" v-if="item.status===0">修改</text>
							<text class="orderinform" @click="copyText(item,'send')">通知收件人</text>
						</view>
					</view>
				</view>
			</block>

			<!-- 1条数据结束 -->
		</view>
	</view>
</template>

<script>
	import {
		isNull
	} from '@/common/util.js'
	export default {

		data() {
			return {
				list: [{
					name: '全部',
					status: '',
				}, {
					name: '待发货',
					status: 0
				}, {
					name: '已发货',
					status: 1
				}],
				status: '',
				current: 0,
				orderList: [],
				showData: true,
				updateFalse:'',
			}
		},
		onLoad() {
			const configs = uni.getStorageSync('configs')
			configs.map(item=>{
				if(item.key==='updateFalse'){
					this.updateFalse = item.value
				}
			})
			this.getOrderList()
		},
		methods: {
			change(index) {
				this.current = index;
				this.status = this.list[index].status
				this.getOrderList()
			},
			toDetail(item) {
				uni.setStorageSync('order_detail', item)
				uni.navigateTo({
					url: '/packages/myorder/infoconfirm'
				})
			},
			getWeek(date){
				const weeks = ["周日", '周一', '周二', '周三', '周四', '周五', '周六', ]
				let week = weeks[this.$moment(date).day()]
				return week
			},
			getOrderList() {
				if (this.status === '') {
					this.$u.api.getUserOrder().then(res => {
						this.orderList = res
						this.showData = res.length > 0 ? false : true
					})

					console.log(this.orderList.length)
				} else {
					this.$u.api.getUserOrder({
						status: this.status
					}).then(res => {
						this.orderList = res
						this.showData = res.length > 0 ? false : true
					})
				}
				// this.listArea = res.regions
			},
			edit(item) {
				if(item.can_update){
					uni.setStorageSync('vuex_update_card', item)
					uni.navigateTo({
						url: '/packages/order/order?hasCard=true&type=edit'
					})
				}else{
					uni.showToast({
						title: this.updateFalse || '不可更改',
						icon: 'none',
						duration: 2000
					});
				}

			},
			// 复制文本到剪贴板
			copyText(item,type) {
				// 通知收件人
				let message = ''
				if(type=='send'){
					message = `${item.contact}先生/女士，您兑换的${item.sku.name}（${item.sku.specs}）将于${item.send_date}发货，请保持电话${item.mobile}畅通。`
				}else{
					message = item.express_number
				} 
				uni.setClipboardData({
					data: message,
					success: function() {
						if(type=='send'){
							uni.showToast({
								title: '复制成功，快发给好友吧。',
								icon: 'none',
								duration: 2000
							});
						}else{
							uni.showToast({
								title: '单号复制成功',
								icon: 'none',
								duration: 2000,
							});
							if(item.express_type.appid){
								setTimeout(function(){
									uni.navigateToMiniProgram({
									    appId: item.express_type.appid, // 小程序appid
									    path: '', // 打开的页面路径，若为空则打开首页
									    extraData: {
									        // 需要传递给小程序的数据
									    },
									    success(res) {
									        // 打开成功的回调
									    },
									    fail(err) {
									        // 打开失败的回调
									    }
									})
								},2000)
							}
						}
					},
					fail: function(e) {
						uni.showToast({
							title: '单号复制失败',
							icon: 'none',
							duration: 2000
						});
						console.log('复制失败', e);
						// 可以添加错误处理或用户友好的提示
					}
				})
			}

		}
	}
</script>

<style lang="scss">
	.ordercontainer {
		background: #f0efed;
		width: 100%;
		height: 100vh;
		overflow-y: scroll;
	}

	.tabsnav {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 80rpx;
		z-index: 999;
		overflow: hidden;
		text-align: center;
		// padding: 0 33rpx;
	}

	.tabsnav {
		/deep/ .u-tab-item {
			z-index: 2;
		}
	}

	.orderlist {
		padding: 82rpx 50rpx 56rpx;
	}

	.orderitem {
		overflow: hidden;
		margin-bottom: 25rpx;
		background: url(../../static/libg.jpg) no-repeat center center;
		background-size: cover;
		border-radius: 10rpx;
		box-shadow: 0 5px 15px rgba(0, 0, 0, .47);
		padding: 12rpx;
	}

	.ordertop {
		display: flex;
		padding: 13rpx 20rpx 23rpx;
		justify-content: space-between;
	}

	.ordertop text {
		font-size: 24rpx;
		color: #f2d6c1;
	}

	.orderinfo {
		background: #fff;
		padding: 24rpx;
		border-radius: 10rpx;
	}

	.orderbrief {
		display: flex;
		padding-bottom: 24rpx;
	}

	.orderimg {

		// overflow: hidden;
		// width: 150rpx;
		// height: 150rpx;
		background: #fff;
		text-align: center;
	}

	.orderimg image {
		border: 1rpx solid rgba(186, 139, 69, .6);
		border-radius: 10rpx;
		width: 100%;
		height: 100%;
		vertical-align: middle;
	}

	.orderright {
		padding-left: 32rpx;
		width: 430rpx;
		font-size: 24rpx;
		color: #000;
		padding-top: 20rpx;
	}

	.ordertitle {
		height: 40rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		line-height: 40rpx;
	}

	.ordersmalltext {
		text-align: justify;
	}

	.ordertime {
		display: flex;
		justify-content: space-between;
		line-height: 36rpx;
		font-size: 24rpx;
		color: #000;
		padding-bottom: 10rpx;
		border-bottom: 1rpx solid #ececec;
		margin-bottom: 26rpx;
	}

	.ordertime view text.orderweek {
		color: #b62828;
		margin: 0 10rpx;
	}

	.ordertab {
		display: flex;
		justify-content: flex-end;
	}

	.ordertab text {
		display: inline-block;
		width: 160rpx;
		height: 50rpx;
		margin-left: 22rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 50rpx;
		border: 1rpx solid #cececd;
		font-size: 24rpx;
		color: #000;
	}

	.ordertab text.orderinform {
		color: #ba8b45;
		margin-right: 0;
	}

	.nodata {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
</style>