<template>
	<view :class="[{oh: !per}]">
		<view class="page">
			<view class="search-wrap">
				<uni-search-bar class="search" radius="30" placeholder="请输入" v-model="keyword" clearButton="auto" cancelButton="none" @confirm="initList" @clear="clearKeyword" />
				<uni-icons class="icon" custom-prefix="iconfont" type="icon-filter" size="22" color="#007eff" @click="typeVisible = true"></uni-icons>
			</view>
			
			<view class="wrap">
				<!-- <sideSelect :menus="menus" @menuClick="menuClick" /> -->
				<view class="list">
					<view class="item" v-for="(item, i) in list" :key="i" @click="toDetail(item)">
						<view class="pro-name">
							<image class="icon" :src="item.logoUrl ? item.logoUrl : Common.shopIcon"></image>
							{{ item.companyName }}
						</view>
						<view class="info">
							<view v-if="item.briefIntroduction" class="intro ell">{{ item.briefIntroduction }}</view>
							<view class="meta">{{ item.province }}-{{ item.city }}</view>
							<view class="order" @click.stop="toOrder(item)">我想采购</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="!per" class="per-mask">功能升级中，敬请期待...</view>
		
		
		<view :class="['type-popup', {active: typeVisible}]">
			<uni-icons class="close" type="closeempty" size="20" color="#757575" @click="closeType"></uni-icons>
			
			<template v-for="(item, i) in classifications">
				<template v-if="item.children.length">
					<view :key="i" class="title">{{ item.classificationName }}</view>
					<view class="types">
						<view v-for="(child, j) in item.children" :key="j" :class="['item', {active: active == child.id}]" @click="classificationClick(child)">{{ child.classificationName }}</view>
					</view>
				</template>
				<view v-else class="types">
					<view :class="['item', {active: active == item.id}]" @click="classificationClick(item)">{{ item.classificationName }}</view>
				</view>
			</template>
		</view>
		
		<order ref="order" />
	</view>
</template>

<script>
	import { selectEnterpriseCertificationList, treeStructureList } from '@/apis/modules/supplier.js'
	import Common from '@/config/common'
	export default {
		data() {
			return {
				Common,
				per: true, // 是否有权限
				teamId: uni.getStorageSync('teamId') || '',
				menus: [
					{
						id: '',
						name: '全部'
					},
					{
						id: 1,
						name: '服装'
					},
					{
						id: 2,
						name: '教具'
					},
					{
						id: 3,
						name: '课程'
					},
					{
						id: 4,
						name: '家具'
					},
					{
						id: 5,
						name: '设备'
					},
					{
						id: 6,
						name: '服务'
					},
					{
						id: 7,
						name: '其它'
					},
				],
				list: [],
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				keyword: '',
				page: 1,
				pageSize: 5,
				total: 0,
				curTab: 0,
				tabs: [],
				classifications: [],
				active: '',
				typeVisible: false,
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		// 上拉加载
		onReachBottom() {
			if (this.reachBottom >= 0) {
				this.reachBottom = 1
				this.status = 'loading'
				this.getList()
			}
		},
		onShow() {
			this.active = ''
			this.keyword = ''
			this.per = true
			this.typeVisible = false
			this.initList()
		},
		methods: {
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getCategory()
				this.getList()
			},
			// 列表
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				selectEnterpriseCertificationList({
					auditStatus: null,
					authenticationStatus: 2,
					pageNum: this.page,
					pageSize: this.pageSize,
					platformSource: 6,
					keyWord: this.keyword,
					supplierClassificationId: this.active,
				}).then(({ data }) => {
					this.list = this.reachBottom > 0 ? [...this.list, ...data.records] : data.records
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === data.total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
					uni.hideLoading()
				}).catch(e => {
					uni.hideLoading()
				})
			},
			// 获取分类
			async getCategory() {
				const { list } = await treeStructureList()
				this.classifications = list[0].children
			},
			// 分类点击回调
			classificationClick(item) {
				this.active = item.id
				this.typeVisible = false
				this.initList()
			},
			// 关闭分类弹框
			closeType() {
				this.typeVisible = false
				this.initList()
			},
			// keyword清除回调
			clearKeyword() {
				this.keyword = ''
				this.initList()
			},
			// 跳转学习详情
			toDetail(item) {
				this.$util.to('/other/supplierDetail/supplierDetail?teamId=' + item.teamId)
			},
			// 切换
			menuClick(tab) {
				this.curTab = tab.id
				this.initList()
			},
			// 我想采购
			toOrder(row) {
				const el = this.$refs.order
				el.remarks = ''
				el.getGoods(row.teamId)
				el.$refs.popup.open()
			}
		}
	}
</script>

<style scoped lang="scss">
.banner {
	display: block;
	width: 100%;
	border: 0;
}
.wrap {
	display: flex;
	align-items: flex-start;
}
.list {
	flex: 1;
	width: 100vw;
	overflow: hidden;
	.item {
		padding: 24rpx;
		margin: 16rpx 24rpx;
		background-color: #fff;
		border-radius: 16rpx;
	}
	.pro-name {
		display: flex;
		align-items: center;
		padding: 18rpx 0;
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		border-bottom: 1px solid #E6E8ED;
		.icon {
			width: 66rpx;
			min-width: 66rpx;
			height: 66rpx;
			margin-right: 20rpx;
			border-radius: 4px;
		}
	}
	.info {
		position: relative;
		padding: 12rpx 0;
	}
	.line {
		display: flex;
		padding: 12rpx 0;
	}
	.name {
		margin-right: 10rpx;
		font-size: 28rpx;
		color: #999;
	}
	.intro {
		max-width: 70%;
		margin: 10rpx 0;
		font-size: 24rpx;
		color: #777;
	}
	.meta {
		font-size: 26rpx;
		color: #333;
	}
	.ell-wrap {
		display: inline-flex;
		align-items: center;
	}
	.ell {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
	.order {
		position: absolute;
		top: 14rpx;
		right: 0;
		padding: 8rpx 28rpx;
		font-size: 28rpx;
		color: #fff;
		background-color: $uni-primary;
		border-radius: 20px;
	}
}
</style>
