<template>
	<view>
		<topnav title="选择医院" @tohome="tohome"></topnav>
		<view class="content">
			<view class="mapbox">
				<map class="mapinfo" :latitude="latitude" :longitude="longitude"></map>
			</view>
			<scroll-view>
				<view class="l-box">
					<block v-for="(item,index) in hospital">

						<view class="l-box-info">
							<view class="l-box-info-left" @tap="toselect" :data-index="index">
								<view class="l-box-info-left-title">
									{{item.name}}
								</view>
								<view class="l-box-info-left-ftitle">
									{{item.address}}
								</view>
							</view>
							<view class="l-box-info-right" @click="tochange" :data-index="index">
								<text class="iconfont icon-check icon-check-in" v-if="item.chekced"></text>
								<text class="iconfont icon-check" v-else></text>
							</view>
						</view>
					</block>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	var util = require("../../utils/util.js");
	export default {
		data() {
			return {
				id: 0,
				title: 'map',
				latitude: 39.909,
				longitude: 116.39742,
				hospital: []
			}
		},
		onShow: function() {
			this.loadhospital();
		},
		methods: {
			tohome:function(){
				uni.navigateTo({
					url:"../index/index"
				})
			},
			tochange: function(e) {
				var index = e.currentTarget.dataset.index;
				var _hospital = this.hospital;
				for (var m of _hospital) {
					m.chekced = false;
				}
				_hospital[index].chekced = true;
				this.hospital = _hospital;
				this.latitude = _hospital[index].latitude;
				this.longitude = _hospital[index].longitude;
			},
			toselect: function(e) {
				var index = e.currentTarget.dataset.index;
				var _hospital = this.hospital;
				uni.setStorageSync('currentProject', _hospital[index]);
				uni.reLaunch({
					url: '/pages/index/index'
				});
			},
			loadhospital: function() {
				var that = this;
				var currentProject = uni.getStorageSync('currentProject');
				util.request({
					api: 'manager/get-projects',
					utilSuccess: function(res) {
						var _res = res;
						for (var m of _res) {
							if (currentProject.id == m.id) {
								that.longitude = m.longitude;
								that.latitude = m.latitude;
								m.chekced = true;
							} else {
								m.chekced = false;
							}
						}


						that.hospital = _res;
					},
					utilFail: function(err) {
						util.alert(err);
					}
				});
			}
		}
	}
</script>

<style>
	page {
		padding-top: 160rpx;
		padding-bottom: 120rpx;
	}

	.mapinfo {
		width: 750rpx;
		height: 641rpx;
	}
</style>
