<template>
	<view class="iots-component-box">
		<view class="attr-item-left">
			<view class="iconfont" :class="icon"></view>
			<view class="attr-name">{{name}}</view>
			<view v-if="logShow" class="iconfont icon-unordered-list" @click="clickLog"></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: 0
			},
			// 图表数据
			dataList:{
				type:[Object],
				default:{series: [{name: '',data: 0}]}
			},
			// 图标
			icon: {
				type: [String],
				default: 'icon-dashboard2'
			},
			name: {
				type: [String],
				default: ''
			},
			logShow:{
				type: [Boolean],
				default: true
			},
			// 最小值
			min:{
				type:Number,
				default:0
			},
			// 最大值
			max:{
				type:Number,
				default:100
			},
			unit:{
				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"
							},
							format:'yAxisDemo2',
							// format:function(val, index, opts){return val.toFixed(2)},
							
						}
					}
				},
				range: [
          { value: 0, text: "近1小时" },
          { value: 1, text: "近1天" },
          { value: 2, text: "近1周" },
        ],
		  };
		},
		watch: {
		  value(newValue) {
				if(newValue!==null){
					this.getServerData();
				}
				// // #ifndef H5
				// this.getServerData();
				// // #endif
		  }
		},
		mounted() {
			this.canvasId=this.$u.guid(32);
		},
		methods:{
			getServerData() {
				if(this.dataList){
					console.log("仪表盘数据",this.value)
					let list = this.dataList;
					this.opts.extra.gauge.startNumber = this.min;
					this.opts.extra.gauge.endNumber = this.max.toFixed(2);
					this.opts.subtitle.name = list.series[0].name;
					this.opts.title.name = this.value + this.unit;
					console.log("this.opts",this.opts)
					// let obj = {
					// 	categories: [{"value":0.2,"color":"#1890ff"},{"value":0.8,"color":"#2fc25b"},{"value":1,"color":"#f04864"}],
						
					// }
					this.chartData = {"categories": [{"value":0.2,"color":"#1890ff"},{"value":0.8,"color":"#2fc25b"},{"value":1,"color":"#f04864"}],"series": this.dataList.series};
					// this.chartData = 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);
			},
			clickLog(){
				this.$emit('clickLog');
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '../../common.css';
	.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;
				box-sizing: border-box;
				padding-top: 40rpx;
			}
		}
	}
</style>