<template>
	<view class="qiun-columns">


		<view class="qiun-charts">
			<!--#ifdef MP-ALIPAY -->
			<canvas canvas-id="canvasMap" id="canvasMap" class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio"
			 :style="{'width':cWidth+'px','height':cHeight+'px'}" @touchstart="touchMap"></canvas>
			<!--#endif-->
			<!--#ifndef MP-ALIPAY -->
			<canvas canvas-id="canvasMap" id="canvasMap" class="charts" @touchstart="touchMap"></canvas>
			<!--#endif-->
			<view class="fugai">覆盖地区</view>
			<view class="dian">点亮中国</view>
		</view>
	</view>
</template>

<script>
	import app from "../../App.vue";
	import uCharts from '@/components/u-charts/u-charts.js';
	import {
		isJSON
	} from '@/common/checker.js';
	var _self;
	var canvaMap = null;

	export default {
		data() {
			return {
				cWidth: '',
				cHeight: '',
				pixelRatio: 1,
				textarea: '',
				cityname: ''
			}
		},
		onLoad() {
			_self = this;
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function(res) {
					if (res.pixelRatio > 1) {
						//正常这里给2就行,如果pixelRatio=3性能会降低一点
						//_self.pixelRatio =res.pixelRatio;
						_self.pixelRatio = 3;
					}
				}
			});
			//#endif
			this.cWidth = uni.upx2px(750);
			this.cHeight = uni.upx2px(750);

			// 获取覆盖城市
			this.getcity()
		},
		methods: {
			getServerData() {
				let that=this;
				uni.showLoading({
					title: '加载中'
				})
				// https://geo.datav.aliyun.com/areas/bound/100000_full.json
				// https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json
				// https://geo.datav.aliyun.com/areas_v2/bound/100000.json
				uni.request({
					url: ' https://geo.datav.aliyun.com/areas/bound/100000_full.json',
					data: {},
					success: function(res) {
						 console.log(res);
						let data = res.data.features;
						
						data.forEach(function(value, index, array) {
							
							value.color = "#CBE2E6";
							if (value.properties.name == "新疆维吾尔自治区") {
								value.properties.name = "新疆"
							} else if (value.properties.name == '内蒙古自治区') {
								value.properties.name = "内蒙古"
							} else if (value.properties.name == '天津市') {
								value.properties.name = "天津"
							} else if (value.properties.name == '北京市') {
								value.properties.name = "北京"
							} else if (value.properties.name == '上海市') {
								value.properties.name = "上海"
							} else if (value.properties.name == '西藏自治区') {
								value.properties.name = "西藏"
							} else if (value.properties.name == '广西壮族自治区') {
								value.properties.name = "广西"
							} else if (value.properties.name == '香港特别行政区') {
								value.properties.name = "香港"
							} else if (value.properties.name == '澳门特别行政区') {
								value.properties.name = "澳门"
							} else if (value.properties.name == '宁夏回族自治区') {
								value.properties.name = '宁夏'
							}else if (value.properties.name == '重庆市') {
								value.properties.name = '重庆'
							}else {
								let name = value.properties.name.split('省');
								
								console.log('3277344',name)
								value.properties.name = name[0];
							}
							
							console.log(value.properties.name,9999)
						})
						uni.hideLoading()
						let cMap = {
							series: []
						};
						//这里我后台返回的是数组,所以用等于,如果您后台返回的是单条数据,需要push进去

						// ssssconsole.log(newmap.features)
						data.forEach(function(value, index, array) {
							that.cityname.forEach(function(valuek, indexk, arrayk) {
								if (value.properties.name == valuek.shortname) {
									console.log(value.properties.name)
									  value.color = "#F5F208";

								}
							})
						})
						cMap.series = data

						_self.showMap("canvasMap", cMap);
					},
					fail: () => {
						_self.tips = "网络错误,小程序端请检查合法域名";
					},
				});
			},
			
			showMap(canvasId, chartData) {
				canvaMap = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'map',
					fontSize: 7,
					padding: [0, 0, 0, 0],
					legend: {
						show: false
					},
					background: '#FFFFFF',
					pixelRatio: _self.pixelRatio,
					series: chartData.series,
					dataLabel: true,
					width: _self.cWidth * _self.pixelRatio,
					height: _self.cHeight * _self.pixelRatio,
					extra: {
						map: {
							border: true,
							borderWidth: 1,
							borderColor: '#C29D5A',
							fillOpacity: 0.6
						}
					}
				});
			},
			touchMap(e) {
				console.log(e)
				canvaMap.showToolTip(e, {
					format: function(item) {
						console.log(item)
						return item.properties.name
					}
				});
			},
			// 获取覆盖城市
			getcity() {
				let that = this;
				var url = 'sundry/get_area_province';
				var params = {

				}
				console.log('7766554', params)
				app.post(url, params).then((res) => {
					console.log(res)
					that.cityname = res.data.data;
					console.log(that.cityname)
					this.getServerData();


				}).catch((err) => {
					console.log(err,999888)

				})
			},
		}
	}
</script>

<style>
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 750upx;
		background-color: #FFFFFF;
	}

	.charts {
		width: 750upx;
		height: 750upx;
		background-color: #FFFFFF;
	}
	.dian{
		color:#3D454C;
		font-size: 64rpx;
		font-weight: bold;
		text-align: center;
		margin: 100rpx auto 0;;
	}
	.fugai{
		color:#3D454C;
		font-size: 32rpx;
		text-align: center;
		margin: 100rpx auto 0;
	}
</style>