<template>
	<view class="content">
		<view class="content-picker d-e-c">
			<view class="picker-small" @click="openCalendar2">
				<text
					class="mr10">{{ checkDay2?checkDay2[0]+'  ' + $t('w_0139')+ '  ' +checkDay2[1]:$t('CK_KS_38') }}</text>
				<u-icon name="arrow-down-fill" size="28rpx" color="#333"></u-icon>
			</view>
		</view>
		<view class="charts-box">
			<!-- 注意：因各小程序及app端通过组件均不能传递function类型参数，组件内所有formatter方法，均需使用format属性指定config-ucharts.js里事先定义好的formatter的key值，组件会自动匹配与其对应的function -->
			<!-- 饼图的format需要挂到chartData中的series[i].format上，例如pieFormatDemo.series[i].format="pieDemo"。当使用localdata数据渲染图表时，因series是组件自动拼接的，暂时不支持使用format -->
			<canvas canvas-id="UdaKiTnYTHLxuZItgHFhiCtezzDQzLfD" id="UdaKiTnYTHLxuZItgHFhiCtezzDQzLfD" class="charts"
				@tap="tap" />
		</view>
		<liu-calendar-date ref="calendar2" isRange @checked='getDate2'></liu-calendar-date>
	</view>
</template>

<script>
	import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
	var uChartsInstance = {};
	var getStarDay = function() {
		let date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		month = (month > 9) ? month : ("0" + month);
		return year + '-' + month + '-01';
	}
	var getEndDay = function() {
		let date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		month = (month > 9) ? month : ("0" + month);
		let day = date.getDate();
		if (day > 1) {
			day -= 1
		}
		day = (day < 10) ? ("0" + day) : day;
		return year + '-' + month + '-' + day;
	}
	export default {

		data() {
			return {
				cWidth: 750,
				cHeight: 800,
				checkDay2: [getStarDay(), getEndDay()]
			};
		},
		onReady() {
			//这里的 750 对应 css .charts 的 width
			this.cWidth = uni.upx2px(750);
			//这里的 500 对应 css .charts 的 height
			this.cHeight = uni.upx2px(800);
			this.getServerData();
		},
		methods: {
			//打开选择器
			openCalendar2() {
				this.$refs.calendar2.open()
			},
			//日期区间选择成功
			getDate2(e) {
				this.checkDay2 = e.value;
				this.getServerData();
			},
			getServerData() {
				let self = this;
				self._post('member/api/achieve/parent-achieve-right', {
					startDate: self.checkDay2[0],
					endDate: self.checkDay2[1],
				}, res => {
					let list = res.data.list;
					let obj = {
						series: [{
							data: []
						}]
					}
					list.forEach((item, index) => {
						if (item.pkGradeKeyVal) {
							obj.series[0].data.push({
								value1: item.achieve * 1,
								value: item.peopleNumber * 1,
								name: item.pkGradeKeyVal
							})
						}

					})
					console.log(obj)
					self.drawCharts('UdaKiTnYTHLxuZItgHFhiCtezzDQzLfD', obj);
				})
			},
			drawCharts(id, data) {
				const ctx = uni.createCanvasContext(id, this);
				uChartsInstance[id] = new uCharts({
					type: "pie",
					context: ctx,
					width: this.cWidth,
					height: this.cHeight,
					series: data.series || [],
					animation: true,
					timing: "easeOut",
					duration: 1000,
					rotate: false,
					rotateLock: false,
					background: "#FFFFFF",
					color: ["#c0c1c1", "#c0c1c1", "#c8d6e5", "#31d2d7", "#ecd884", "#7090ff"],
					padding: [5, 5, 5, 5],
					fontSize: 13,
					fontColor: "#666666",
					dataLabel: true,
					dataPointShape: true,
					dataPointShapeType: "solid",
					touchMoveLimit: 60,
					enableScroll: false,
					enableMarkLine: false,
					legend: {
						show: true,
						position: "bottom",
						float: "center",
						padding: 5,
						margin: 5,
						backgroundColor: "rgba(0,0,0,0)",
						borderColor: "rgba(0,0,0,0)",
						borderWidth: 0,
						fontSize: 13,
						fontColor: "#666666",
						lineHeight: 11,
						hiddenColor: "#CECECE",
						itemGap: 10
					},
					extra: {
						pie: {
							activeOpacity: 0.5,
							activeRadius: 10,
							offsetAngle: 0,
							labelWidth: 15,
							border: true,
							borderWidth: 3,
							borderColor: "#FFFFFF",
							customRadius: 0,
							linearType: "none"
						},
						tooltip: {
							showBox: true,
							showArrow: true,
							showCategory: false,
							borderWidth: 0,
							borderRadius: 0,
							borderColor: "#000000",
							borderOpacity: 0.7,
							bgColor: "#000000",
							bgOpacity: 0.7,
							gridType: "solid",
							dashLength: 4,
							gridColor: "#CCCCCC",
							boxPadding: 3,
							fontSize: 13,
							lineHeight: 20,
							fontColor: "#FFFFFF",
							legendShow: true,
							legendShape: "auto",
							splitLine: true,
							horizentalLine: false,
							xAxisLabel: false,
							yAxisLabel: false,
							labelBgColor: "#FFFFFF",
							labelBgOpacity: 0.7,
							labelFontColor: "#666666"
						}
					}
				});
			},
			tap(e) {
				let self =this;
				//格式化ToolTip
				uChartsInstance[e.target.id].showToolTip(e, {
					formatter: (item, category, index, opts) => {
						return item.name + ' ' + item.value + ' ' + self.$t('ENU_TOTAL_V_1') + item.value1
					}
				});
			},
			getServerData1() {
				setTimeout(() => {
					//因部分数据格式一样，这里不同图表引用同一数据源的话，需要深拷贝一下构造不同的对象
					//开发者需要自行处理服务器返回的数据，应与标准数据格式一致，注意series的data数值应为数字格式
					this.chartsDataLine1 = JSON.parse(JSON.stringify(demodata.Line))

					//数据点格式化示例
					//使用format属性指定config-ucharts.js里事先定义好的formatter的key值，组件会自动匹配与其对应的function 
					let columnFormatDemo = JSON.parse(JSON.stringify(demodata.Column))
					for (var i = 0; i < columnFormatDemo.series.length; i++) {
						columnFormatDemo.series[i].format = "seriesDemo1"
					}
					this.chartsDataColumn2 = columnFormatDemo

					//饼图格式化示例
					//使用format属性指定config-ucharts.js里事先定义好的formatter的key值，组件会自动匹配与其对应的function 
					let pieFormatDemo = JSON.parse(JSON.stringify(demodata.PieA))
					for (var i = 0; i < pieFormatDemo.series.length; i++) {
						pieFormatDemo.series[i].format = "pieDemo"
					}
					this.chartsDataPie1 = pieFormatDemo
					console.log(pieFormatDemo)
				}, 1500);
			},
		}
	};
</script>

<style>
	page {
		background: #ffffff;
	}

	.content {
		display: flex;
		flex-direction: column;
		flex: 1;
		background-color: #ffffff;
		min-height: 100vh;
	}

	.content-picker {
		padding: 26rpx 22rpx 26rpx 23rpx;
	}

	.picker-small {
		height: 72rpx;
		border: 1rpx solid #EEEEEE;
		border-radius: 15rpx;
		font-size: 28rpx;
		padding: 0 28rpx 0 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;

	}

	.charts {
		width: 100%;
		height: 800rpx;
	}

	.charts-box {
		width: 100%;
		height: 300px;
	}
</style>