<template>
	<view class="pages-land" v-if="isLoad">
		<fixed>
			<view class="fill-base flex-between b-1px-b">
				<view style="width: 650rpx;">
					<tab @change="handerTabChange($event,'activeIndex')" :list="tabList" :activeIndex="activeIndex"
						:activeColor="primaryColor" height="100rpx"></tab>
				</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 @tap.stop="goDetail(index)" class="land-item fill-base mt-md ml-md mr-md box-shadow radius-16"
			v-for="(item,index) in list.data" :key="index">
			<image mode="aspectFill" lazy-load class="cover" :src="item.cover"></image>
			<view class="pd-lg">
				<view class="f-title c-title">{{item.title}}</view>
				<view class="f-caption c-caption ellipsis-2">
					<text decode="emsp" style="word-break:break-all;">{{item.desc}}</text>
				</view>
				<view class="flex-between mt-md">
					<view class="flex-y-baseline">
						<view class="f-lg-title c-warning">¥{{item.min_price}}</view>
						<view class="f-caption c-caption ml-sm">起</view>
					</view>
					<view class="common-btn flex-center f-paragraph c-base radius" :style="{background:primaryColor}">
						选择土地</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: '距离优先'
				}],
				tabList: [],
				activeIndex: 0,
				cate_id: 0,
				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,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			location: state => state.user.location,
			loginType: state => state.user.loginType,
		}),
		async onLoad() {
			if (this.isLoad) return
			this.$util.showLoading()
			this.initIndex()
		},
		async onShow() {
			if (!this.isLoad || (this.location.lat && this.rankInd != 2)) 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()
		},
		onShareAppMessage(e) {
			let {
				id: pid
			} = this.userInfo
			let path = `/pages/land?pid=${pid}`
			this.$util.log(path)
			return {
				title: '',
				imageUrl: '',
				path,
			}
		},
		methods: {
			...mapActions(['getConfigInfo']),
			...mapMutations(['updateUserItem']),
			async initIndex(refresh = false) {
				if(!this.configInfo.id || refresh){
					await this.getConfigInfo()
					if (this.loginType == 'apple') {
						this.updateUserItem({
							key: 'isShowLogin',
							val: false
						})
					}
				}
				await this.getLocation()
				let cate = await this.$api.claim.claimCateList({
					type: 1
				})
				cate.unshift({
					id: 0,
					title: '全部'
				})
				this.isLoad = true
				this.tabList = cate
				this.param.page = 1
				this.getList()
			},
			initRefresh() {
				this.$refs.rank_item.close()
				this.initIndex(true)
			},
			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
				this.$refs.rank_item.close()
				if (type == 'activeIndex') {
					this.cate_id = this.tabList[index].id
				}
				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)
			},
			async getList() {
				let {
					list: oldList,
					param,
					tabList,
					activeIndex,
					rankList,
					rankInd,
					cate_id
				} = this
				let ind = tabList.findIndex(item => {
					return item.id == cate_id
				})
				cate_id = ind == -1 ? 0 : cate_id
				activeIndex = ind == -1 ? 0 : ind
				this.cate_id = cate_id
				this.activeIndex = activeIndex
				let {
					id: sort
				} = rankList[rankInd]
				if (sort == 3 && !this.location.lat) {
					this.$util.hideAll()
					await this.getLocation()
					return
				}
				let {
					lng = 0,
						lat = 0
				} = this.location
				param = Object.assign({}, param, {
					cate_id,
					sort,
				});
				if (sort === 3) {
					param.lat = lat
					param.lng = lng
				}
				let newList = await this.$api.land.landList(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 = `/land/pages/detail?id=${id}`
				this.$util.toCheckLogin({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
	.pages-land {
		.land-item {
			.cover {
				width: 710rpx;
				height: 345rpx;
				border-radius: 15rpx 15rpx 0 0;
			}
		}

	}
</style>
