<template>
	<view class="home-monitor-list" v-if="isLoad">
		<fixed>
			<view class="fill-base flex-between pt-md pb-md b-1px-b">
				<view class="pl-md pr-md" style="width: 650rpx;">
					<search type="input" @input="toSearch" textAlign="left" placeholder="搜索农场名称" :padding="0"
						:radius="35">
					</search>
				</view>
				<view @tap.stop="toShowRank" class="flex-center c-caption b-1px-l" style="width: 100rpx;">
					<i class="iconfont icon-down-bold" :class="[{'rotate-180':show_rank_item}]"></i>
				</view>
			</view>
		</fixed>

		<uni-popup @change="popupChange" ref="rank_item" type="top" :custom="true">
			<view style="height: 100rpx;"></view>
			<view class="pd-lg fill-base">
				<view @tap.stop="handerTabChange(index,'rankInd')" class="f-paragraph c-title"
					:class="[{'mt-md':index!=0}]" :style="{color:index==rankInd?primaryColor:''}"
					v-for="(item,index) in rankList" :key="index">
					{{item.title}}
				</view>
			</view>
		</uni-popup>
		<view class="flex-warp ml-md mr-md">
			<view @tap.stop="goDetail(index)" class="monitor-item fill-base box-shadow radius-16"
				:class="[{'mr-md':index%2==0}]" v-for="(item,index) in list.data" :key="index">
				<image mode="aspectFill" lazy-load class="cover" :src="item.cover"></image>
				<view class="content">
					<view class="f-paragraph c-title ellipsis">{{item.title}}</view>
					<view class="flex-y-center f-caption c-caption"><i class="iconfont icon-dingwei mr-sm"></i>
						<view class="max-300 ellipsis">{{item.address}}</view>
					</view>
					<view class="flex-y-center f-caption c-caption"><i class="iconfont icon-eyeopen mr-sm"></i>
						<view class="max-300 ellipsis">{{item.iv || 0}}</view>
					</view>
				</view>
			</view>
		</view>
		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>
		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				isLoad: false,
				options: {},
				rankInd: 0,
				rankList: [{
					id: 1,
					title: '综合排序'
				}, {
					id: 2,
					title: '距离最近'
				}, {
					id: 3,
					title: '观看最多'
				}],
				activeIndex: 0,
				tabList: [],
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false,
				show_rank_item: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			userInfo: state => state.user.userInfo,
			location: state => state.user.location,
		}),
		async onLoad() {
			if (this.isLoad) return
			this.$util.showLoading()
			this.initIndex()
		},
		async onShow() {
			if (!this.isLoad || (this.location.lat && this.rankInd != 1)) return
			let [err, result] = await uni.getSetting()
			if (err || !result.authSetting[`scope.userLocation`]) return
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			...mapMutations(['updateUserItem']),
			async initIndex() {
				await this.getLocation()
				let cate = await this.$api.claim.claimCateList({
					type: 1
				})
				cate.unshift({
					id: 0,
					title: '全部'
				})
				this.tabList = cate
				this.isLoad = true
				this.param.page = 1
				this.getList()
			},
			initRefresh() {
				this.initIndex()
			},
			async getLocation() {
				let {
					location
				} = this
				if (!location.lat) {
					location = await this.$util.getBmapLocation()
					this.updateUserItem({
						key: 'location',
						val: location
					})
				}
			},
			handerTabChange(index, type) {
				this[type] = index
				if (type == 'rankInd') {
					this.$refs.rank_item.close()
				}
				this.$util.showLoading()
				this.param.page = 1
				this.list.data = []
				this.getList()
			},
			toShowRank() {
				let {
					show_rank_item,
				} = this
				if (this.lockTap) return
				this.lockTap = true
				setTimeout(() => {
					let methodModel = show_rank_item ? 'close' : 'open'
					this.$refs.rank_item[methodModel]()
				}, 500)
			},
			popupChange(e) {
				let {
					show
				} = e
				this.show_rank_item = show
				setTimeout(() => {
					this.lockTap = false
				}, 200)
			},
			toSearch(val) {
				this.loading = true
				this.param.page = 1
				this.param.title = val
				this.getList()
			},
			async getList() {
				let {
					list: oldList,
					param,
					rankList,
					rankInd
				} = this
				let {
					id: sort
				} = rankList[rankInd]
				if (sort == 2 && !this.location.lat) {
					await this.getLocation()
					return
				}
				let {
					lng = 0,
						lat = 0
				} = this.location
				param = Object.assign({}, param, {
					sort,
				});
				if (sort === 2) {
					param.lat = lat
					param.lng = lng
				}
				let newList = await this.$api.home.farmerList(param)

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			async goDetail(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/home/pages/monitor/search?id=${id}`
				this.$util.goUrl({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
	.home-monitor-list {
		.monitor-item {
			width: 345rpx;
			margin-top: 24rpx;

			.cover {
				width: 345rpx;
				height: 190rpx;
				border-radius: 15rpx 15rpx 0 0;
			}

			.content {
				padding: 24rpx;

				.iconfont {
					font-size: 26rpx;
				}
			}
		}

	}
</style>
