<template>
	<view class="app">
		<view class="nursetation" @tap='detailed()'>
			<text class="what">什么是护理站？</text>
			<u-icon class="icon" name="arrow-right"></u-icon>
		</view>
		<view class="map" style="height:35vh;">
			<map style="width:100%; height:100%" scale="17" :latitude="latitude" :longitude="longitude"
				:markers="markers"></map>
		</view>
		<view class="conNew">
			<view class="addressContent" v-for="(item,index) in nursestationlist" :key="index">
				<view class="lists">
					<view class="nurse">{{item.nurseStationName}}</view>
					<view class="distance">距离您{{item.distance}}KM</view>
					<image class="background" :src="item.stationPictureUrl" mode=""></image>
					<view class="understand" @tap='godetails(item)'>
						点击了解
					</view>
				</view>
			</view>
		</view>
		<view class="masks" style="" v-if="mask">
			<view class="mask">
				<view class="picture">
					<image src="../../static/locatinsmall.png"></image>
				</view>
				<view class="text">
					请打开系统定位
				</view>
				<view class="btns">
					<view class="bt" @tap='cancel()'>取消</view>
					<view class="btn" @tap='getlocation()'>确定</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		nearbyList,
		getPatientInfo
	} from '@/api/site/site.js';
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				nursestationlist: [],
				mask: false,
				pageNum: 1,
				pageSize: 10,
				total: 0,
				//map地图所需
				id: 0, // 使用 marker点击事件 需要填写id
				// latitude: 39.909,
				// longitude: 116.39742,
				latitude: 36.649372,
				longitude: 116.977102,
				nurseTypeCode: '',
				markers: [],
				covers: [],
			}
		},
		methods: {
			getlocation() {
				var that = this
				uni.getLocation({
					type: 'wgs84',
					success: res => {
						that.latitude = res.latitude
						that.longitude = res.longitude
						that.markers.push({
							id: 3,
							latitude: res.latitude,
							longitude: res.longitude,
							width: 20,
							height: 25,
							iconPath: "../../static/locatinsmall.png"
						})
						that.requestinfo();
					},
					fail: error => {
						uni.showModal({
							title: '提示',
							content: '获取定位失败，是否授权打开定位',
							success: (res) => {
								if (res.confirm) {
									uni.getSystemInfo({
										success: (sys) => {
											if (sys.platform == 'ios') {
												plus.runtime.openURL(
													"app-settings://");
											} else {
												var main = plus.android
													.runtimeMainActivity();
												var Intent = plus.android.importClass(
													"android.content.Intent");
												//可能应该直接进入应用列表的权限设置？=> android.settings.APPLICATION_SETTINGS
												var mIntent = new Intent(
													'android.settings.LOCATION_SOURCE_SETTINGS'
												);
												main.startActivity(mIntent);
											}
										}
									})
								}
							}
						})
					}
				});
			},
			requestinfo() {
				nearbyList(this.pageSize, this.pageNum, this.longitude, this.latitude, this.nurseTypeCode).then(res => {
					res.rows.forEach(e => {
						e.stationPictureUrl = baseurl + e.stationPictureUrl
					})
					this.nursestationlist = res.rows
					this.mask = false
					this.total = res.total
					// console.log(this.nursestationlist)
				})
			},
			godetails(item) {
				console.log(item)
				uni.navigateTo({
					url: `/pages/nursestation/nursestation?nurseStationId=${item.nurseStationId}`
				})
			},
			detailed() {
				uni.navigateTo({
					url: `/pages/detail/detail`
				})
			},
			cancel() {
				this.mask = false
				uni.switchTab({
					url: '/pages/homepage/homepage',
				})
			},
		},
		onReachBottom() {
			if (this.nursestationlist.length >= this.total) {} else {
				this.pageNum++;
				nearbyList(this.pageSize, this.pageNum, this.longitude, this.latitude, this.nurseTypeCode)
					.then(res => {
						res.rows.forEach(e => {
							e.stationPictureUrl = baseurl + e.stationPictureUrl
							this.nursestationlist.push(e)
						})
					})
			}
		},
		onPullDownRefresh() { //下拉刷新
			this.pageNum = 1;
			nearbyList(this.pageSize, this.pageNum, this.longitude, this.latitude, this.nurseTypeCode, this.iptVal)
				.then(res => {
					res.rows.forEach(e => {
						e.stationPictureUrl = baseurl + e.stationPictureUrl
					})
					this.nursestationlist = res.rows
				})
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		watch: {
			iptVal() {
				// this.requestinfo()
			},
		},
		onLoad() {
			var that = this
			try {
				const value = uni.getStorageSync('patientId');
				if (value) {
					getPatientInfo(value).then(res => {
						if (res.code == 200) {
							if (res.data.homeLatitude && res.data.homeLongitude) {
								that.latitude = res.data.homeLatitude
								that.longitude = res.data.homeLongitude
								that.markers.push({
									id: 3,
									latitude: res.data.homeLatitude,
									longitude: res.data.homeLongitude,
									width: 20,
									height: 25,
									iconPath: "../../static/locatinsmall.png"
								})
								that.requestinfo();
							} else {
								that.getlocation()
							}
						} else {
							that.getlocation()
						}
					})
				} else {
					that.getlocation()
				}
			} catch (e) {}
		},
	}
</script>
<style lang="scss">
	@import './site.scss'
</style>
