<template>
	<view class="container">
		<scroll-view @scrolltolower="lowerBottom" scroll-y="true" class="scrollHeight" @scroll="scroll"
			:scroll-top="scrollTop">
			<image class="background-image" src="/static/images/tabBar/527@2x.png" style="width: 100%;height: 320rpx;"
				mode="scaleToFill"></image>
			<view>
				<view style="display: flex;align-items: center;">
				<!-- 	<u-search placeholder="请输入商品名称" bgColor="#fff" readonly :showAction="false"
						searchIconColor='#E75F37' :searchIconSize='24' margin='20rpx' disabled
						@click="goSearch"></u-search> -->
						<u-search placeholder="请输入商品名称" bgColor="#fff" searchIconColor='#E75F37' :actionStyle="{color:'#FFF'}"
							:searchIconSize='24' margin='20rpx'  disabled @click="goSearch"></u-search>
				</view>

			</view>
			<view style="padding: 0 20rpx 20rpx 20rpx;">
				<view style="border-radius: 10rpx;overflow: hidden;" v-if="list3.length>0">
					<uni-swiper-dot :info="list3" :current="dotCurrent" mode="round" :dots-styles="dotsStyles">
						<swiper circular :autoplay="true" :interval="2000" :duration="500" @change="swiperChange">
							<swiper-item v-for="(item,index) in list3" :key="index">
								<image mode="scaleToFill" :src="item" @error="imageError"
									style="width: 100%;height: 100%;">
								</image>
							</swiper-item>
						</swiper>
					</uni-swiper-dot>
				</view>
				<view class="card" v-if="brandList.length >0" style="padding: 20rpx;">
					<uni-grid :column="5" :show-border="false" :square="false" :highlight="false">
						<uni-grid-item v-for="(item ,index) in brandList" :key="index">
							<view class="brandImg" @click="click(index)">
								<image :src="item.brandFilesList[0].url" mode="aspectFit"
									style="width:88rpx;height: 88rpx;border-radius: 100%;" />
								<text class="text">{{item.cname}}</text>
							</view>
						</uni-grid-item>
						<uni-grid-item v-if="brandList.length >= 9 && gengduofalse">
							<view class="brandImg" @click="Moreprojectsfun">
								<image src="/static/images/view/buyGoods/2842x.png" mode="aspectFit"
									style="width:88rpx;height: 88rpx;border-radius: 100%;" />
								<text class="text">更多品牌</text>
							</view>
						</uni-grid-item>
						<uni-grid-item v-if="brandList.length >= zsbrandList.length && zsbrandList.length > 8">
							<view class="brandImg" @click="shohuifun">
								<image src="../../static/images/view/buyGoods/fanhui.png" mode="aspectFit"
									style="width:88rpx;height: 88rpx;border-radius: 100%;" />
								<text class="text">回收品牌</text>
							</view>
						</uni-grid-item>
					</uni-grid>
				</view>
				<view class="card">
					<view style="padding: 16rpx;border-top: 2px #F1F1F1 solid;;">
						<uni-grid :column="2" :show-border="false" :square="false" :highlight="false"
							v-if="dataList.length > 0">
							<uni-grid-item v-for="(item ,index) in dataList" :key="index">
								<view class="goodsImg" @click="choice(item,index)">
									<image v-if="item.billType == 0" src="/static/images/tabBar/xsd2x.png"
										mode="aspectFit" class='imgIcon' />
									<image v-if="item.billType == 1" src="/static/images/tabBar/crd2x.png"
										mode="aspectFit" class='imgIcon' />
									<image :src="item.url" mode="aspectFit"
										style="width:300rpx;height:300rpx;border-radius:20rpx;border: 2rpx #E7E7E7 solid;" />
									<text class="text">{{item.cname}}</text>
									<text
										style="color:#E75F37;width: 300rpx;font-weight: bold;font-size: 32rpx;">￥{{checkStatus == '通过'?item.mallPrice:'***'}}</text>
								</view>
							</uni-grid-item>
						</uni-grid>

						<u-empty v-if="dataList.length == 0" mode="list" icon="/static/images/tabBar/emptylist.jpg" />
					</view>

				</view>
				<u-loadmore v-if="page.total !== 0 && dataList.length != 0" :status="status" />
				<u-back-top :scroll-top="old.scrollTop" @tap="goTop" bottom="300rpx"></u-back-top>
			</view>
		</scroll-view>
	</view>
