<template>
	<page-layout class="exhibit-index exhibitor-index">
		<nav-bar title="展品信息" @init="onInitNavbar"></nav-bar>
		<u-scroll-view>
			<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="select-box">
          <view class="select-head">
            <view class="select-title item-title">
              筛选
            </view>
            <view class="select-search-btn" @click="searchList()">搜索</view>
          </view>
          <view class="select-body">
            <view class="select-list">
              <view class="select-module">
                <view class="select-input" @click="()=>{activeName1 = !activeName1;activeName2=false;}">展品分类</view>
                <view class="select-option-module" :class="{'active': activeName1}">
                  <u-scroll-view :tabbar-conflict="true">
                    <u-select title="展品分类" v-model="searchCategoryId" :data="categories" uId="dropdown2" :children="true"
                              @change-event="()=>{exhibitParams.page=1; searchList()}"></u-select>
                  </u-scroll-view>
                </view>
              </view>
              <view class="select-module">
                <view class="select-input" @click="()=>{activeName2 = !activeName2;activeName1=false;}">应用领域</view>
                <view class="select-option-module" :class="{'active': activeName2}">
                  <u-scroll-view :tabbar-conflict="true">
                    <u-select title="应用领域" v-model="searchApplicationAreas" :data="applicationAreass" uId="dropdown3"
                              @change-event="exhibitParams.page=1; searchList()"></u-select>
                  </u-scroll-view>
                </view>
              </view>
            </view>
          </view>

        </view>
				<view class="ad-space" v-if="adInfo && adInfo.ad_file" @tap="navigateToAdLink(adInfo.ad_link)">
					<image :src="adInfo.ad_file" mode="widthFix" />
				</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"
							@updateItemValue="updateItemValue" />
					</template>
				</view>
				<u-pagination :page="exhibitParams.page" :pageSize="exhibitParams.page_size" :total="total" @page-change="handlePageChange()"
					v-if="total>0"></u-pagination>
				<disclaimer-text></disclaimer-text>
			</view>
		</u-scroll-view>
		<float-button></float-button>
		<van-dialog id="van-dialog" />
	</page-layout>
</template>

<script>
	import NavBar from '@/components/layout/nav-bar'
	import USelect from '@/components/common/u-select/index.vue'
	import UScrollView from '@/components/common/u-scroll-view'
	import USearch from '@/components/common/u-search'
	import UDropdownSelect from '@/components/common/u-dropdown-select'
	import UPagination from '@/components/common/u-pagination/index.vue'
	import ExhibitItem from '@/pages/exhibitor/components/exhibit-item.vue'
	import floatButton from "@/components/layout/float-button"
	import {
		exhibitorsProductList,
		productCategoryList,
		applicationAreasList,
		globalPollShow
	} from '@/api/exhibitor'
	import {
		getAdInfo
	} from '@/api'
	import DisclaimerText from '@/components/disclaimer-text/index.vue'
	import PageLayout from "@/components/layout/page-layout";
	import vanCollapse from '@/wxcomponents/vant/collapse/index'
	import vanCollapseItem from '@/wxcomponents/vant/collapse-item/index'
  import {getToken} from "@/utils/auth";

	export default {
		options: {
			// styleIsolation: 'shared'
		},
		components: {
			PageLayout,
			NavBar,
			UScrollView,
			USearch,
			UDropdownSelect,
			ExhibitItem,
			DisclaimerText,
			USelect,
			UPagination,
			floatButton,
			vanCollapse,
			vanCollapseItem
		},
		computed: {},
		data() {
			return {
				activeName: '',
				searchCategoryId: '',
        activeName1: false,
        activeName2: false,
				searchApplicationAreas: '',
				scrollviewHeight: 0,
				searchKeyword: '',
				exhibitParams: {
					page: 1,
					page_size: 10,
					keyword: '',
					product_cate_id: '',
					application_areas_id: ''
				},
				exhibitList: [],
				exhibitListLoading: false,
				exhibitListLastPage: 1,
				categories: [],
				applicationAreass: [],
				pollShow: {
					exhibitors_poll_show: 0,
					product_poll_show: 0
				},
				total: 0,
				baseUrl: this.$store.getters.baseApi,
				adInfo: {
					ad_file: ''
				}
			}
		},
		created() {
			this.getProductCategoryList()
			this.getApplicationAreasList()
			this.getGlobalPollShow()
			this.getAdData()
      this.getList()
      uni.$on('refreshData', (val) => {
        if (val === 'product') {
          this.searchList()
        }
      })
		},
		methods: {
			onChange(event) {
				this.activeName = event
			},
			handlePageChange(page) {
				this.exhibitParams.page = page
        this.searchList()
				/*exhibitorsProductList(this.exhibitParams).then(res => {
					if (res.data.data) {
						this.exhibitList = res.data.data
					} else {
						this.showToast('系统繁忙，稍候再试')
					}
				})*/
			},
			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.showLoading()
				let product_cate_ids = ''
				let application_areas_ids = ''
				if (this.searchCategoryId.length === 0) {
					product_cate_ids = 'product_cate_id='
				} else {
					for (let i = 0; i < this.searchCategoryId.length; i++) {
						if (i === this.searchCategoryId.length - 1) {
							product_cate_ids = product_cate_ids + 'product_cate_id[]=' + this.searchCategoryId[i]
						} else {
							product_cate_ids = product_cate_ids + 'product_cate_id[]=' + this.searchCategoryId[i] + '&'
						}
					}
				}
				if (this.searchApplicationAreas.length === 0) {
					application_areas_ids = 'application_areas_id='
				} else {
					for (let i = 0; i < this.searchApplicationAreas.length; i++) {
						if (i === this.searchApplicationAreas.length - 1) {
							application_areas_ids = application_areas_ids + 'application_areas_id[]=' + this.searchApplicationAreas[i]
						} else {
							application_areas_ids = application_areas_ids + 'application_areas_id[]=' + this.searchApplicationAreas[
								i] + '&'
						}
					}
				}

				let url = this.baseUrl + '/api/exhibitors/product/list?page='+this.exhibitParams.page+'&page_size='+ this.exhibitParams.page_size+'&keyword=&' + product_cate_ids + '&' +
					application_areas_ids
				uni.request({
					url: url,
					method: 'GET',
          header: {
            'token': getToken()
          },
					success: (res) => {
						// 处理返回的数据
						this.exhibitList = res.data.data
						this.total = res.data.total
						this.exhibitListLastPage = res.data.last_page
						this.hideLoading()
					},
					fail: (err) => {
						console.error('请求失败', err);
					}
				});
			},
			getList() {
				this.showLoading()
				exhibitorsProductList(this.exhibitParams).then(res => {
					if (res.data.data) {
						this.exhibitList = res.data.data
						this.total = res.data.total
						this.exhibitListLastPage = res.data.last_page
					} else {
						this.showToast('系统繁忙，稍候再试')
					}
					this.exhibitListLoading = false
					this.hideLoading()
				})
			},
			// 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.$emit('share',e)
			},
			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)
			},
			getGlobalPollShow() {
				globalPollShow().then(res => {
					this.pollShow = res.data
				})
			},
			updateItemValue(e) {
				this.exhibitList.forEach((item) => {
					if (item.id === e.id) {
						item[e.key] = e.value
					}
				})
			},
			navigateToAdLink(href) {
				this.navigateTo(href)
			},
			getAdData() {
				getAdInfo({
					number: "ExhibitorProductTop001"
				}).then(res => {
					this.adInfo = res.data[0]
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
