<template>
	<view style="padding-bottom: 30rpx;">
		<!-- tab -->
		<view class="tab-box ddflex">
			<view class="fflex ddflex">
				<view :class="'tab-item fflex '+(queryType==1?'tab-item-active':'')" @click="queryTypeChange(1)">
					<view>实时状态</view>
					<image class="tab-active-icon" src="../../static/images/tab_cur.png"></image>
				</view>
				<view :class="'tab-item fflex '+(queryType==2?'tab-item-active':'')" @click="queryTypeChange(2)">
					<view>BMS控制</view>
					<image class="tab-active-icon" src="../../static/images/tab_cur.png"></image>
				</view>
			</view>
		</view>
		<view style="height: 110rpx;"></view>
		
		<block v-if="queryType==1">
			<view class="option-box">
				<view class="option-time ddflex">
					<view>更新时间: {{time}}</view>
					<view class="refresh ddflex" @click="getData()">
						<image src="../../static/pages/images/refresh.png"></image>
						<text>刷新</text>
					</view>
				</view>
				<view style="position: relative;">
					<view style="height: 130rpx;"></view>
					<view style="margin-top: 40rpx;justify-content: center;position: absolute;left: 50%;transform: translateX(-50%);top: -50%;" class="ddflex">
						<cell :width="99" :height="197" :soc="info.State.SOC" :showsoc="true" :rotate="90"></cell>
					</view>
				</view>
			</view>
			
			<view class="bms-info">
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>电池类型：</text>
						<text class="bms-row-data">{{info.State.BetteryType==1?'铁锂':'三元'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>工作状态：</text>
						<text class="bms-row-data">{{info.State.WorkStatus==1?'充电中':info.State.WorkStatus==2?'闲置':'放电中'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>总电压：</text>
						<text class="bms-row-data">{{info.State.BetteryV_All}}V </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>标称容量：</text>
						<text class="bms-row-data">{{info.State.FCC}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>剩余容量：</text>
						<text class="bms-row-data">{{info.State.SYLA}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>充电电流：</text>
						<text class="bms-row-data">{{info.State.CPowerA}}A </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>剩余容量百分比：</text>
						<text class="bms-row-data">{{info.State.SOC}}% </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>充电保护状态：</text>
						<text class="bms-row-data">{{info.State.CPStatus==1?'硬控保护':info.State.CPStatus==2?'软控保护':'正常'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>放电保护状态：</text>
						<text class="bms-row-data">{{info.State.DPStatus==1?'硬控保护':info.State.DPStatus==2?'软控保护':'正常'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>短路保护：</text>
						<text class="bms-row-data">{{info.State.SCProtect==1?'保护':'正常'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>均衡类型：</text>
						<text class="bms-row-data">{{info.State.BetteryType==1?'被动':info.State.BetteryType==2?'主动':'无'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>均衡开关：</text>
						<text class="bms-row-data">{{(info.State.EM==1||info.State.EM=='Open')?'打开':'关闭'}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>保护板温度：</text>
						<text class="bms-row-data">{{info.State.TC_T}}°C </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>出厂日期：</text>
						<text class="bms-row-data">{{info.State.CCXX}} </text>
					</view>
				</view>
				<view class="bms-row ddflex">
					<view class="fflex ddflex">
						<text>厂商编码：</text>
						<text class="bms-row-data">{{info.State.BMSCode}} </text>
					</view>
				</view>
			</view>
		</block>
		
		<block v-if="queryType==2">
			<view class="option-box">
				<view class="option-time ddflex">
					<view>更新时间: {{time}}</view>
					<view class="refresh ddflex" @click="getData()">
						<image src="../../static/pages/images/refresh.png"></image>
						<text>刷新</text>
					</view>
				</view>
				<!-- <view class="ddflex state">
					<view class="ddflex">
						<view>充电开关：</view>
						<view style="color: #A8A5A5;">{{info.Seting.CHON==1?'开启':'关闭'}}</view>
					</view>
					<switch style="transform: scale(0.6);" :checked="info.Seting.CHON" color="#FF753A"></switch>
				</view> -->
				<view class="ddflex state">
					<view class="ddflex">
						<view>放电开关：</view>
						<view style="color: #A8A5A5;">{{info.Seting.DHON==1?'开启':'关闭'}}</view>
					</view>
					<switch style="transform: scale(0.6);" :checked="info.Seting.DHON==1?true:false" color="#FF753A" @change="switchChange"></switch>
				</view>
			</view>
		</block>
		
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request.js");
	import cell from "../../components/cell/index";
	const utils = require("../../utils/util");

	export default {
		components: {
			cell
		},
		props: {},
		data() {
			return {
				systems: {},
				isTop:0,
				
				queryType: 1,
				
				id:null,
				info:{},
				time:''
			}
		},
		onLoad(options) {
			this.id = options.id
			this.getData()
		},
		onShow() {
			
		},
		methods: {
			jumpUrl(url) {
				if (req.isLogins(true)) {
					uni.navigateTo({
						url: url
					})
				}
			},
			queryTypeChange(val) {
				if (this.queryType == val) return false
				this.queryType = val
			},
			getData(){
				uni.showLoading({
					title:'加载中'
				})
				req.getRequest('/admin/v2/battery/bmsData',{batteryId:this.id},res=>{
					this.info = res[0]
					this.info.Seting = JSON.parse(this.info.Seting)
					this.info.State = JSON.parse(this.info.State)
					this.time = utils.formatTime(new Date()).t1
					uni.hideLoading()
				})
			},
			switchChange(event){
				event.detail.value = event.detail.value?1:0
				this.info.Seting.DHON = event.detail.value
				let that = this
				uni.showModal({
					title:'提示',
					content:event.detail.value?'确定开启放电开关？':'确定关闭放电开关？',
					success: (r) => {
						if(r.confirm){
							uni.showLoading({
								title:event.detail.value?'开启中':'关闭中',
							})
							req.postRequest('/admin/v2/battery/bmsSend',{batteryId:this.id,type:event.detail.value},res=>{
								uni.hideLoading()
								if(res){
									// req.msg(event.detail.value?'开启成功':'关闭成功')
									uni.showModal({
										title:'提示',
										content:'操作成功,该操作会有延迟，请稍候查看最新状态',
										showCancel:false,
										success() {
											that.getData()
										}
									})
								}else {
									req.msg(event.detail.value?'开启失败':'关闭失败')
									this.info.Seting.DHON = this.info.Seting.DHON==1?0:1
								}
							})
						}else{
							this.info.Seting.DHON = this.info.Seting.DHON==1?0:1
							// this.getData()
						}
						
					}
				})
			},
		},
		mounted() {
		},
		onPageScroll: function(e) {
		}
	}
</script>

<style>
	@import "./BMS.css";
</style>