</template>
<script>
	import {
		appList,
		userDept,
		validationCorpEmpty,
		brandDescList,
		brandDescDetail
	} from '@/api/tabBar/home.js'
	import {
		shoppingCartList
	} from '@/api/tabBar/shoppingCart.js'
	import {
		listAll
	} from '@/api/tabBar/classification.js'
	import {
		details,
		nameDetail
	} from '@/api/views/personalInformation/index.js'
	import {
		activityList
	} from '@/api/tabBar/activity.js'
	export default {
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				activeStyle: {
					color: '#E75F37',
					fontWeight: 'bold',
					fontSize: '28rpx'
				},
				inactiveStyle: {
					color: '#797979',
					fontSize: '28rpx'
				},
				dotsStyles: {
					backgroundColor: 'rgba(0, 0, 0, .1)',
					border: '1px #fff solid',
					color: '#fff',
					selectedBackgroundColor: '#fff',
					selectedBorder: '1px #fff solid'
				},
				dotCurrent: 0,
				show: false,
				search: {},
				page: {
					total: 0,
					size: 10,
					current: 1
				},
				status: 'loadmore',
				dataList: [],
				list3: [],
				brandList: [],
				// 静音棉
				originalFactory: 0,
				// 自修补
				selfRecovery: 0,
				// 防爆
				explosionProof: 0,
				dianjiqielist: [{
						name: '全部',
					},
					{
						name: '静音棉',
					},
					{
						name: '自修补',
					},
					{
						name: '防爆',
					}
				],
				groupModel: null,
				PPindex: null,
				// 品牌
				zsbrandList: [],
				brandDescfor: 7,
				gengduofalse: true,
				// 审核状态
				checkStatus: '',
				enableOrNot: '',
				salesmanName: '', // 业务员姓名
				phoneNumber: '', // 业务员联系电话
				salesmanShow: false, // 联系业务员弹窗
			}
		},
		created() {
			activityList({
				current: 1,
				size: 10,
				status: 1
			}).then(res => {
				uni.setTabBarBadge({
					index: 2, // tabIndex，tabBar的哪一项，从0开始
					text: res.data.total.toString() // 显示的文本，超过 99 显示成 “…”
				})
			})
			shoppingCartList().then(res => {
				let num = 0
				res.data.forEach(e => {
					num = num + e.list.length
				})
				uni.setTabBarBadge({
					index: 3, // tabIndex，tabBar的哪一项，从0开始
					text: num.toString() // 显示的文本，超过 99 显示成 “…”
				})
			})
			if (uni.getStorageSync('cname')) {
				this.$set(this.search, 'cname', JSON.parse(JSON.stringify(uni.getStorageSync('cname'))))
				uni.removeStorageSync('cname')
			}
			// 获取审核状态
			details().then(res => {
				this.checkStatus = res.data.checkStatus
				this.enableOrNot = res.data.enableOrNot
				uni.setStorageSync('checkStatus', res.data.checkStatus);
				uni.setStorageSync('enableOrNot', res.data.enableOrNot);
				// 查询业务员详情拿取手机号
				this.salesmanName = res.data.salesmanName
				if (!this.phoneNumber) {
					nameDetail({
						id: res.data.salesmanId
					}).then(res => {
						this.phoneNumber = res.data.phone
					})
				}

			})
			// 轮播图
			brandDescList({
				type: 'LBT',
				whetherIntegral: "1"
			}).then(res => {
				brandDescDetail({
					id: res.data.records[0].id
				}).then(res => {
					console.log(res, 193);
					// this.list3 = res.data.brandFilesList
					this.list3 = res.data.brandFilesList.map(item => {
						return item.url
					})
				})
			})
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			//获取所有品牌
			listAll({
				type: 'PP',
				enableOrNot: '1',
				whetherIntegral: '1'
			}).then(res => {
				this.zsbrandList = []
				this.brandList = []
				// this.zsbrandList = res.data
				this.zsbrandList = res.data.sort(this.compare('sort'))
				if (this.zsbrandList.length <= 8) {
					this.gengduofalse = false
					this.brandList = this.zsbrandList
				} else {
					for (let i in res.data) {
						if (i < this.brandDescfor) {
							this.brandList.push(res.data[i])
						}
					}
				}
			})
			this.page = {
				total: 0,
				size: 10,
				current: 1
			}
			this.dataList = []
			this.onSearch()
			uni.hideLoading();
		},
		methods: {
			goTop(e) {
				// 解决view层不同步的问题
				this.scrollTop = this.old.scrollTop
				this.$nextTick(() => {
					this.scrollTop = 0
					this.old.scrollTop = 0
				});
			},
			scroll(e) {
				this.old.scrollTop = e.detail.scrollTop
			},
			swiperChange(e) {
				this.dotCurrent = e.detail.current;
			},
			goSearch() {
				this.$emit('goSearch')
			},
			doLoadMore() {
				console.log(3333)
			},
			scrolltolower() {
				console.log(111111)
				uni.$u.debounce(this.queryList(), 500)
			},
			lowerBottom() {
				console.log(22222)
				this.status = 'loading'
				if (this.dataList.length < this.page.total) {
					// if (this.dataList.length < 20) {
					this.page.current++
					this.onSearch()
				} else {
					this.status = 'nomore'
				}
			},
			tabbarClick(tab) {
				// uni.$u.route('/pages/integralMall/'+tab);	
				uni.$u.route({
					url: '/pages/integralMall/' + tab
				})
			},
			// 点击联系业务员
			clickCall() {
				uni.makePhoneCall({
					phoneNumber: this.phoneNumber,
					success: function() {
						console.log('拨打电话成功');
						this.salesmanShow = false
					},
					fail() {
						console.log('打电话失败了');
						this.salesmanShow = false
					}
				})
			},
			// 数组排序 property:根据什么属性排序
			compare(property) {
				return function(a, b) {
					var value1 = a[property]
					var value2 = b[property]
					return value1 - value2
				}
			},
			// 更多项目盒子
			Moreprojectsfun() {
				this.brandDescfor += 4
				this.brandList = []
				console.log(123);
				for (let i in this.zsbrandList) {
					if (i < this.brandDescfor) {
						this.brandList.push(this.zsbrandList[i])
					}
				}
				if (this.brandDescfor >= this.zsbrandList.length) {
					this.gengduofalse = false
				}
			},
			// 收回项目
			shohuifun() {
				this.brandDescfor = 7
				this.brandList = []
				for (let i in this.zsbrandList) {
					if (i < this.brandDescfor) {
						this.brandList.push(this.zsbrandList[i])
					}
				}
				this.gengduofalse = true
				// if (this.brandDescfor >= this.zsbrandList.length) {
				// 	this.gengduofalse = false
				// }
			},

			groupChange(e) {
				console.log(e, 228);
				if (e.name == '静音棉') {
					this.originalFactory = 1
					this.selfRecovery = 0
					this.explosionProof = 0
				} else if (e.name == '自修补') {
					this.originalFactory = 0
					this.selfRecovery = 1
					this.explosionProof = 0
				} else if (e.name == '防爆') {
					this.originalFactory = 0
					this.selfRecovery = 0
					this.explosionProof = 1
				} else {
					this.originalFactory = 0
					this.selfRecovery = 0
					this.explosionProof = 0
				}
				this.page.current = 1
				this.dataList = []
				this.onSearch()

			},
			// 点击筛选条件
			dianjiqiehuan(name) {
				if (this[name] == 0) {
					this[name] = 1
				} else {
					this[name] = 0
				}
				this.click()
			},
			// 获取列表数据
			onSearch() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				let obj = {}
				if (this.originalFactory) {
					obj.originalFactory = 1
				}
				if (this.selfRecovery) {
					obj.selfRecovery = 1
				}
				if (this.explosionProof) {
					obj.explosionProof = 1
				}
				appList({
					size: this.page.size,
					current: this.page.current,
					...obj,
					...this.search,
					whetherIntegral: "1"
				}).then(res => {
					this.dataList = this.dataList.concat(res.data.records)
					this.page.total = res.data.total
					if (this.dataList.length == res.data.total) {
						this.status = 'nomore'
					}
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
				})
			},
			close() {
				this.show = false
				this.search.brandId = ''
				this.search.brandName = ''
			},
			// 原厂点击差号
			yuanchangclose(type) {
				if (type == 'originalFactory') {
					this.originalFactory = false
				} else if (type == 'selfRecovery') {
					this.selfRecovery = false
				} else {
					this.explosionProof = false
				}
				this.click()
			},

			click(name) {
				if (name != undefined) {
					this.page = {
						total: 0,
						size: 10,
						current: 1,
					}
					this.dataList = []
					if (this.PPindex == name) {
						this.PPindex = null
						delete this.search.brandId
						delete this.search.brandNmae
					} else {
						this.PPindex = name

						this.search = {
							...this.search,
							brandId: this.brandList[this.PPindex].id,
							brandNmae: this.brandList[this.PPindex].cname,
						}
					}

				}
				this.show = true

				this.onSearch()
			},
			searchButton(value) {
				this.dataList = []
				this.page = {
					total: 0,
					size: 10,
					current: 1
				}
				this.search = {
					...this.search,
					cname: value,
				}
				// this.onSearch()
				uni.setStorageSync('homeSearch', this.search);
				this.search.cname = null
				// uni.switchTab({
				// 	url: '/pages/integralMall/classification',
				// })
				this.$emit('goTab', 1)
			},
			//打开明细
			choice(item, index) {
				if (this.checkStatus != '通过') {
					uni.showToast({
						title: "当前用户未授权,请联系客服",
						icon: "none",
						mask: true
					});
					return
				}
				uni.$u.route('/pages/integralMall/buyGoods/commodityDetails', {
					id: item.id
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	.scrollHeight {
		height: calc(100vh - calc(50px + env(safe-area-inset-bottom)));
	}
	.card {
		background-color: #fff;
		border-radius: 10rpx;
		overflow: hidden;
		margin: 12rpx 0;
	}

	.carBtn {
		display: flex;
		align-items: center;
		justify-content: center;
		color: #E75F37;
		font-weight: bold;
		width: 25%;
		text-align: center;
		background-color: #fff;
		height: 64rpx;
		border-radius: 36rpx;
		margin-right: 20rpx;
	}

	.brandImg {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin: 16rpx 0;

		.text {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			width: 140rpx;
			text-align: center;
			font-size: 24rpx;
			color: #797979;
		}
	}

	.goodsImg {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin: 16rpx 0;

		.text {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			width: 300rpx;
			text-align: center;
			font-size: 24rpx;
			color: #797979;
		}
	}

	.container {
		position: relative;
	}

	.background-image {
		position: absolute;
		z-index: -1;
		/* 将图片置于组件后面 */
	}

	.imgIcon {
		position: absolute;
		width: 100rpx;
		height: 35rpx;
		top: 16rpx;
		right: 36rpx;
	}
</style>