<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-line" v-show="charShow">
				<qiun-data-charts 
					type="line"
					:opts="opts"
					:chartData="chartData"
					:canvas2d="true"
					:reshow="charShow"
					:canvasId="canvasId"
				/>
			</view>
			<view class="attr-value item-line" 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-line',
		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: [15,15,0,5],
					enableScroll: false,
					legend: {},
					xAxis: {
						disableGrid: true
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow"
						}
					}
				},
				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){
					this.chartData = JSON.parse(this.dataList);
				}else{
					this.charShow = false;
				}
				//模拟从服务器获取数据时的延时
				// setTimeout(() => {
				// 	//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
					// let res = {
					// 		categories: ["2018","2019","2020","2021","2022","2023"],
					// 		series: [
					// 			{
					// 				name: "目标值",
					// 				data: [35,36,31,33,13,34]
					// 			},
					// 			{
					// 				name: "完成量",
					// 				data: [18,27,21,24,6,28]
					// 			}
					// 		]
					// 	};
				// 	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>