<template>
	<view class="project-detail">
		<swiper class="swiper" circular :indicator-dots="true">
			<swiper-item v-for="item in responsibility" :key="item.id">
				<image class="image" :src="item.url" mode="aspectFill">
				</image>
			</swiper-item>
		</swiper>
		<view class="project-title">
			<view class="project-name">{{detail.name}}</view>
			<view class="project-label">距离您{{detail.distance || '-'}}km</view>
			<button type="default" class="wx-icon" open-type="share">
				<uni-icons type="weixin" open-type="share" size="38" color="#43b156">
				</uni-icons>
			</button>
		</view>
		<view class="project-label project-article">{{detail.comment}}</view>
		<view class="project-content">
			<view class="content-title">地理位置</view>
			<view class="content-map">
				<map id="map" class="map" :show-location="true" :latitude="latitude" :longitude="longitude"></map>
			</view>
			<view class="content-title">配套设施</view>
			<view class="content-device">
				<view class="device-item"
					v-for="item in $field.findTypeNameByList('supportingFacilities',detail.supportingFacilities)"
					:key="item.id">
					<uni-icons class="device-icon" custom-prefix="iconfont" :type="item.icon" size="18"></uni-icons>
					<text class="device-label">{{item.name}}</text>
				</view>
			</view>
			<view class="content-title">房源列表</view>
			<view class="house-list">
				<house-items v-for="(item,index) in list" :house="item" :key="item.id"></house-items>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getOpenHouseListByPage
	} from '@/request/api/house.js'
	import {
		getProjectDetailById
	} from '@/request/api/project.js'
	import houseItems from "@/components/house/houseItems.vue";
	export default {
		data() {
			return {
				list: [],
				detail: {},
				responsibility: [],
				coordinates: [],
				latitude: 39.90923,
				longitude: 116.397428,
				projectId: '',
				coordinatesstr: ''
			}
		},
		onLoad(body) {
			this.projectId = body.projectId;
			this.coordinatesstr = this.$store.getters.coordinates;
			this.init();
		},
		onReady() {
			this._mapContext = uni.createMapContext("map", this);
		},
		components: {
			houseItems
		},
		methods: {
			init() {
				console.log(this.coordinatesstr);
				getProjectDetailById(this.projectId + '?coordinates=' + this.coordinatesstr).then(res => {
					if (res.code === 200) {
						this.detail = res.data;
						if (this.detail.picture) this.responsibility = JSON.parse(this.detail.picture);
						if (this.detail.coordinates) {
							this.coordinates = this.detail.coordinates.split(',');
							this.latitude = this.coordinates[1];
							this.longitude = this.coordinates[0];
							this.addMarkers();
						}

					}
				})
				getOpenHouseListByPage({
					currPage: 1,
					pageSize: 100,
					projectId: parseInt(this.projectId)
				}).then(res => {
					if (res.code === 200) {
						this.list = this.list.concat(res.data.dataList); //追加新数据
					}
				})
			},
			addMarkers() {
				const positions = [{
					latitude: this.coordinates[1],
					longitude: this.coordinates[0],
				}]
				const markers = []
				positions.forEach((p, i) => {
					markers.push(
						Object.assign({}, {
							id: i + 1,
							width: 50,
							height: 50,
							joinCluster: true, // 指定了该参数才会参与聚合
						}, p)
					)
				})
				this._mapContext.addMarkers({
					markers,
					clear: false,
					complete(res) {}
				})
			}
		},
	}
</script>

<style lang="scss">
	.project-detail {
		background: #fff;
		padding-bottom: 60rpx;

		.swiper {
			height: 400rpx;

			.image {
				width: 100%;
				height: 100%;
			}
		}

		.project-label {
			font-size: 24rpx;
			color: $uni-secondary-color;
			font-weight: 300;

		}

		.project-article {
			font-size: 28rpx;
			padding: 0 30rpx;
		}

		.project-title {
			padding: 40rpx 130rpx 30rpx 30rpx;
			position: relative;

			.project-name {
				font-size: 36rpx;
				font-weight: 500;
			}

			.project-label {
				margin-top: 6rpx;
			}

			.wx-icon {
				position: absolute;
				top: 0;
				right: 30rpx;
				transform: translateY(50%);
				line-height: 1;
				background: transparent;

				&::after {
					display: none;
				}
			}
		}

		.content-map {
			width: 100%;
			height: 360rpx;
			border-radius: 16rpx;
			overflow: hidden;
			display: flex;

			.map {
				flex: 1;
				height: 100%;
				border-radius: 16rpx;
			}
		}

		.project-content {
			padding: 0 30rpx;
			box-sizing: border-box;

			.content-title {
				font-size: 32rpx;
				font-weight: 500;
				margin: 30rpx 0;
			}

			.content-device {
				display: flex;
				flex-wrap: wrap;

				.device-item {
					width: 50%;
					display: flex;
					align-items: center;
					margin-bottom: 20rpx;

					.device-label {
						flex: 1;
						width: 0;
						color: $uni-secondary-color;
						font-weight: 200;
						margin-left: 10rpx;
					}

					.device-icon {
						color: $uni-base-color;
					}
				}
			}
		}
	}
</style>