<template>
	<el-layout>
		<el-navbar v-model:top="navTop" :placeholder="true" :fixed="true" :border="false" title="订单列表"></el-navbar>
		<view class="tabs" :style="{ top: (navTop - 1) + 'px' }">
			<el-tabs :showBar="false" :list="tabList" :current="current" @change="onTabs" :itemStyle="{width: '25%'}"></el-tabs>
		</view>
		<view style="width: 100%; height: 50px;"></view>
		
		<el-refresh ref="refreshRef" @refresh="refresh" @scrolltolower="scrolltolower" :scrollHeight="getScrollHeight">
			
			<view class="wrapper">
				
				<view class="order-list">
					<view class="order-box" v-for="(value,index) in list" :key="index">
						<view class="top">
							<view>{{ value.create_time }}</view>
							<view>{{ value.text }}</view>
						</view>
						<view class="content">
							<view class="goods-list" @click="navigateTo(`/pages/refund/detail?order_id=${value.id}`)">
								<view class="goods-box" v-for="(item,j) in value.goods" :key="j">
									<view class="thumb-image">
										<image :src="item.thumb_image"></image>
									</view>
									<view class="goods-info">
										<view class="goods-name">
											<text class="gift" v-if="item.type == 2">[赠品]</text>
											<text>{{ item.goods_name }}</text>
										</view>
										<view class="spec" v-if="item.spec.length > 0">{{ item.spec }}</view>
										<view class="price">￥<text>{{ item.sell_price }}</text></view>
									</view>
									<view class="num">x{{ item.goods_total_nums }}</view>
								</view>
							</view>
							<view class="order-amount">
								实付款：<text>￥</text><text>{{ value.order_amount }}</text>
							</view>
						</view>
						<view class="bottom">
							<view class="red" @click="navigateTo(`/pages/refund/detail?order_id=${value.id}`)">退款详情</view>
						</view>
					</view>
				</view>
				
			</view>
			
		</el-refresh>
	</el-layout>
</template>

<script setup>
	import { onLoad } from "@dcloudio/uni-app";
	import { nextTick,ref,computed } from "vue";
	import { inArray } from "@/el-ui/libs/check";
	import { navigateTo } from "@/el-ui/libs/navigate";
	
	const refreshRef  = ref();
	const navTop 	  = ref(0);
	const current 	  = ref(0);
	const activeType  = ref('');
	const tabList = ref([
		{name: "全部",type: "all"},
		{name: "待审核",type: "verify"},
		{name: "已同意",type: "agree"},
		{name: "已拒绝",type: "refuse"}
	]);
	
	const getTabsIndex = (value)=>{
		tabList.value.find((item,index)=>{
			if(typeof value == "string" && item.type == value){
				current.value = index;
				activeType.value = item.type;
				return true;
			}else if(typeof value == "number" && value == index){
				current.value = index;
				activeType.value = item.type;
				return true;
			}
		});
	}
	
	onLoad((option)=>{
		getTabsIndex(option.type)
		nextTick(()=>{
			init();
		})
	});
	
	const list = ref([]);
	const init = async ()=>{
		refreshRef.value?.resetPage();
		list.value = [];
	}
	
	const onTabs = (value)=>{
		getTabsIndex(value);
		init();
	}
	
	const getScrollHeight = computed(()=>{
		const { windowHeight } = uni.getSystemInfoSync();
		return (windowHeight - navTop.value) - 50;
	})
	
	/**
	 * 下拉刷新
	 */
	const refresh = ()=>{
		init();
	}
	
	/**
	 * 上拉加载
	 */
	const scrolltolower = (data)=>{
		if(refreshRef.value.isMore()) {
			refreshRef.value.loadingPage();

			setTimeout(() => {
				uni.$http.get("order.refund/index",{ type: activeType.value, page: data.page }).then(res=>{
					if(res.code == 1){
						if(data.page == 1) list.value = [];
						refreshRef.value.setData(res.data.total,res.data.list.length);
						list.value = list.value.concat(res.data.list);
					}else if(res.code == -1){
						if(data.page == 1 && res.data.list.length <= 0) list.value = [];
						refreshRef.value.emptyPage();  
					}
				});
			}, 100)
		}
	}
	
</script>

<style lang="scss" scoped>
	.tabs {
		position: fixed;
		left: 0;
		width: 100%;
		z-index: 100000;
	}
	.wrapper {
		padding: 20rpx;
		.order-list {
			display: flex;
			flex-direction: column;
			.order-box {
				background-color: #fff;
				border-radius: 15rpx;
				margin-bottom: 30rpx;
				font-size: 28rpx;
				.top {
					padding: 0 20rpx;
					height: 80rpx;
					line-height: 80rpx;
					display: flex;
					justify-content: space-between;
					border-bottom: 1px solid #eee;
					view {
						&:nth-child(2){
							color: red;
						}
					}
				}
				.content {
					padding: 0 20rpx;
					.goods-list {
						.goods-box {
							display: flex;
							flex-direction: row;
							margin-top: 20rpx;
							.thumb-image {
								image {
									width: 150rpx;
									height: 150rpx;
									border-radius: 20rpx;
								}
							}
							.goods-info {
								width: 100%;
								padding: 0 20rpx;
								display: flex;
								flex-direction: column;
								view {
									padding: 5rpx 0;
									&.goods-name{
										display: -webkit-box;
										overflow: hidden; 
										word-break:break-all;
										-webkit-line-clamp: 1;
										-webkit-box-orient: vertical;
										.gift {
											font-size: 22rpx;
											color: #eb5d2a;
										}
									}
									&.spec {
										display: -webkit-box;
										overflow: hidden; 
										word-break:break-all;
										-webkit-line-clamp: 1;
										-webkit-box-orient: vertical;
										font-size: 26rpx;
										color: #999;
									}
									&.price {
										color: #eb5d2a;
										font-size: 24rpx;
										text {
											font-size: 30rpx;
										}
									}
								}
							}
							.num {
								padding-top: 6rpx;
							}
						}
					}
					.order-amount {
						font-weight: bold;
						font-size: 26rpx;
						display: flex;
						justify-content: flex-end;
						align-items: center;
						height: 80rpx;
						text {
							color: #eb5d2a;
							font-size: 28rpx;
							
						}
					}
				}
				.bottom {
					border-top: 1px solid #eee;
					width: 100%;
					min-height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					padding: 10rpx 0;
					view {
						font-size: 24rpx;
						text-align: center;
						border-radius: 30rpx;
						background-color: #fff;
						padding: 15rpx 25rpx;
						margin-right: 20rpx;
						&.white {
							border: 1px solid #eee;
						}
						&.red {
							background-color: #e93323;
							color: #fff;
						}
					}
				}
			}
		}
	}
</style>