<template>
	<view class="content">
		<view style="width: 100%;display: flex;align-items: center;">
			<view class="content">
				<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" ref="chartId" :id="chartId" class="echarts"></view>
			</view>
			<view>
				<text style="font-size: 28rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: left;color: #5f636f;">意向总价</text>
				<view style="display: flex;justify-content: space-between;margin-top: 10rpx;">
					<text style="font-size: 28rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: left;" :style="{'color':fuzhuColor}" v-if="dataList.length > 0">约{{dataList[0]}}万</text>
					<text style="font-size: 28rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: left; margin-left: 82rpx;" v-if="dataList.length > 1">约{{dataList[1]}}万</text>
					<text style="font-size: 28rpx;font-family: Verdana, Verdana-Regular;font-weight: 400;text-align: left; margin-left: 82rpx;" v-if="dataList.length > 2">约{{dataList[2]}}万</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import echarts from '../../static/echarts.js'
	var colorList = ['#73DDFF', '#73ACFF', '#FDD56A'];
	export default {
		props: {
			chartId: String,
			title: {
				type: String,
				default: '面积'
			},
			dataList: {
				type: Array,
				default:() => {
					return []
				}
			}
		},
		data() {
			return {
				themeColor: "",
				fuzhuColor: "",
				themeColor25: "",
				option: {
					title: {
						text: '总价',
						x: 'center',
						y: 'center',
						textStyle: {
							fontSize: 10
						}
					},
					color: colorList,
					series: [{
						name: '访问来源',
						type: 'pie',
						radius: ['50%', '70%'],
						avoidLabelOverlap: false,
						hoverAnimation: false,
						label: {
							show: false,
							position: 'center'
						},
						itemStyle: {
							normal: {
								// color: function(params) {
								// 	return colorList[params.dataIndex]
								// }
							}
						},
						emphasis: {
							label: {
								show: false,
								fontSize: '30',
								fontWeight: 'bold'
							}
						},
						labelLine: {
							show: false
						},
						data: []
						// data: [{
						// 		value: 335,
						// 		name: '直接访问'
						// 	},
						// 	{
						// 		value: 310,
						// 		name: '邮件营销'
						// 	},
						// 	{
						// 		value: 234,
						// 		name: '联盟广告'
						// 	}
						// ]
					}]
				}
			}
		},

		methods: {
			getDataAction() {
				// this.dataList = []
				// this.data1 = []
				// for (var i = 0; i < 3; i++) {
				// 	var num1 = Math.ceil(Math.random() * 50)
				// 	this.data1.push(num1)
				// 	this.dataList.push(num1)
				// }
				this.option.series[0].data = this.dataList
				// this.option.xAxis.data = this.dataList
				// console.log('总价数据刷新', this.option)
			},
			onViewClick(options) {
				// console.log(options)
			}
		},
		watch: {
			chartId: {
				handler(e) {
					// console.log('走没走!!!!', e)
					// this.getDataAction()
				},
				immediate: true
			},
			dataList: {
				handler(e) {
					this.getDataAction()
				},
				immediate: true,
				deep: true
			}
		},
		mounted() {
			let app = getApp()
			let globalData = app.globalData;
			this.themeColor = globalData.themeColor
			this.fuzhuColor = globalData.fuzhuColor
			this.themeColor25 = globalData.themeColor25
			let that = this;

		}

	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			this.initAction()
		},
		methods: {
			initAction() {
				if (typeof window.echarts === 'function') {
					this.initEcharts()
				} else {
					// 动态引入较大类库避免影响页面展示
					const script = document.createElement('script')
					// view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
					script.src = 'static/echarts.js'
					script.onload = this.initEcharts.bind(this)
					document.head.appendChild(script)
				}
			},
			initEcharts() {
				// console.log("initEcharts -> this.$refs", this.$refs.chartId.$el.id)
				myChart = echarts.init(document.getElementById(this.$refs.chartId.$el.id))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		.top-Section {
			padding: 0 20rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			font-family: Verdana;

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #383838;
			}

			.rightBtn {
				font-size: 24rpx;
				color: #546074;
				margin-top: 5rpx;
			}
		}

		.echarts {
			width: 214rpx;
			margin-top: 0;
			height: 200rpx;
		}
	}
</style>