<template>
	<view class="flex flex-column overflow-hidden" style="height: 100vh;">
		<!-- <view class="flex align-center justify-between pt-3 bg-white flex-shrink">
			<view  v-for="(item,index) in tabBar" :key="index" class="flex-1 text-center " @click="changeTab(index)">
				<text :class="tabIndex==index?'text-danger font-weight-bold':'text-muted'">{{item}}</text>
				<view :class="tabIndex==index?'bg-danger':''" style="width: 30rpx;height: 10rpx;" class="rounded-lg d-block mx-auto mt-2 "></view>
			</view>
		</view> -->
		<view class="flex-1" style="overflow-y: auto;">
			<scroll-view class="flex-1 list overflow-hidden" :scroll-y="true" :refresher-enabled="true"
				:refresher-triggered="refresh" @refresherrefresh="refreshData()" @scrolltolower="loadData()">
				<i-order-list :resdata="orderList" @cancel="cancelOrder" @confirm="confirmOrder"></i-order-list>
				<uni-load-more :status="more_status" @clickLoadMore="loadData"></uni-load-more>
			</scroll-view>
		</view>
		<!-- <swiper class="flex-1" :current="tabIndex"  @change="swiperChange" > -->
			<!-- <swiper-item class="flex" v-for="(item,index) in tabBar" :key="index"> -->
				
			<!-- </swiper-item> -->
		<!-- </swiper> -->
	</view>
</template>

<script>
import { TaAjax } from '../../common/ajax'
import { friendlyDate } from '../../common/util';

	export default {
		data() {
			return {
				tabIndex:0,
				tabBar:['全部','待发货','待收货','已收货'],
				loading: false,
				refresh: true,
				finish: true,
				curPage: 1,
				orderList: [],
			}
		},
		computed: {
			more_status() {
				if (this.finish) {
					return 'no-more';
				} else if (this.loading) {
					return 'loading';
				} else {
					return 'more';
				}
			},
		},
		methods: {
			refreshData() {
				this.refresh = true;
				this.curPage = 1;
				this.loadData();
			},
			loadData() {
				if (this.refresh) {
					this.orderList.length = 0;
					this.finish = false;
				}
				if (this.finish) {
					return;
				}
				this.loading = true;
				TaAjax.get('/points_mall/api.auth.order/get', {
					page: this.curPage
				}).then((response) => {
					const data = response.data;
					const data_list = data.list.map((item) => {
						item.create_at = friendlyDate(new Date(item.create_time.replace(/\-/g, '/'))
						.getTime())
						return item;
					})
					if (this.refresh) {
						this.orderList.length = 0
					}
					this.orderList = this.orderList.concat(data_list)
					this.finish = data.page.total <= this.orderList.length;
					this.curPage++;
				}).catch((e) => {
					console.warn(e)
					if (this.orderList.length == 0) {
						this.finish = true;
					}
				}).finally(() => {
					this.refresh = false;
					this.loading = false;
				});
			},
			changeTab(index){
				this.tabIndex = index
				this.refreshData()
			},
			swiperChange(e){
				this.tabIndex = e.detail.current
				this.refreshData()
			},
			cancelOrder(orderNo) {
				uni.showLoading({
					title: '取消中'
				})
				TaAjax.post('/points_mall/api.auth.order/cancel', {
					order_no: orderNo,
				}).then((response) => {
					if (response.code == 1) {
						uni.hideLoading()
						uni.showToast({
							title: '取消成功',
							icon: 'success',
						})
					} else {
						uni.hideLoading()
						uni.showToast({
							title: response.info,
							icon: 'none',
						})
					}
				}).catch((e) => {
					console.warn(e)
					uni.hideLoading()
					uni.showToast({
						title: '取消失败',
						icon: 'none',
					})
				}).finally(() => {
					this.refreshData()
				})
			},
			confirmOrder(orderNo) {
				uni.showLoading({
					title: '确认中',
				})
				TaAjax.post('/points_mall/api.auth.order/confirm', {
					order_no: orderNo,
				}).then((response) => {
					if (response.code == 1) {
						uni.hideLoading()
						uni.showToast({
							title: '确认成功',
							icon: 'success',
						})
					} else {
						uni.hideLoading()
						uni.showToast({
							title: response.info,
							icon: 'none',
						})
					}
				}).catch((e) => {
					console.warn(e)
					uni.hideLoading()
					uni.showToast({
						title: '确认失败',
						icon: 'none',
					})
				}).finally(() => {
					this.refreshData()
				})
			},
		}
	}
</script>

<style scoped>

</style>
