<template>
	<view class="bg">
		<view class="top-box">
			<view class="search-box flex-between">
				<view class="left">
					<image src="https://static.ticket.sz-trip.com/tourist/index/search.png"></image>
					<input v-model="keywords" type="text" placeholder="请输入关键字" />
				</view>
		
				<view class="search-btn flex-center" @click="onReload()">搜索</view>
			</view>
			<view class="common-box">
				<view @click="setType(index)" v-for="(item, index) in typeList" :key="index" :class="['common-type', typeIndex == index ? 'active' : '']">
					{{ item.name }}
				</view>
			</view>
		</view>
		
		<view v-if="list.length > 0">
			<navigator :url="'/subPackages/order/orderDetail?id=' + item.order_id" class="item" v-for="(item, key) in list" :key="item.id">
				<view class="item-top flex-between">
					<view>
						<view>{{item.order_child[0].type_id == 1 ? '线路产品' : '导游服务'}}</view>
						<view>订单号：{{ item.order_id }}</view>
					</view>
					<view class="item-state">{{item.status_text}}</view>
				</view>
				
				<view class="item-center">
					<image :src="showImg(item.order_child[0].specifications_image)" class="item-img"></image>
					<view class="item-content flex-column">
						<view class="title text-overflow">{{item.order_name}}</view>
						<view style="margin-bottom: 10rpx;">
							<view class="subtitle flex-between" v-for="(sItem,sIndex) in item.order_child" :key="sItem.id">
								<view>{{sItem.specifications_name}}</view>
								<view>
									￥{{sItem.pay_money}}
									<span style="margin-left: 30rpx;">x{{sItem.num}}</span>
								</view>
							</view>
						</view>
						<view class="subtitle" style="text-align: right;color: #333333;">共{{item.order_child.length}}件商品</view>
						<view class="price">
							<view class="subtitle" style="margin-right: 15rpx;">总价￥{{item.money / 100}}</view>
							实付款<span>{{item.pay_money / 100}}</span>
						</view>
					</view>
				</view>
				
				<view class="item-btns">
					<view @click.stop="() => refund(item.order_id, key)" v-if="item.status == 'PAYMENT_SUCCESSFULLY'">申请退款</view>
					<view @click.stop="() => closeOrder(item.order_id, item)" v-if="item.status == 'WAIT_PAYMENT'">关闭订单</view>
					<view @click.stop="gotoDetailByTypeId(item.order_id[0].goods_id, item.order_id[0].type_id)" v-if="item.status == 'CLOSED'">再次购买</view>
					<view class="pay-btn" @click.stop="setOrderId(item.order_id)" v-if="item.status == 'WAIT_PAYMENT'">立即支付</view>
				</view>
			</navigator>
		</view>
		
		<view v-if="list.length === 0" class="noDate">
			<view>暂无订单</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				finished: false,
				list: [],
				typeList: [
					{
						id: 'ALL',
						name: '全部'
					},
					{
						id: 'WAIT_PAYMENT',
						name: '待支付'
					},
					{
						id: 'PAYMENT_SUCCESSFULLY',
						name: '已取消'
					},
					{
						id: 'PAYMENT_SUCCESSFULLY',
						name: '待出行'
					},
					{
						id: 'WAIT_REFUND,REFUND_SUCCESS,REFUND_REFUSAL,REFUND_ERROR,REFUND_PART',
						name: '退款/售后'
					}
				],
				typeIndex: 0,
				ajaxFlag: true,
				keywords: '',
				orderId: null,
				dateRange: [],
				type: ''
			}
		},
		onLoad(options) {
			if (options.type) this.typeIndex = this.typeList.findIndex(vm => vm.name === options.type);
			this.getList();
		},
		onReachBottom() {
			if (this.finished) return false;
			this.getList();
		},
		methods: {
			// 申请退款
			refund(id, index) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否申请退款？',
					success: successRes => {
						if (successRes.confirm) {
							that.Post(
								{
									order_id: id
								},
								'/api/order/applyRefund'
							).then(res => {
								if (res.code == 1) {
									uni.showToast({
										title: '申请成功',
										icon: 'success'
									});
									that.onReload();
								}
							});
						}
					}
				});
			},
			// 关闭订单
			closeOrder(id, index) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否关闭订单？',
					success: successRes => {
						if (successRes.confirm) {
							that.Post(
								{
									order_id: id
								},
								'/api/order/closeOrder'
							).then(res => {
								if (res.code == 1) {
									uni.showToast({
										title: '关闭成功',
										icon: 'success'
									});
									that.onReload()
								}
							});
						}
					}
				});
			},
			// 立即支付
			setOrderId(id) {
				
			},
			onReload() {
				this.list = [];
				this.finished = false;
				this.getList();
			},
			// 获取订单列表
			getList() {
				let data = {
					status: this.typeList[this.typeIndex].id == 'ALL' ? '' : this.typeList[this.typeIndex].id,
					offset: this.list.length,
					limit: 5,
					name: this.keywords
				};
				this.Post(data, '/api/order/orderList').then(res => {
					this.list = [...this.list, ...res.data]
					if (res.data.length < 5) {
						this.finished = true;
					}
				});
			},
			setType(index) {
				this.typeIndex = index;
				this.onReload();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bg {
		min-height: 100vh;
		background: #F7F7F7;
		padding-bottom: 100rpx;
	}
	
	.top-box {
		background: #FFFFFF;
		
		.search-box {
			height: 84rpx;
			padding: 0 26.67rpx;
			
			.left {
				width: 563rpx;
				height: 60rpx;
				background: #FFFFFF;
				border: 1rpx solid #96684F;
				padding: 0 26rpx;
				display: flex;
				align-items: center;
				
				image {
					width: 29.33rpx;
					height: 29.33rpx;
					margin-right: 21rpx;
				}
				
				input {
					flex: 1;
					font-weight: 500;
					font-size: 28rpx;
					color: #999999;
				}
			}
			
			.search-btn {
				width: 120rpx;
				height: 60rpx;
				background: #96684F;
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}
		
		.common-box {
			height: 93rpx;
			display: flex;
			align-items: center;
			
			.common-type {
				width: 20%;
				text-align: center;
				font-weight: 500;
				font-size: 28rpx;
				color: #666666;
			}
			
			.common-type.active {
				font-weight: bold;
				font-size: 28rpx;
				color: #96684F;
				position: relative;
			}
			.common-type.active::after {
				width: 20%;
				height: 4rpx;
				content: '1';
				font-size: 0;
				display: block;
				position: absolute;
				left: 0;
				right: 0;
				bottom: -10rpx;
				margin: auto;
				background: #96684F;
			}
		}
	}
	
	.noDate {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 200rpx;
		font-size: 24rpx;
		color: #777777;
	}
	
	.item {
		width: 697rpx;
		height: auto;
		background: #FFFFFF;
		border-radius: 13rpx;
		margin: 27rpx auto 0;
		
		.item-top {
			height: 100rpx;
			background: linear-gradient(90deg, #F4E8DD, #F9F2EC);
			border-radius: 13rpx 13rpx 0rpx 0rpx;
			padding: 0 20rpx;
			font-weight: 500;
			font-size: 27rpx;
			color: #96684F;
			
			.item-state {
				font-weight: bold;
				font-size: 28rpx;
				color: #DC2525;
			}
		}
		
		.item-center {
			height: 270rpx;
			padding: 20rpx;
			display: flex;
			
			.item-img {
				width: 140rpx;
				height: 140rpx;
				border-radius: 7rpx;
				margin-right: 20rpx;
			}
			
			.item-content {
				flex: 1;
				justify-content: space-between;
				
				.title {
					font-weight: 500;
					font-size: 31rpx;
					color: #000000;
				}
				
				.subtitle {
					font-weight: 500;
					font-size: 24rpx;
					color: #999999;
				}
				
				.price {
					font-weight: 500;
					font-size: 24rpx;
					color: #333333;
					display: flex;
					align-items: baseline;
					justify-content: flex-end;
					
					span {
						font-weight: bold;
						font-size: 32rpx;
						color: #DC2525;
					}
					span::before {
						font-size: 24rpx;
						content: '￥';
					}
				}
			}
		}
		
		.item-btns {
			height: 102rpx;
			border-top: 1rpx solid rgba(216, 216, 216, .5);
			padding-right: 20rpx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			
			view {
				width: 160rpx;
				height: 60rpx;
				line-height: 60rpx;
				text-align: center;
				background: rgba(237,237,237,0);
				border-radius: 7rpx;
				border: 1rpx solid #D8D8D8;
				margin-left: 13rpx;
			}
			
			.pay-btn {
				background: #DC2525;
				color: #fff;
				border: none;
			}
		}
	}
</style>