<template>
	<view>
		<u-navbar title="用电量明细 (度)" title-color="#101010"></u-navbar>
		<view class="dropdown">
			
			<view class="radio">
				<u-radio-group v-model="type" @change="monthChange">
					<u-radio  v-for="(item, index) in monthList" :key="index" :name="item.value"
						:disabled="item.disabled">
						{{item.label}}
					</u-radio>
				</u-radio-group>
			</view>
			
			
		</view>
		
		<u-select title="设备选择" v-model="show1" mode="mutil-column-auto" :list="electricMeterList"
			@confirm="electricMeterChange" cancel-text="重置" @cancel="electricMeterReset">
		</u-select>

		
		<u-picker mode="time" :defaultTime="defaultTime" v-model="show3" :params="params" @confirm="timeChange"></u-picker>

		<view class="table-box">
			<table class="table1">
				<tr class="tr1">
					<td class="td1"></td>
					<td class="td2">起止码</td>
					<td class="td3">度数</td>
				</tr>
				<tr class="tr2">
					<td class="td1">{{kwhMap.meterName}}</td>
					<td class="td2">
						{{kwhMap.startMeter!=null ? kwhMap.startMeter : '0'}}-{{kwhMap.endMeter!=null ? kwhMap.endMeter : '0'}}
					</td>
					<td class="td3">{{kwhMap.kwh}}度</td>
				</tr>
				
				<tr v-for="(item,i) in childMap" :key="i" v-if="Object.keys(childMap).length!=0">
					<td class="td1">{{item.meterName}}</td>
					<td class="td2">
						{{item.startMeter!=null ? item.startMeter : '0'}}-{{item.endMeter!=null ? item.endMeter : '0'}}
					</td>
					<td class="td3">{{item.kwh}}度</td>
				</tr>
				<tr class="tr2" v-if="Object.keys(childMap).length!=0" style="height: 40px;">
					<td class="td1" colspan="2">{{childLevel}}合计用电</td>

					<td>{{childKwh}}度</td>
				</tr>

				<tr class="tr3" v-if="Object.keys(childMap).length!=0">
					<td class="td1" colspan="2">扣除后用电量</td>

					<td>{{subKwh}}度</td>
				</tr>
			</table>
		</view>

	</view>
</template>

