<template>
	<view class="app">
		<view class="inputs">
			<i class="icon"></i>
			<input v-model="searchName" type="text" name="" id="" class="input" placeholder="请输入">
		</view>
		<view class="Classificationlist">
			<view v-for="(item,index) in nurseItemClassifyInfoList" :key="index" @tap='tapitemclass(item)'
				:class="item.id == itemClassId?'itemclass':'item'">
				<image :src="item.classifyPictureUrl" mode=""></image>
				<view class="title">
					{{item.classifyName}}
				</view>
			</view>
			<view class="item" v-if='nurseItemClassifyInfoList.length>8'>
				<image src="../../static/gd.png" mode=""></image>
				<view class="title">
					更多
				</view>
			</view>
		</view>
		<u-swiper :list="lbinfo" height='200' interval='3000' duration='1500'></u-swiper>
		<view class="conNew">
			<view class="title">
				周边医养机构
			</view>
			<u-tabs :list="nurseAgencyClassifyInfoList" :current="conNewcurrent" @change="change"></u-tabs>
			<view class="" v-if="nursestationlist.length>0">
				<view class="addressContent" v-for="(item,index) in nursestationlist" :key="index">
					<view class="lists" @tap='godetails(item)'>
						<view class="nurse">{{item.nurseStationName}}</view>
						<view class="distance"
							v-show="homeLatitudeLongitude.homeLatitude&&homeLatitudeLongitude.homeLongitude">
							距离您{{item.distance}}KM</view>
						<image class="background" :src="item.stationPictureUrl" mode=""></image>
						<view class="tags">
							<view class="tag" v-for="uitem in item.classifyInfoList">
								{{uitem.classifyName}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="noorder" v-else>
				<image src="../../static/noorder.png" mode=""></image>
				<view class="">
					暂无护理站
				</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,
		getNurseClassifyList
	} from '@/api/site/site.js';
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				nurseAgencyClassifyInfoList: [], //护理站分类
				conNewcurrent: 0,
				nurseItemClassifyInfoList: [], //护理项目分类
				lbinfo: [], //轮播list
				nursestationlist: [],
				mask: false,
				pageNum: 1,
				pageSize: 10,
				total: 0,
				latitude: 39.90374,
				longitude: 116.397827,
				homeLatitudeLongitude: null,
				nurseClassId: '', //护理站分类id
				itemClassId: '', //护理项目分类id
				searchName: '', //通用名称
			}
		},
		methods: {
			//点击护理项目分类
			tapitemclass(item) {
				if (this.itemClassId == item.id) {
					this.itemClassId = ''
				} else {
					this.itemClassId = item.id
				}
				this.requestinfo()
			},
			//护理站和护理项目分类
			getNurseClassifyinfo() {
				getNurseClassifyList().then(res => {
					this.nurseAgencyClassifyInfoList.push({
						name: '全部',
						id: '',
					})
					res.data.nurseAgencyClassifyInfoList.forEach(e => {
						e.name = e.classifyName
						this.nurseAgencyClassifyInfoList.push(e)
					})
					res.data.nurseItemClassifyInfoList.forEach(e => {
						e.classifyPictureUrl = baseurl + e.classifyPictureUrl
					})
					res.data.poserInfoList.forEach(e => {
						var obj = {
							image: baseurl + e.posterPictureUrl
						}
						this.lbinfo.push(obj)
					})
					this.nurseItemClassifyInfoList = res.data.nurseItemClassifyInfoList.slice(0, 7)
				})
			},
			change(index) {
				this.conNewcurrent = index;
				this.nurseClassId = this.nurseAgencyClassifyInfoList[index].id
				this.requestinfo()
			},
			getlocation(value) {
				var that = this
				uni.openSetting({
					success(res) {
						if (res.authSetting['scope.userLocation']) {
							// uni.getLocation({
							// 	type: 'wgs84',
							// 	success: function(res) {
							// 		that.latitude = res.latitude
							// 		that.longitude = res.longitude
							// 		that.mask = false
							// 	}
							// });
						}
					}
				});
			},
			requestinfo() {
				this.pageNum = 1;
				nearbyList(this.pageSize, this.pageNum, this.longitude, this.latitude, this.nurseClassId, this.itemClassId,
						this.searchName)
					.then(res => {
						uni.removeStorageSync('Refresh');
						res.rows.forEach(e => {
							e.stationPictureUrl = baseurl + e.stationPictureUrl
							if (e.classifyInfoList.length >= 1) {
								e.classifyInfoList = e.classifyInfoList.slice(0, 2)
							}
						})
						this.nursestationlist = res.rows
						this.mask = false
						this.total = res.total
					})
			},
			godetails(item) {
				uni.navigateTo({
					url: `/pages/nursestation/nursestation?nurseStationId=${item.nurseStationId}&distance=${item.distance}`
				})
			},
			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.nurseClassId, this.itemClassId,
				// 		this.searchName)
				// 	.then(res => {
				// 		res.rows.forEach(e => {
				// 			e.stationPictureUrl = baseurl + e.stationPictureUrl
				// 			if (e.classifyInfoList.length >= 1) {
				// 				e.classifyInfoList = e.classifyInfoList.slice(0, 2)
				// 			}
				// 			this.nursestationlist.push(e)
				// 		})
				// 	})
			}
		},
		onPullDownRefresh() { //下拉刷新
			this.pageNum = 1;
			// this.requestinfo()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		watch: {
			searchName() {
				this.requestinfo()
			},
		},
		onShow() {
			this.nurseClassId = ''
			this.itemClassId = ''
			var that = this
			const value = uni.getStorageSync('patientId');
			if (value) {
				getPatientInfo(value).then(res => {
					if (res.code == 200) {
						this.homeLatitudeLongitude = res.data
						if (res.data.homeLatitude && res.data.homeLongitude) {
							that.latitude = res.data.homeLatitude
							that.longitude = res.data.homeLongitude
							that.requestinfo();
						} else {
							that.requestinfo();
							// uni.getLocation({
							// 	type: 'wgs84',
							// 	success: function(resp) {
							// 		that.latitude = resp.latitude
							// 		that.longitude = resp.longitude
							// 		that.requestinfo();
							// 	},
							// 	fail(err) {
							// 		that.mask = true;
							// 	}
							// });
						}
					} else if (res.code == 9999) {} else {
						that.requestinfo();
					}
				})
			} else {
				that.requestinfo();
				// uni.getLocation({
				// 	type: 'wgs84',
				// 	success: function(res) {
				// 		that.latitude = res.latitude
				// 		that.longitude = res.longitude
				// 		that.requestinfo();
				// 	},
				// 	fail(err) {
				// 		that.mask = true;
				// 	}
				// });
			}
		},
		onLoad() {
			this.getNurseClassifyinfo();
		},
		//1.分享给朋友
		onShareAppMessage(res) {
			let pages = getCurrentPages();
			let url = pages[pages.length - 1].$page.fullPath
			return {
				title: '泉医到家',
				path: url,
			}
		},
		//2.分享到朋友圈
		onShareTimeline(res) {
			let pages = getCurrentPages();
			let url = pages[pages.length - 1].$page.fullPath
			return {
				title: '泉医到家',
				path: url,
			}
		},
	}
</script>
<style lang="scss">
	@import './site.scss'
</style>
