<template>
	<view>
		<!-- <input class="uni-input" focus placeholder="自动获得焦点" /> -->
		<view v-if="!isShowSearch">
			<view class="top-warp">
				<view class="search">
					<view class="place">
						<selectAddress @selectAddress='selectAddress' :searchPlace='searchPlace'>
							<u-icon :slot="'icon'" name="arrow-down" color="#333333" size="17" bold
								style='margin-left: 6rpx;'></u-icon>
						</selectAddress>
						<!-- 					<picker @change="bindPickerChange" :value="multiIndex" :range="multiArray" range-key='label'
							mode="multiSelector" @columnchange='columnchange'>
							<view class="text">{{searchPlace}}</view>
						</picker> -->

					</view>
					<u-search placeholder="请输入作物名称或用户名称" v-model="keyword" :input-style='inputStyle'
						:show-action="false" disabled @click='toSearch'>
					</u-search>
					<view class="del" @click.stop='delIcon' v-if="keyword">
						<u-icon name="close-circle" size="40" color="#999"></u-icon>
					</view>
				</view>
				<me-tabs v-model="tabIndex" :tabs="tabs" @change="tabChange"></me-tabs>
			</view>
			<mescroll-body ref="mescrollRef" @init="mescrollInit" top="180" @down="downCallback" :up="upOption"
				@up="upCallback" @emptyclick="emptyClick" :bottombar="false">
				<!-- 数据列表 -->
				<view class="list" v-if="goods.length!=0">
					<view class="list-item" v-for="(item,index) in goods">
						<view class="row1 flex jcsb alc">
							<view class="left flex alc">
								<image
									:src="item.commonUser.avatarUrl?item.commonUser.avatarUrl:'../../static/img/myimg/YongHu@3x.png'"
									mode="widthFix" class="head-img"></image>
								<view class="">
									<view class="title">
										{{item.publisher}}
									</view>
									<view class="bottom" style="margin-top: 6rpx;">
										<text class="color1" v-if="item.tranType=='采购'">采购</text>
										<text class="color2" v-if="item.tranType=='销售'">销售</text>
										<text class="color3">{{item.remark3?$helper.changeTime(item.remark3):''}}</text>
									</view>
								</view>
							</view>
							<view class="right"
								v-if="item.followed==1&&item.commonUser&&item.commonUser.userName!=userInfo.userName"
								@click="follow(item,1)">
								<text class="case" style="color:#878C9C;">已关注</text>
							</view>
							<view class="right"
								v-else-if='item.followed!=1&&item.commonUser&&item.commonUser.userName!=userInfo.userName'
								@click="follow(item,2)">
								<u-icon name="plus" color="#333333" size="17" bold></u-icon><text class="case">关注</text>
							</view>

						</view>
						<view class="row2">
							<mote-lines-divide :dt="item.publishingContent" :line="8" expandText="展开" foldHint="收起" />
						</view>
						<view class="row3">
							<image :src="item1" mode="aspectFill" v-for="(item1,index) in item.imgList"
								@tap="previewImage(item,index)" class="img">
							</image>
							<image v-if='item.imgList&&item.imgList.length%3==1||item.imgList&&item.imgList.length%3==2'
								class="img" style='background:#fff;'></image>
							<image v-if='item.imgList&&item.imgList.length%3==1' class="img" style='background:#fff;'>
							</image>
							<!-- 	<u-row justify="space-between" gutter="10">
								<u-col span="4">
									<view class="demo-layout bg-purple">
										<image src="../../static/img/liangmai/bg.png" mode="aspectFill"
											style="width: 100%;height: 100%;"></image>
									</view>
								</u-col>
								<u-col span="4">
									<view class="demo-layout bg-purple-light">
										<image src="../../static/img/liangmai/bg.png" mode="aspectFill"
											style="width: 100%;height: 100%;"></image>
									</view>
								</u-col>
								<u-col span="4">
									<view class="demo-layout bg-purple">
										<image src="../../static/img/liangmai/bg.png" mode="aspectFill"
											style="width: 100%;height: 100%;"></image>
									</view>
								</u-col>
							</u-row> -->
						</view>
						<view class="row4">
							<u-icon name="map-fill" color="#AFB3BF" size="28" bold></u-icon>
							<text class="text">{{item.placeDelivery}}</text>
						</view>
					</view>

				</view>

				<!-- <good-list :list="goods"></good-list> -->
			</mescroll-body>
			<view class="add" @click="release">
				<u-icon name="plus" color="#fff" size="34" bold></u-icon>
			</view>
			<u-modal v-model="show" content="确定不在关注？" :show-title='false' :show-cancel-button='true'
				:content-style="contentStyle"  @confirm="cancelFollow" @cancel="show=false">
			</u-modal>
		</view>
		<!-- <search v-show="isShowSearch" @searchVal='searchVal'></search> -->
		<u-modal v-model="isShowAlert" :title-style="{fontSize: '18px',fontWeight:'500'}"
			:content-style="{fontSize: '14px',fontWeight:'400'}" confirm-color='#22C572' confirm-text='确定' title='提示'
			 content="尚未登录,是否立即登录" @confirm="alertBtn" @cancel="cancelClick"></u-modal>
	<!-- 	<u-modal v-model="isSHowReleaseAlert" :title-style="{fontSize: '18px',fontWeight:'500'}"
			:content-style="{fontSize: '14px',fontWeight:'400','text-align':'left'}" confirm-color='#22C572'
			confirm-text='我知道了' title='温馨提示' content="交易信息为用户自由发布，正式签订合同前切勿进行任何付款操作，以免给您的财产造成损失。"
			@confirm="releaseAlertbtn"></u-modal> -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MoteLinesDivide from "@/components/text-over-flow"
	import selectAddress from "@/components/selectAddress.vue"
	import search from "@/components/search.vue"
	import {
		apiGoods
	} from "@/api/mock.js"
	import {
		mapState
	} from 'vuex';
	export default {
		components: {
			MoteLinesDivide,
			selectAddress,
			search
		},
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				searchPlace: '全国',
				selectType: '',
				isSHowReleaseAlert: null,
				canReset: false,
				focus:false,
				selectPlace: '',
				mescroll: null,
				isShowAlert: false,
				contentStyle: {
					"font-weight": 700
				},
				isShowSearch: false,
				selectItem: {
					followInformation:{
						id:''
					}
				},
				show: false,
				keyword: '',
				inputStyle: {
					// "padding-left": '30rpx'
				},
				content: '',
				upOption: {
					// page: {
					// 	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						// icon: '/static/empty.png',
						tip: '暂无内容', // 提示
						// btnText: '去看看'
					},
					textNoMore: '没有更多了~',
					textColor: '#878C9C',
					toTop: {
						src: "",
					},
				},
				goods: [], //列表数据
				tabs: [{
					name: '全部',
					type: 'xx'
				}, {
					name: '采购',
					type: 'xx'
				}, {
					name: '销售',
					type: 'xx'
				}, {
					name: '关注',
					type: 'xx'
				}],
				
				tabbar: true,
				windowHeight: '',
				tabIndex: 0 // tab下标
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'clientId']),
		},
		onLoad() {
		    // uni.getSystemInfo({
		    //     success: (res)=> {
		    //         this.windowHeight = res.windowHeight;
		    //     }
		    // });    
		    // uni.onWindowResize((res) => {
		    //     if(res.size.windowHeight < this.windowHeight){
		    //         uni.hideTabBar()
		    //     }else{
		    //         uni.showTabBar()
		    //     }
		    // })
		},
		onShow() {
			if(uni.getStorageSync('jiaoyikeyword')){
				this.keyword=uni.getStorageSync('jiaoyikeyword')
			}else{
				this.keyword=''
			}
			let _tl = uni.getStorageSync('transactionLocation')
			if (_tl) {
				this.searchPlace = _tl.showVal
				this.selectPlace = _tl.address
			} else {

			}
			this.isSHowReleaseAlert = getApp().globalData.userRelease
			// console.log("this.isSHowReleaseAlert", this.isSHowReleaseAlert)
			uni.showTabBar()
			uni.hideKeyboard()
			var userInfo = uni.getStorageSync("userInfo")
			var that = this
			console.log("userInfo", userInfo)
			// setTimeout(function() {
			// 	that.$api.doRequest('get', '/appVersion/test', {
			// 		userId: "7e83070d05fc4d50aaa46e00a3ee03d8"
			// 	}).then(res => {

			// 	})
			// }, 500);
			uni.removeTabBarBadge({
				index: 4
			})
			this.$api.doRequest('get', '/newNoticeTask/query/noticeTasks').then(res => {
				if (res.data.data) {
					let name = 'myTip';
					let value = res.data.data.total;
					that.$store.commit('$uStore', {
						name,
						value
					});
					if (value != 0 && value) {
						uni.setTabBarBadge({
							index: 4,
							text: value + ""
						})
					}
					name = 'taskTip';
					value = res.data.data.total;
					that.$store.commit('$uStore', {
						name,
						value
					});
				}
			})
			this.$nextTick(function() {
				this.canReset && this.mescroll.resetUpScroll() // 重置列表数据为第一页  
				this.canReset && this.mescroll.scrollTo(0, 0) // 重置列表数据为第一页时,建议把滚动条也重置到顶部,避免无法再次翻页的问题  
				this.canReset = true // 过滤第一次的onShow事件,避免初始化界面时重复触发upCallback, 无需配置auto:false
			});

		},
		onNavigationBarButtonTap(e) {
			console.log(e)
			if (!this.hasLogin) {
				this.isShowAlert = true;
			} else {
				uni.navigateTo({
					url: 'myAttention?selectType=' + '关注'

				})
			}

		},

		methods: {
			delIcon() {
				this.keyword = ''
				uni.setStorageSync('jiaoyikeyword','')
				this.mescroll.resetUpScroll()
			},
			previewImage(item, index) {
				var i = item.imgList; //获取当前页面的轮播图数据
				//uniapp预览轮播图
				uni.previewImage({
					current: index, //预览图片的下标
					urls: i //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				})
			},
			releaseAlertbtn() {
				getApp().globalData.userRelease = false
				this.isSHowReleaseAlert = false
			},
			cancelFollow() {
				let _obj = {
					id: this.selectItem.followInformation.id
				}
				console.log()
				this.$api.doRequest('post', '/followInformation/api/deleteInfo', _obj).then(res => {
					if (res.data.code == 200) {
						this.$refs.uToast.show({
							title: '取关成功',
							type: 'success',
						})
						this.mescroll.resetUpScroll()
					}
					uni.hideLoading()
				})
			},
			mescrollInit(mescroll) {
				console.log("mescroll", mescroll)
				this.mescroll = mescroll;
			},
			cancelClick() {
				this.isShowAlert = false
			},
			alertBtn() {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			},
			
			toSearch() {
				uni.setStorageSync('jiaoyikeyword', this.keyword)
				uni.setStorageSync('jiaoyifocus', true)
				// this.isShowSearch = true
				uni.navigateTo({
					url:'/pages/business/search'
				})
			},
			follow(val, type) {
				// 2关注
				if (!this.hasLogin) {
					this.isShowAlert = true;
				} else {
					this.selectItem = val
					if (type == 1) {
						this.show = true
					} else {
						let _obj = {
							userId: this.userInfo.id,
							followedId: val.userId
						}
						uni.showLoading({
							title: '数据加载中',
							mask: true
						})
						this.$api.doRequest('post', '/followInformation/api/addInfo', _obj).then(res => {
							if (res.data.code == 200) {
								this.$refs.uToast.show({
									title: '关注成功',
									type: 'success',
								})
								this.mescroll.resetUpScroll()
							}
							uni.hideLoading()
						})
					}
				}


			},
			selectAddress(val) {
				console.log(val)
				try {
					uni.setStorageSync('transactionLocation', val);
					this.selectPlace = val.address
					this.searchPlace = val.showVal
				} catch (e) {
					// error
				}
				this.mescroll.resetUpScroll()
				console.log(val)
			},
			release() {
				if (!this.hasLogin) {
					this.isShowAlert = true;

				} else {
					this.$api.doRequest('get', '/commonUser/getInfo', {id:uni.getStorageSync('userInfo').id}).then(
						res => {
							console.log(res.data)
							if (!res.data.data.transactionStatus || res.data.data.transactionStatus && res.data.data.transactionStatus != '禁止发布') {
								uni.navigateTo({
									url: 'release'
								})
							} else {
								uni.showToast({
									title: '暂未获得发布权限！',
									icon:'none'
								})
							}
						})
					
					

				}

			},
			downCallback() {
				this.mescroll.resetUpScroll();
			},
			async upCallback(page) {
				uni.showLoading({
					title: '数据加载中',
					mask: true
				})
				console.log("this.selectPlace", this.selectPlace)
				console.log("this.selectType", this.selectType)
				let _obj = {

				}
				console.log(this.selectType)
				if (this.selectType == '关注') {
					_obj = {
						pageSize: page.size,
						currentPage: page.num,
						placeDelivery: this.selectPlace,
						tranType: this.selectType,
						userIdFollow: this.userInfo.id,
						nowUserId: this.userInfo.id,
						pcphone: 'mobile'
					}
				} else {
					_obj = {
						pageSize: page.size,
						currentPage: page.num,
						placeDelivery: this.selectPlace,
						tranType: this.selectType,
						nowUserId: this.userInfo.id,
						searchKeyWord: this.keyword,
						pcphone: 'mobile'
					}
				}
				await this.$api.doRequest('get', '/transactionExchangeInfo/selectTransactionExchangeInfo', _obj).then(
					res => {
						console.log(res.data)
						if (res.data.code == 200) {
							console.log(res.data.data.records, page.num)
							let curPageData = res.data.data.records;
							let curPageLen = curPageData.length;
							let totalPage = res.data.data.total;
							if (page.num == 1) this.goods = [];
							this.goods = this.goods.concat(curPageData);
							console.log(curPageLen, totalPage)
							this.mescroll.endByPage(curPageLen, totalPage);
						}
					})

				if (this.goods.length > 0) {
					for (let i = 0; i < this.goods.length; i++) {
						await this.$api.doRequest('get', 'appendix/query/getFileList', {
							appendixIds: this.goods[i].urlImg
						}).then(res1 => {
							if (i == this.goods.length - 1) {
								uni.hideLoading()
							}
							var arr = []
							for (let q = 0; q < res1.data.data.length; q++) {
								arr.push(res1.data.data[q].appendixPath)
							}
							this.goods[i].imgList = arr
							this.$forceUpdate()
						})
					}
				}else{
					uni.hideLoading()
				}

			},
			//点击空布局按钮的回调
			emptyClick() {
				uni.showToast({
					title: '点击了按钮,具体逻辑自行实现'
				})
			},

			// 切换菜单
			tabChange(e) {
				this.goods = [] // 先置空列表,显示加载进度
				if (this.tabs[e].name == '全部') {
					this.selectType = ''
				} else {
					this.selectType = this.tabs[e].name
				}
				console.log(this.selectType)
				this.mescroll.resetUpScroll() // 再刷新列表数据
			}

		}
	}
</script>

<style lang='scss' scoped>
	page,
	.content {
		background: #F5F6FA;
	}

	/deep/.button-show {
		width: 80rpx;
	}

	.flex {
		display: flex;
	}

	.alc {
		align-items: center;
	}

	.jcse {
		justify-content: space-evenly;
	}

	.jcsb {
		justify-content: space-between;
	}

	.top-warp {
		z-index: 998;
		position: fixed;
		top: --window-top;
		/* css变量 */
		left: 0;
		width: 100%;
		/* height: 120upx; */
		background-color: white;
	}


	.top-warp .tip {
		font-size: 28upx;
		height: 60upx;
		line-height: 60upx;
		text-align: center;
	}

	.slot-wrap {
		margin-right: 45rpx;
		font-size: 32rpx;
	}

	.list {
		margin: 0 20rpx;

		.list-item {
			padding: 26rpx 17rpx;
			background: #fff;
			margin-bottom: 20rpx;
			border-radius: 20rpx;

			.row1 {

				.head-img {
					width: 72rpx;
					border-radius: 8rpx;
					margin-right: 22rpx;
				}

				.left {
					.title {
						font-weight: 700;
						color: #333333;
					}

					.bottom {
						.color1 {
							font-size: 24rpx;
							padding: 4rpx 10rpx;
							color: #fff;
							display: inline-block;
							background: linear-gradient(180deg, #607AE0 0%, #516CDC 100%);
							border-radius: 4rpx;
						}

						.color2 {
							font-size: 24rpx;
							padding: 4rpx 10rpx;
							color: #fff;
							display: inline-block;
							background: linear-gradient(180deg, #FD714F 0%, #FD613C 100%);
							border-radius: 4rpx;
						}

						.color3 {
							font-size: 24rpx;
							color: #AFB3BF;
							margin-left: 14rpx;

						}
					}
				}

				.right {
					display:flex;
					align-items: center;
					padding: 10rpx 20rpx;
					border: 1px solid #CDCDCD;
					border-radius: 50rpx;
				}
			}

			.row2 {
				margin-top: 20rpx;
				/* 		overflow: hidden;
				text-overflow: ellipsis;
				width: 100%;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 3;
				word-break: break-all; */
			}

			.row3 {
				/* padding: 30rpx 30rpx 0 30rpx; */
				margin-top: 18rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex-wrap: wrap;
				/* background: #bbb; */


				.img {
					display: flex;
					align-items: center;
					/* justify-content: space-between; */
					width: 210rpx;
					height: 210rpx;
					margin-bottom: 20rpx;
					background: lightblue;
					list-style: none;
					border-radius: 10rpx;
					margin-right: 16rpx;
				}

				.img:nth-of-type(3),
				.img:nth-of-type(6),
				.img:nth-of-type(9) {
					margin-right: 0;
				}
			}

			.row4 {
				margin-top: 4rpx;

				.text {
					color: #878C9C;
					font-size: 24rpx;
				}
			}
		}

		.case {
			font-weight: 700;
			margin-left: 6rpx;
		}
	}

	.add {
		position: fixed;
		bottom: 150rpx;
		right: 32rpx;
		background: #22C572;
		z-index: 999;
		width: 84rpx;
		height: 84rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.search {
		display: flex;
		align-items: center;
		position: relative;
		margin: 20rpx 28rpx 20rpx 28rpx;
		position: relative;

		.text {
			width: 90rpx;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;

		}

		.del {
			position: absolute;
			right: 20rpx;
		}
	}

	/deep/.u-search .u-content {
		padding-left: 120rpx;
	}

.place {
		display: flex;
		align-items: center;
		left: 28rpx;
		position: absolute;
		font-weight: 700;
	}
	.uni-page-head-ft {
		margin-right: 40rpx;
	}
</style>
