<template>
	<view class="shop-choose-store" v-if="isLoad">
		<fixed>

			<view class="rel">
				<view class="user-store-info abs">
					<image mode="aspectFill" lazy-load class="store-bg abs" src="https://lbqny.migugu.com/admin/farm/bg-store.png">
					</image>
				</view>
				<uni-nav-bar :fixed="false" :shadow="false" :statusBar="true" :onlyLeft="true" color="#fff"
					:backgroundColor="`none`">
					<view @tap.stop="toChooseLocation" class="flex-y-center" slot="left">
						<i class="iconfont icon-dingwei mr-sm"></i>
						<view class="text-bold max-400 ellipsis">
							{{location.address ?location.address : isLoad ? '定位失败' : '定位中...'}}
						</view>
						<i class="iconfont icon-down-bold ml-sm"></i>
					</view>
				</uni-nav-bar>
				<view class="search-box-radius fill-base">
					<search @input="toSearch" @confirm="toSearch" type="input" :radius="30"
						:disabled="location.address ? false : true" placeholder="搜索店铺名称"></search>
				</view>
			</view>
		</fixed>

		<view @tap.stop="goDetail(index)" class="farm-item rel fill-base  mt-md ml-md mr-md pd-lg box-shadow radius-24"
			v-for="(item,index) in list.data" :key="index">
			<view class="more-btn abs flex-center">
				<view class="flex-y-baseline" :style="{color:primaryColor}">
					<view>进店选购</view>
					<i class="iconfont icon-right"></i>
				</view>
			</view>
			<view class="flex-center">
				<image mode="aspectFill" lazy-load class="cover box-shadow-mini radius-24" :src="item.cover"></image>
				<view class="flex-1 ml-lg">
					<view class="flex-between f-title c-title mt-sm mb-sm">
						<view class="max-300 ellipsis">{{item.title}}</view>
						<view class="f-caption c-caption">
							{{item.distance}}
						</view>
					</view>
					<view class="star-fill-info rel">
						<view class="flex-warp star rel">
							<view class="item-star flex-center" v-for="(aitem,aindex) in 5" :key="aindex">
								<i class="iconfont icon-star-bold-fill"></i>
							</view>
						</view>
						<view class="star-fill abs" :style="{width: item.star_percent}">
							<view class="flex-warp">
								<view class="item-star flex-center" v-for="(aitem,aindex) in 5" :key="aindex">
									<i class="iconfont icon-star-bold-fill icon-font-color"
										:style="{backgroundImage: '-webkit-linear-gradient(90deg, #FDCD47, #FFC000)'}"></i>
								</view>
							</view>
						</view>
					</view>
					<view class="flex-y-center f-caption c-caption mt-sm"><i class="iconfont icon-dingwei mr-sm"></i>
						<view class="addr-text ellipsis">{{item.address}}</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>
		<abnor @confirm="$util.checkAuth({ type: 'userLocation' })" :tip="[{ text: '定位失败，请开启地理位置授权后重试~', color: 0 }]"
			:button="[{ text: '开启定位' , type: 'confirm' }]" btnSize="" v-if="!loading && !location.lng"> </abnor>

		<view class="space-max-footer"></view>

		<fix-bottom-button @confirm="$util.goUrl({ url: 1, openType: `navigateBack` })"
			:text="[{ text: '返回上页', type: 'confirm' }]" bgColor="#fff" v-if="isLoad"> </fix-bottom-button>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				isLoad: false,
				param: {
					page: 1,
					long: '',
					lat: '',
					store_name: ''
				},
				list: {
					data: []
				},
				loading: true,
			}
		},
		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,
		}),
		onLoad() {
			this.$util.setNavigationBarColor({
				color: '#ffffff',
				bg: ''
			})
		},
		onShow() {
			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 path = `/user/pages/store-list`
			this.$util.log(path);
			return {
				title: '',
				imageUrl: '',
				path,
			}
		},
		methods: {
			...mapActions(['getUserInfo', 'getShopCarList']),
			...mapMutations(['updateUserItem']),
			async initIndex() {
				let {
					location
				} = this
				if (!location.lat) {
					location = await this.$util.getBmapLocation()
					this.updateUserItem({
						key: 'location',
						val: location
					})
				}
				if (!location.lng) {
					this.loading = false
					this.isLoad = true
					return
				}
				await this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			toSearch(val) {
				this.param.page = 1
				this.param.store_name = val
				this.getList()
			},
			async getList() {
				let {
					list: oldList,
					param,
					location
				} = this
				let {
					lng = 0,
						lat = 0
				} = location
				param.lng = lng
				param.lat = lat
				let newList = await this.$api.shop.indexStoreList(param);
				newList.data.map(item => {
					item.is_open = 1
					item.star_percent = (item.star * 1 / 5 * 100).toFixed(2) + '%'
				})
				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.isLoad = true
				this.$util.hideAll()
			},
			// 选择地区
			async toChooseLocation(e) {
				await this.$util.checkAuth({
					type: 'userLocation'
				})

				let [, {
					address = '',
					longitude: lng,
					latitude: lat
				} = {}] = await uni.chooseLocation();
				if (!lng) return
				let location = {
					lng,
					lat,
					address
				}
				this.updateUserItem({
					key: 'location',
					val: location
				})
				this.location = location
				this.param.page = 1
				this.getList()
			},
			async goDetail(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/shop/pages/store?id=${id}`
				this.$util.goUrl({
					url,
					openType: 'redirectTo'
				})
			}
		}
	}
</script>


<style lang="scss">
	.shop-choose-store {

		.user-store-info {
			width: 100%;
			z-index: -1;

			.store-bg {
				width: 100%;
				height: 168rpx;
				height: calc(168rpx + env(safe-area-inset-bottom) / 2);
				padding-bottom: calc(5px + env(safe-area-inset-bottom) / 2);
				z-index: -1;
			}

		}

		.search-box-radius {
			border-radius: 30rpx 30rpx 0 0;
		}


		.farm-item {
			.cover {
				width: 160rpx;
				height: 160rpx;
			}

			.icon-dingwei {
				font-size: 24rpx;
			}


			.addr-text {
				max-width: 420rpx;
			}

			.more-btn {
				top: 0;
				right: 0;
				width: 122rpx;
				height: 46rpx;
				font-size: 20rpx;
				background: rgba(112, 152, 64, 0.1);
				border-radius: 0 25rpx 0 15rpx;

				.iconfont {
					font-size: 20rpx;
				}
			}
		}
	}
</style>
