<template>
	<view class="iots-component-box">
		<view class="attr-item-left">
			<view class="iconfont" :class="icon"></view>
			<view class="attr-name">{{name}}</view>
			<view class="select-list">
				<uni-data-select
					v-model="selectValue"
					:localdata="range"
					:clear="false"
					@change="changeSelect"
				></uni-data-select>
			</view>
		</view>
		<view class="attr-item-right">
			<view class="attr-value item-gauge" v-show="charShow">
				<qiun-data-charts 
					type="gauge"
					:opts="opts"
					:chartData="chartData"
					:canvas2d="true"
					:reshow="charShow"
					:canvasId="canvasId"
				/>
			</view>
			<view class="attr-value item-gauge" v-show="!charShow">
				<u-empty></u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import uniDataSelect from "../../uni-data-select/uni-data-select.vue"
	export default{
		name: 'iots-gauge',
		props:{
			// 选择值
			value: {
				type: [String,Number],
				default: false
			},
			// 图表数据
			dataList:{
				type:[String,null],
				default:50
			},
			// 图标
			icon: {
				type: [String],
				default: 'icon-gongnengdingyi'
			},
			name: {
				type: [String],
				default: ''
			}
		},
		components:{
			uniDataSelect
		},
		data() {
		  return {
		    chartData: {},
				selectValue:this.value,
				charShow: true,
				canvasId:this.$u.guid(32),
				opts: {
					color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
					padding: [5,5,5,5],
					title: {
						name: "60Km/H",
						fontSize: 25,
						color: "#2fc25b",
						offsetY: 50
					},
					subtitle: {
						name: "实时速度",
						fontSize: 15,
						color: "#666666",
						offsetY: -50
					},
					extra: {
						gauge: {
							type: "default",
							width: 30,
							labelColor: "#666666",
							startAngle: 0.75,
							endAngle: 0.25,
							startNumber: 0,
							endNumber: 100,
							labelFormat: "",
							splitLine: {
								fixRadius: 0,
								splitNumber: 10,
								width: 30,
								color: "#FFFFFF",
								childNumber: 5,
								childWidth: 12
							},
							pointer: {
								width: 24,
								color: "auto"
							}
						}
					}
				},
				range: [
          { value: 0, text: "近1小时" },
          { value: 1, text: "近1天" },
          { value: 2, text: "近1周" },
        ],
		  };
		},
		watch: {
		  value(newValue) {
		    this.selectValue = newValue;
		  }
		},
		mounted() {
			this.getServerData();
			this.canvasId=this.$u.guid(32);
		},
		methods:{
			getServerData() {
				if(this.dataList){
					console.log("this.dataList",this.dataList)
					let list = JSON.parse(this.dataList);
					this.opts.subtitle.name = list.series[0].name;
					this.opts.title.name = (list.series[0].data * 100) + '%';
					
					// let obj = {
					// 	categories: [{"value":0.2,"color":"#1890ff"},{"value":0.8,"color":"#2fc25b"},{"value":1,"color":"#f04864"}],
						
					// }
					this.chartData = JSON.parse(this.dataList);
				}else{
					this.charShow = false;
				}
				//模拟从服务器获取数据时的延时
				      // setTimeout(() => {
				      //   //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
				      //   let res = {
				      //       categories: [{"value":0.2,"color":"#1890ff"},{"value":0.8,"color":"#2fc25b"},{"value":1,"color":"#f04864"}],
				      //       series: [
				      //         {
				      //           name: "完成率",
				      //           data: 0.66
				      //         }
				      //       ]
				      //     };
				      //   this.chartData = JSON.parse(JSON.stringify(res));
				      // }, 500);
			},
			// 点击步进器
			changeSelect(e) {
				console.log("e",e)
				this.$emit('input', e);
				this.$emit('change',e);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.iots-component-box{
		padding: 20rpx 20rpx;
		background: #fff;
		// display: flex;
		// align-items: center;
		// justify-content: space-between;
		margin-top: 20rpx;
		border-radius: 10rpx;
		.attr-item-left{
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #000;
			.iconfont{
				margin-right: 20rpx;
				font-size: 40rpx;
			}
			.select-list{
				margin-left: auto;
				width: 160rpx;
			}
		}
		.attr-item-right{
			// flex: 1;
			display: flex;
			// flex-wrap: wrap;
			align-items: center;
			justify-content: center;
			.attr-value{
				width: 100%;
				height: 600rpx;
			}
		}
	}
</style>