<template>
	<view class="cart">
		<view class="head-box"></view>
		<Navbar title="我的订单" :bgColor="bgColor" leftIconSize="0px" height="100rpx" :titleStyle="{color:fontColor}" />
		<view class="content contentPosition_">
			<uv-sticky offsetTop="220rpx" :bgColor="bgColor">
				<uv-tabs :scrollable="false" @click="tabs" :list="tabList" lineWidth="40" :current="tabCurrent"
					:lineColor="`url(${lineBg}) 100% 100%`"
					:activeStyle="{color: '#FD5C5C', fontWeight: 'bold',transform: 'scale(1.05)'}"
					:inactiveStyle="{color: '#999', transform: 'scale(1)'}"
					itemStyle="padding-left: 15px; padding-right: 15px; height: 44px;"></uv-tabs>
			</uv-sticky>
			<cardList :cardListData="cardListData" @btnClick="btnClick" @toOrderDetails="toOrderDetails" />
			<uv-load-more :status="status" fontSize="24rpx" dashed line />
		</view>
		<uv-modal ref="modal" align="center" content='是否取消订单？' @confirm="confirm"></uv-modal>
		<tabber select="cart" />
	</view>
</template>

<script>
	import tabber from '@/components/base/tabbar.vue'
	import cardList from '@/components/cart/CardList.vue'
	import Navbar from '@/pages/components/Navbar.vue'
	import {
		globalMixin
	} from '../mixins/globalMixin';

	export default {
		mixins: [globalMixin],
		components: {
			tabber,
			cardList,
			Navbar
		},
		data() {
			this.orderId = '';
			return {
				tabCurrent:0,
				status: "loading",
				params: {
					state: '',
					pageNo: 1,
					pageSize: 10
				},
				tabList: [{
						id: '',
						name: '全部'
					},
					{
						id: 0,
						name: '未付款'
					},
					{
						id: 1,
						name: '待参加'
					},
					{
						id: 2,
						name: '已完成'
					},
					{
						id: 3,
						name: '已取消'
					},
				],
				lineBg: require('@/static/image/cart/tabIcon.png'),
				totalPage: 0,
				cardListData: []
			}
		},
		onReachBottom() {
			if (this.params.pageNo >= this.totalPage) return
			this.params.pageNo++
			this.getOrderPageList()
		},
		onLoad(e) {
			if(uni.getStorageSync('currentState')) {
				this.tabCurrent = uni.getStorageSync('currentState')
				uni.removeStorageSync('currentState')
			}
			this.params.state = this.tabList[this.tabCurrent].id
			this.getOrderPageList()
		},
		methods: {
			activeList() {
				uni.navigateTo({
					url: '/pages_my/activeList'
				})
			},
			getOrderPageList() {
				this.status = "loading"
				this.$api('orderPageList', this.params, res => {
					if (res.code == 200) {
						this.totalPage = res.result.pages
						this.cardListData = [...this.cardListData, ...res.result.records]
						if (this.params.pageNo >= this.totalPage) {
							this.status = "nomore"
						} else {
							this.status = "loadmore"
						}
					}
				})
			},
			tabs(val) {
				this.params.state = val.id
				this.params.pageNo = 1
				this.cardListData = []
				this.getOrderPageList()
			},
			toOrderDetails(val) {
				uni.navigateTo({
					url: `/pages_order/orderDetails?id=${val.id}`
				})
			},
			btnClick(item, type) { //0取消活动 1活动签到 2评价活动 3开具发票 
				this.orderId = item.id;
				if (type == 0) {
					this.$refs.modal.open();
				}
				if (type == 1) {
					this.$api('signIn', {
						orderId: item.id
					}, res => {
						if (res.code == 200) {
							this.cardListData = []
							this.getOrderPageList()
							this.$refs.toast.show({
								type: 'success',
								message: res.result
							})
						}
					})
				}
				if (type == 2) {
					uni.navigateTo({
						url: `/pages_order/orderEvaluation?activityId=${this.orderId}`
					})
				}
				if (type == 3) {
					uni.navigateTo({
						url: `/pages_order/invoiceIssuance?orderId=${this.orderId}`
					})
				}
			},
			confirm() {
				this.$api('cancelOrder', {
					orderId: this.orderId
				}, res => {
					if (res.code == 200) {
						this.cardListData = [];
						this.getOrderPageList();
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		padding-bottom: 200rpx;
	}
</style>