<template>
	<NavBar />
	<view class="container">
		<PageTitle :title="$t('activity.shop')" />
	</view>
	<view class="content" :style="{ height: contentHeight }">
		<view
			style="
				background-color: rgba(0, 0, 0, 0.71);
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				padding: 36upx;
				box-sizing: border-box;
				display: flex;
				flex-direction: column;
			"
		>
			<view class="filter">
				<view style="display: flex; align-items: flex-end">
					<view style="font-weight: 700; font-size: 40upx; color: #25c0e5; margin-right: 10upx">{{ point }}</view>
					<view style="color: #fff; font-weight: 400; font-size: 32upx">{{ $t('activity.point') }}</view>
				</view>
				<button style="margin-right: 16upx" class="shop-dark-btn" @click="goMyOrder">
					{{ $t('activity.myOrder') }}
				</button>
				<button style="margin: 0" class="shop-btn" @click="goPointRecord">
					{{ $t('activity.pointRecord') }}
				</button>
			</view>
			<view class="filter" style="flex-wrap: nowrap; overflow-x: auto; column-gap: 56upx" v-if="typeList && typeList.length">
				<template v-for="(item, index) in typeList">
					<view class="goods-type" :class="{ 'goods-type-active': searchFormData.goods_type == item.code }" @click="handleType(item.code)">{{ item.text }}</view>
				</template>
			</view>
			<view class="filter">
				<view style="flex-grow: 0; flex-shrink: 1; margin-right: calc(40upx + 2px)">
					<uni-easyinput
						primaryColor="#29BBE4"
						:clearable="false"
						v-model="searchFormData.integralBegin"
						class="filter-input"
						:inputBorder="false"
						:styles="filterInputStyle"
					>
						<template #right>
							<view style="line-height: 60upx">{{ $t('activity.point') }}</view>
						</template>
					</uni-easyinput>
				</view>
				<view style="margin: auto 8upx">
					<view style="width: 28upx; height: 1px; background-color: rgba(255, 255, 255, 0.2)"></view>
				</view>
				<view style="flex-grow: 0; flex-shrink: 1; margin-right: calc(40upx + 2px)">
					<uni-easyinput
						primaryColor="#29BBE4"
						:clearable="false"
						v-model="searchFormData.integralEnd"
						class="filter-input"
						:inputBorder="false"
						:styles="filterInputStyle"
					>
						<template #right>
							<view style="line-height: 60upx">{{ $t('activity.point') }}</view>
						</template>
					</uni-easyinput>
				</view>
				<button style="margin: 0 0 0 40upx" class="shop-btn" @click="handleQuery">
					<icon type="search" size="26upx" style="margin-right: 10upx" color="#0F3675" />
					{{ $t('activity.search') }}
				</button>
			</view>
			<Spin v-show="loading1" />
			<scroll-view scroll-y="true" class="goods-list" :show-scrollbar="false" @scrolltoupper="upper" @scrolltolower="lower">
				<view style="display: flex; justify-content: space-between; row-gap: 20upx; flex-wrap: wrap">
					<template v-for="(item, index) in goodsList">
						<view class="goods-item" @click="goDetail(item.id)">
							<image class="img" mode="aspectFill" :src="item.imgs.split(',')[0]"></image>
							<view class="info">
								<view class="goods-title uni-ellipsis">{{ item.goods_name }}</view>
								<view class="goods-footer">
									<text>{{ item.integral + $t('activity.point') }}</text>
								</view>
							</view>
						</view>
					</template>
					<uni-load-more style="width: 100%" v-if="goodsList && goodsList.length" :status="loadStatus"></uni-load-more>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { initShop, getGoodsList } from '@/services/activity/shop';
export default {
	data() {
		return {
			contentHeight: 'auto',
			filterInputStyle: {
				backgroundColor: 'transparent'
			},
			loading: false,
			loading1: false,
			typeList: [],
			point: 0,
			searchFormData: {
				curr_page: 1,
				page_size: 12,
				integralBegin: undefined,
				integralEnd: undefined,
				goods_type: ''
			},
			total: 0,
			goodsList: [],
			hasMore: false
		};
	},
	computed: {
		loadStatus() {
			if (this.loading || this.loading1) {
				return 'loading';
			}
			if (this.goodsList && this.goodsList.length) {
				if (this.goodsList.length < this.total) {
					return 'more';
				}
			}
			return 'no-more';
		}
	},
	methods: {
		async getListData() {
			this.loading1 = true;
			let res = await initShop();
			this.point = res.data.integral;
			this.typeList = [{ code: '', text: this.$t('activity.all') }, ...res.data.typeList];
			await this.loadGoods();
			this.loading1 = false;
		},
		async loadGoods() {
			this.searchFormData.curr_page = 1;
			this.searchFormData.goodsList = [];
			this.hasMore = false;
			this.total = 0;
			this.goodsList = [];
			const res = await getGoodsList(this.searchFormData);
			if (res && res.code === 0) {
				this.total = res.data.totalRecords;
				this.goodsList = res.data.records;
				this.searchFormData.curr_page = this.goodsList && this.total > this.goodsList.length ? 2 : 1;
				this.hasMore = this.goodsList && this.total > this.goodsList.length;
			}
		},
		async handleType(type) {
			this.loading1 = true;
			this.searchFormData.goods_type = type;
			await this.loadGoods();
			this.loading1 = false;
		},
		lower() {
			if (this.loading || this.loading1 || !this.hasMore) {
				return;
			}
			this.loading = true;
			getGoodsList(this.searchFormData)
				.then((res) => {
					if (res && res.code === 0) {
						this.total = res.data.totalRecords;
						this.goodsList = [...this.goodsList, ...res.data.records];
						this.searchFormData.curr_page = this.total > this.goodsList.length ? this.searchFormData.curr_page + 1 : this.searchFormData.curr_page;
						this.hasMore = this.total > this.goodsList.length;
					}
					this.loading = false;
				})
				.catch(() => {
					this.loading = false;
				});
		},
		goDetail(id) {
			uni.navigateTo({
				url: '/pages/activity/shop/goods?id=' + id
			});
		},
		goMyOrder() {
			uni.navigateTo({
				url: '/pages/activity/shop/myOrder'
			});
		},
		goPointRecord() {
			uni.navigateTo({
				url: '/pages/activity/shop/pointRecord'
			});
		},
		async handleQuery() {
			this.loading1 = true;
			await this.loadGoods();
			this.loading1 = false;
		}
	},
	created() {
		this.getListData();
	},
	mounted() {
		this.$nextTick(() => {
			const query = uni.createSelectorQuery().in(this);
			query
				.select('.container')
				.boundingClientRect((data) => {
					if (data) {
						this.contentHeight = `calc(100vh - ${data.height + 46}px)`;
					}
				})
				.exec();
		});
	}
};
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
