<template>
	<view class="exhibit-index exhibitor-index">
		<nav-bar title="展品信息" @init="onInitNavbar"></nav-bar>
		<u-scroll-view @scroll-near-lower="onScrollToLower">
			<view class="main-container">
				<view class="exhibitor-filter">
					<view>
						<view class="exhibitor-filter-label">展馆号</view>
						<u-dropdown-select ref="select1" v-model="categoryId" placeholder="选择展馆号" :options="categories" @dropdown="onSelectDropdown(1)" @change="searchList()"/> 
					</view>
					<view>
						<view class="exhibitor-filter-label">产品类别</view>
						<u-dropdown-select ref="select2" v-model="searchCategoryId" placeholder="选择产品类别" :options="categories" @dropdown="onSelectDropdown(2)" @change="searchList()" /> 
					</view>
					<view>
						<view class="exhibitor-filter-label">应用领域</view>
						<u-dropdown-select  ref="select3" v-model="searchApplicationAreas" placeholder="选择应用领域" :options="applicationAreass" @dropdown="onSelectDropdown(3)" @change="searchList()"/> 
					</view>
				</view>
				<u-search v-model="searchKeyword" placeholder="搜索展商 / 展品名称 / 会议" @search="onSearch" />
				<view class="ad-space">
					<image src="https://oss.starify.cn/prod/starify/up/0001018678/20241108/672da70a6c76a.png?x-oss-process=image/resize,w_200" mode="aspectFill"/>
				</view>
				<van-empty v-if="exhibitList.length === 0" description="暂无数据" />
				<view v-else class="exhibit-list exhibitor-list">
					<template v-for="(item, index) in exhibitList">
						<exhibit-item :item="item" :key="index" :pollShow="pollShow.product_poll_show" @share="onShare" />
					</template>	
				</view>
			</view>
		</u-scroll-view>
		<u-share-action-sheet :show.sync="showShare" :show-info="shareInfo" />
	</view>
</template>

<script>
	import NavBar from '@/components/layout/nav-bar'
	import UScrollView from '@/components/common/u-scroll-view'
	import UShareActionSheet from '@/components/common/u-share-action-sheet'
	import USearch from '@/components/common/u-search'
	import UDropdownSelect from '@/components/common/u-dropdown-select'
	import ExhibitItem from '@/pages/exhibitor/components/exhibit-item.vue'
	import { exhibitorsProductList, productCategoryList, applicationAreasList, globalPollShow } from '@/api/exhibitor'
	
	export default {
		options: {
			// styleIsolation: 'shared'
		},
		components: {
			NavBar,
			UScrollView,
			USearch,
			UDropdownSelect,
			UShareActionSheet,
			ExhibitItem
		},
		computed: {
		},
		data() {
			return {
				searchCategoryId: '',
				searchApplicationAreas: '',
				scrollviewHeight: 0,
				searchKeyword: '',
				exhibitList: [],
				exhibitParams: {
					page: 1,
					page_size: 10,
					keyword: '',
					product_cate_id: '',
					application_areas_id: ''
				},
				exhibitList: [],
				exhibitListLoading: false,
				exhibitListLastPage: 1,
				shareInfo: null,
				showShare: false,
				categories: [],
				applicationAreass: [],
				pollShow: {
					exhibitors_poll_show: 0,
					product_poll_show: 0
				}
			}
		},
		created() {
			this.loadFontFace('Poppins')
			this.getProductCategoryList()
			this.getApplicationAreasList()
			this.getList()
			this.getGlobalPollShow()
		},
		methods: {
			getApplicationAreasList() {
				applicationAreasList().then(res => {
					let areas = []
					res.data.forEach(item => {
						areas.push({
							label: item.name,
							value: item.id,
						})
					})
					this.applicationAreass = areas
				})
			},
			getProductCategoryList() {
				productCategoryList().then(res => {
					let cate = []
					res.data.forEach(item => {
						let child = []
						item.children.forEach(itemC => {
							child.push({
								label: itemC.name,
								value: itemC.id
							})
						})
						cate.push({
							label: item.name,
							value: item.id,
							children: child
						})
					})
					this.categories = cate
				})
			},
			searchList() {
				this.exhibitParams.application_areas_id = this.searchApplicationAreas || ''
				this.exhibitParams.product_cate_id = this.searchCategoryId || ''
				this.exhibitParams.page = 1
				this.getList()
			},
			getList() {
				this.exhibitListLoading = true
				exhibitorsProductList(this.exhibitParams).then(res => {
					if (res.data.data) {
						if (this.exhibitParams.page > 1) {
							this.exhibitList = [...this.exhibitList, ...res.data.data]
						} else {
							this.exhibitList = res.data.data
							this.exhibitListLastPage = res.data.last_page
						}
					} else {
						this.showToast('系统繁忙，稍候再试')
					}
					this.exhibitListLoading = false
				})
			},
			onScrollToLower(e) {
				if (this.exhibitListLastPage === this.exhibitParams.page) {
					return
				}
				if (this.exhibitListLoading === true) {
					return
				}
				this.exhibitListLoading = true
				this.exhibitParams.page = this.exhibitParams.page+1
				this.getList()
			},
			onShare(e) {
				this.shareInfo = e.detail
				this.showShare = true
			},
			onShareAppMessage: function (res) {
				if (res.from === 'button') {
					if (this.shareInfo) {
						return this.shareInfo
					}
				}
				this.shareInfo = null
				this.showShare = false
				return {
					title: '慕尼黑上海电子生产设备展',
					path: '/pages/index/index',
					imageUrl: 'https://oss.starify.cn/prod/starify/up/0001018678/20241108/672da5a7dd374.png?x-oss-process=image/resize,w_600,h_600,limit_0,m_pad'
				}
			},
			onSelectDropdown(index) {
				['select1', 'select2', 'select3'].forEach(v => {
					if (v !== 'select' + index) {
						this.$refs[v].hideDropdown()
					}
				})
			},
			onClickExhibit(item) {
				this.navigateTo('/pages/exhibitor/exhibit-detail')
			},
			onSearch() {
				this.navigateTo('/pages/index/search?query=' + this.searchKeyword)
			}
		}
	}
</script>

<style lang="scss">
</style>