<script>
	import * as API_electricityMeter from '@/apis/pagejs/electricityMeter.js'
	import * as API from '@/apis/pagejs/index.js'

	export default {
		data() {
			return {
				childLevel: '',
				childKwh: 0,
				childMap: {},
				kwhMap: {},
				subKwh: 0,
				id: '',
				meterName: '',
				meterId: '',
				type: '2',
				queryDate: '',
				electricMeter: '11楼公摊电表',
				month: '上月',
				show1: false,
				show2: false,
				electricMeterList: [],
				monthList: [{
					value: '1',
					label: '本月'
				}, {
					value: '4',
					label: '今日'
				}, {
					value: '2',
					label: '上月'
				}, {
					value: '3',
					label: '本年'
				}, {
					value: '0',
					label: '合计'
				}, {
					value: '10',
					label: '指定月份'
				}],
				show3: false, // 时间选择
				defaultTime: '',
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false,
					timestamp: false
				},
			}
		},
		onLoad(op) {
			if(op.id) {
				this.meterId = op.id;
				this.electricMeter =  op.name;
				this.type = op.type;
				for (var i = 0; i < this.monthList.length; i++) {
					if(this.type == this.monthList[i].value) {
						this.month = this.monthList[i].label;
					}
				}
				
				this.id = op.id;
				//this.meterName =  op.name;
				
				this.getElectricityStatisticsDetails();
				//this.getCompanyInfoList();
			}
			
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth()+1 >= 10 ? date.getMonth()+1 : '0'+(date.getMonth()+1);
			this.defaultTime = year + '-' + month;
		},
		methods: {
			timeChange(params) {
				// console.log(params)
				this.month = params.year + '-' + params.month;
				this.queryDate = params.year + '-' + params.month + '-01';
				this.monthList[5].label = this.month;
				this.getElectricityStatisticsDetails();
			},
			electricMeterReset() {
				this.meterId = this.id;
				this.electricMeter =  this.meterName;
				
				this.getElectricityStatisticsDetails();
			},
			electricMeterChange(e) {
				this.meterId = e[2].value;
				this.electricMeter = e[2].label;
				
				this.getElectricityStatisticsDetails();
			},
			monthChange(e) {
				
				this.type = e;
				
				if(e == '10') {
					this.show3 = true;
				} else {
					this.monthList[5].label = "指定月份";
					this.getElectricityStatisticsDetails();
				}
			},
			getElectricityStatisticsDetails() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API_electricityMeter.electricityStatisticsDetails({
					meterId: this.meterId,
					type: this.type,
					queryDate: this.queryDate,
				}).then((res) => {
					uni.hideLoading();
					this.childKwh = res.data.childKwh;
					this.childMap = res.data.childMap;
					this.kwhMap = res.data.kwhMap;
					this.subKwh = res.data.subKwh;
					this.childLevel = res.data.childLevel;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getCompanyInfoList() {
				// uni.showLoading({
				// 	title: "加载中",
				// 	mask: true,
				// })
				API.deviceCompanyList().then((response) => {
					// uni.hideLoading();
					var list = response.data.companyInfoList;
					this.electricMeterList = [];
					this.electricMeterList = list.map(item => {
						if (item.remoteReadingMeterList.length != 0 && item.remoteMonitorMeterList.length != 0) {
							return {
								label: item.name,
								value: item.id,
								children: [{
										label: '抄表',
										value: '1',
										children: item.remoteReadingMeterList.map(item => {
											return {
												label: item.name,
												value: item.id,
											}
										})
									},
									{
										label: '监控表',
										value: '1',
										children: item.remoteMonitorMeterList.map(item => {
											return {
												label: item.name,
												value: item.id,
											}
										})
									}
								]
							}
						} else if (item.remoteReadingMeterList.length != 0 && item.remoteMonitorMeterList.length == 0) {
							return {
								label: item.name,
								value: item.id,
								children: [{
									label: '抄表',
									value: '1',
									children: item.remoteReadingMeterList.map(item => {
										return {
											label: item.name,
											value: item.id,
										}
									})
								}]
							}
						} else if (item.remoteReadingMeterList.length == 0 && item.remoteMonitorMeterList.length != 0) {
							return {
								label: item.name,
								value: item.id,
								children: [{
									label: '监控表',
									value: '1',
									children: item.remoteMonitorMeterList.map(item => {
										return {
											label: item.name,
											value: item.id,
										}
									})
								}]
							}
						} else {
			
						}
					})
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.dropdown {
		background-color: #fff;
		position: sticky;
		top: 88rpx;
		z-index: 999;
		padding: 18rpx 46rpx;
		display: flex;
		border-bottom: 1px solid rgba(245, 245, 245, 1);

		.dropdown-item {
			width: 50%;
			text-align: center;
			height: 60rpx;
			line-height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.text {
				max-width: 80%;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.table-box {
		padding: 24rpx 32rpx;
	}

	.table1 {

		padding-bottom: 20rpx;
		width: 100%;
		border-collapse: collapse;

		td {
			border: 1px solid #e7e7e7;
			text-align: end;
			
			padding: 16rpx 16rpx;
			height: 60rpx;

		}

		.td1 {
			text-align: start;
			width: 264rpx;
		}

		.td2 {
			width: 270rpx;
		}

		.td3 {
			width: 170rpx;
			white-space: pre;
		}

		tr {
			background-color: #f5f5f6;
		}

		.tr1 {
			text-align: end;
			font-weight: bold;
		}
		
		.tr2 {
			background-color: #fff;
		}
		
		.tr3 {
			font-weight: bold;
			height: 40px;
			background-color: rgba(22,119,255,1);
			color: rgba(255,255,255,1);
		}

		// tr:nth-child(even) {
		// 	background-color: #fff;
		// }
	}
</style>