<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="searchConfirm" />
			</view>
			
			<view class="banner">
				<image class="pic" src="@/static/image/index/banner.png" mode="widthFix"></image>
			</view>
			
			<ul class="tab">
				<li v-for="(tab, i) in tabs" :class="{active: curTab === tab.id}" @click="tabChange(tab)">{{ tab.name }}</li>
			</ul>
			<view class="list">
				<li v-for="(item, i) in list" :key="i" @click="toDetail(item)">
					<view class="pro-name">
					<image class="icon" src="@/static/image/info-bg.jpg"></image>
						{{ item.productName }}
					</view>
					<view class="info">
						<view class="line">
							<text class="name">产品简介：</text>
							<view class="val ell-wrap">
								<view class="ell">{{ item.productIntroduction }}</view>
							</view>
						</view>
						<view class="line">
							<text class="name">产品类型：</text>
							<text class="val">{{ item.typeName }}</text>
						</view>
					</view>
				</li>
			</view>
		</view>
		<view v-if="!per" class="per-mask">功能升级中，敬请期待...</view>
	</view>
</template>

<script>
	import { getUserRolesPermissionMenu } from '@/apis/modules/user.js'
	import { tagsList, listOfGoods, shoppingCartList } from '@/apis/modules/product.js'
	import { getTeamsByAccountId } from '@/apis/modules/client.js'
	export default {
		data() {
			return {
				per: true, // 是否有权限
				teamId: uni.getStorageSync('teamId') || '',
				list: [],
				keyword: '',
				tags: [],
				hotProducts: [],
				offcialProducts: [],
				total: 0,
				curTab: 0,
				tabs: [
					{
						name: '全部',
						id: 0
					},
					{
						name: '论坛',
						id: 1
					}
				],
			}
		},
		onShow() {
			this.keyword = ''
			this.per = true
			this.getInfo()
			this.getShopCart()
		},
		methods: {
			// 初始化权限
			initRole() {
				if (!uni.getStorageSync('auth').includes('首页')) {
					this.per = false
				}
				this.getTags()
				this.getProducts()
			},
			// 查询当前角色权限
			getAuth() {
				uni.getStorageSync('token') && getUserRolesPermissionMenu({
					teamId: this.list.find(e => e.teamId == this.teamId).partnerClassificationId,
					platformId: 4
				}).then(({ permissionMenu }) => {
					uni.hideLoading()
					const auth = []
					// 生成权限数组
					const generateAuth = (list, parent) => {
						list.map(e => {
							const name = `${parent ? parent + ':' : ''}${e.name}`
							auth.push(name)
							generateAuth(e.children, name)
						})
					}
					generateAuth(permissionMenu[0].children, '')
					uni.setStorageSync('auth', auth)
					this.$forceUpdate()
					this.initRole()
				}).catch(e => {
					uni.hideLoading()
					uni.setStorageSync('auth', [])
					this.initRole()
				})
			},
			// 获取团队
			getInfo() {
				uni.showLoading({
					title: '加载中'
				})
				getTeamsByAccountId().then(({ data }) => {
					data.map(e => {
						const n = e.partnerClassificationList
						e.id = n.id
						// parnerId是商务经理id，teamId则是下面这个，其他地方要用的话直接uni.getStorageSync('team').partnerId去使用
						e.teamId = e.isTeam == 1 ? +e.partnerClassificationId : n.id
						e.partnerClassificationName = n.partnerClassificationName
						delete e.partnerClassificationList
					})
					if (data.length) {
						/**
						 * @description 如果是第一次进，则默认选中第一个团队，并把该团队的信息存入缓存
						 * 或者团队列表里没有该id，则说明超管已经被转让，也需要重新选中团队
						 */
						if (!uni.getStorageSync('team') || !data.find(e => e.teamId == this.teamId)) {
							this.teamId = data[0].teamId
							uni.setStorageSync('team', data[0])
						}
					} else {
						// 如果没有团队，则退出登录
						uni.hideLoading()
						uni.clearStorageSync()
						uni.navigateTo({
							url: '../login/login'
						})
					}
					
					this.list = data
					this.getAuth()
				}).catch(e => {
					uni.hideLoading()
				})
			},
			// 热门标签
			getTags() {
				tagsList().then(res => {
					this.tags = res.tagsList.slice(0, 8)
				}).catch(e => {})
			},
			// 获取购物车数量
			getShopCart() {
				shoppingCartList({
					pageNum: 1,
					pageSize: 1000,
				}).then(({ data }) => {
					this.total = data.total
				}).catch(e => {})
			},
			// 产品
			getProducts() {
				// 热销产品
				listOfGoods({
					pageNum: 1,
					pageSize: 5,
					sort: 1,
					isShelves: 0,
					hotTag: 1
				}).then(({ page }) => {
					page.records.forEach(e => {
						e.productIntroduction = this.$util.removeTag(e.productIntroduction)
					})
					this.hotProducts = page.records
				}).catch(e => {})
				
				// 官方推荐
				listOfGoods({
					pageNum: 1,
					pageSize: 5,
					sort: 0,
					isShelves: 0,
					hotTag: 1,
					selection: 1
				}).then(({ page }) => {
					page.records.forEach(e => {
						e.productIntroduction = this.$util.removeTag(e.productIntroduction)
					})
					this.offcialProducts = page.records
				}).catch(e => {})
			},
			// 模糊查询回调
			searchConfirm(e) {
				e.value && this.$util.to(`/order/products/products?keyword=${e.value}`)
			},
			// 跳转热门标签
			toHot(item) {
				this.$util.to(`/order/products/products?tagId=${item.tagsId}&tagsName=${item.tagsName}`)
			},
			// 跳转学习
			toProduct() {
				this.$util.to('/order/products/products')
			},
			// 跳转学习详情
			toDetail(item) {
				this.$util.to('/order/supplierDetail/supplierDetail?id=')
			},
			// tab切换
			tabChange(tab) {
				this.curTab = tab.id
				// this.initList()
			},
		}
	}
</script>

<style scoped lang="scss">
.banner {
	position: relative;
	.pic {
		width: 100%;
	}
}
.list {
	li {
		padding: 24rpx;
		margin: 16rpx 24rpx;
		background-color: #fff;
		border-radius: 16rpx;
	}
	.pro-name {
		display: flex;
		align-items: center;
		padding: 18rpx 0;
		font-size: 30rpx;
		font-weight: 600;
		color: #333;
		border-bottom: 1px solid #E6E8ED;
		.icon {
			width: 58rpx;
			min-width: 58rpx;
			height: 58rpx;
			margin-right: 20rpx;
			border-radius: 4px;
		}
	}
	.info {
		padding: 12rpx 0;
	}
	.line {
		display: flex;
		padding: 12rpx 0;
	}
	.name {
		margin-right: 10rpx;
		font-size: 28rpx;
		color: #999;
	}
	.val {
		max-width: 70%;
		font-size: 28rpx;
		color: #333;
	}
	.ell-wrap {
		display: inline-flex;
		align-items: center;
	}
	.ell {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
	.toggle {
		margin-left: 10rpx;
		white-space: nowrap;
		font-size: 24rpx;
		color: #0e92ef;
	}
}
</style>
