<template>
    <view class="container">
        <view class="tab-container">
            <view class="tab-row flex-row flex-around">
                <view :class="'tab-item ' + (selectedTabIndex == 1 ? 'tab-selected' : '')" @tap="bindSelectedTab" data-idx="1">实时状态</view>
                <view :class="'tab-item ' + (selectedTabIndex == 2 ? 'tab-selected' : '')" @tap="bindSelectedTab" data-idx="2">保护统计</view>
                <view :class="'tab-item ' + (selectedTabIndex == 3 ? 'tab-selected' : '')" @tap="bindSelectedTab" data-idx="3">BMS控制</view>
            </view>
        </view>
        <view class="man-body">
            <view v-if="selectedTabIndex == 1" class="box-body">
                <view class="section-head">
                    <view class="fresh-area flex-row flex-between">
                        <view>
                            <text>更新时间:</text>
                            <text class="fresh-time">{{ tools.formatTime(bmsInfo.State.BMS_DateTime / 1000) }}</text>
                        </view>
                        <view @tap="loadBmsInfo">
                            <image class="fresh-icon" src="/static/resource/images/fr-icon.png"></image>
                            <text>刷新</text>
                        </view>
                    </view>
                    <view class="battery-model">
                        <!-- <image class="battery-model-icon" src="/resource/images/b3.png"></image> -->
                        <com-ele-quantity :quantity="bmsInfo.State.RC"></com-ele-quantity>
                        <text></text>
                    </view>
                </view>
                <view class="section-body">
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            总电压：
                            <text class="param-value">{{ bmsInfo.State.BetteryV_All }}</text>
                            V
                        </view>
                        <view class="param">
                            温控数：
                            <text class="param-value">{{ bmsInfo.State.TemperatureCount }}</text>
                            个
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            温控明细：
                            <text class="param-value">
                                [{{ bmsInfo.State.TC_B_1 }}°C][{{ bmsInfo.State.TC_B_2 }}°C][{{ bmsInfo.State.TC_B_3 }}°C][{{ bmsInfo.State.TC_B_4 }}°C]
                            </text>
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            过压保护：
                            <text class="param-value"></text>
                        </view>
                        <view class="param">
                            欠压保护：
                            <text class="param-value"></text>
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            电流数据：
                            <text class="param-value">{{ bmsInfo.State.BetteryA }}</text>
                            A
                        </view>
                        <view class="param">
                            循环次数：
                            <text class="param-value">{{ bmsInfo.State.BXHC }}</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            剩余容量：
                            <text class="param-value">{{ bmsInfo.State.RC }}%</text>
                        </view>
                        <view class="param">
                            剩余容量：
                            <text class="param-value"></text>
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            均衡开闭：
                            <text class="param-value">{{ bmsInfo.State.EM == 'Open' ? '开启' : '关闭' }}</text>
                        </view>
                        <view class="param">
                            标称容量：
                            <text class="param-value">{{ bmsInfo.State.JZRL }}</text>
                            AH
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            设备编码：
                            <text class="param-value">{{ bmsInfo.State.SBBM }}</text>
                        </view>
                        <view class="param">
                            出厂日期：
                            <text class="param-value">{{ bmsInfo.State.CCXX }}</text>
                        </view>
                    </view>
                </view>

                <view class="section-body">
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            电池数量：
                            <text class="param-value">{{ bmsInfo.State.BetteryC }}</text>
                            串
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            最低电压：
                            <text class="param-value">{{ bmsInfo.State.min_v }}</text>
                            V
                        </view>
                        <view class="param">
                            最高电压：
                            <text class="param-value">{{ bmsInfo.State.max_v }}</text>
                            V
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            相差电压：
                            <text class="param-value">{{ bmsInfo.State.span_min_max }}</text>
                            V
                        </view>
                    </view>

                    <view class="param-item batterys-show">
                        <view class="battery-row">
                            <!-- <view class="battery-block" wx:for="{{bmsInfo.State.betList}}" wx:key="unique"> -->
                            <view class="battery-block" v-for="(item, index) in batteryNumArr" :key="index">
                                <view class="battery-bat">
                                    <view class="i-bat">
                                        <image class="i-bat-icon" src="/static/resource/images/b0.png"></image>
                                        <view class="i-bat-index">{{ index + 1 }}</view>
                                    </view>
                                    <view class="i-vol">{{ bmsInfo.State.betList[index] ? bmsInfo.State.betList[index] : 0 }}</view>
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <view v-else-if="selectedTabIndex == 2" class="box-body">
                <view class="section-head"></view>
                <view class="section-body">
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            刷新时间:
                            <text class="fresh-time">{{ tools.formatTime(bmsInfo.State.BMS_DateTime / 1000) }}</text>
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            1.短路保护次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            2.充电过流次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            3.放电过流次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            4.单体过压次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            5.单体欠压次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            6.充电高温次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            7.充电低温次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            8.放电高温次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            9.放电低温次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            10.整体过压次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            11.整体欠压次数:
                            <text class="param-value">0</text>
                            次
                        </view>
                    </view>
                </view>
            </view>

            <view v-else-if="selectedTabIndex == 3" class="box-body">
                <view class="section-head">
                    <view class="fresh-area flex-row flex-between">
                        <view>
                            <text>更新时间:</text>
                            <text class="fresh-time">{{ tools.formatTime(bmsInfo.State.BMS_DateTime / 1000) }}</text>
                        </view>
                        <view @tap="loadBmsInfo">
                            <image class="fresh-icon" src="/static/resource/images/fr-icon.png"></image>
                            <text>刷新</text>
                        </view>
                    </view>
                </view>
                <view class="section-body">
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            MOS充电开关:
                            <text class="param-value">{{ bmsInfo.Seting.EMST == 'Close' ? '关闭' : '开启' }}</text>
                        </view>
                        <view class="command-control">
                            <text class="btn-def">开启</text>
                            <text class="btn-def">关闭</text>
                        </view>
                    </view>
                    <view class="param-item flex-row flex-between">
                        <view class="param">
                            MOS放电开关:
                            <text class="param-value">{{ bmsInfo.Seting.CHON == 'Close' ? '关闭' : '开启' }}</text>
                        </view>
                        <view class="command-control">
                            <text class="btn-def">开启</text>
                            <text class="btn-def">关闭</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
<script module="tools" lang="wxs" src="@/pages/common/wxs/tools.wxs"></script>
<script>
import comEleQuantity from '@/component/comEleQuantity/comEleQuantity';
// pages/bms/bms.js
var config = require('../../common/config.js');
var http = require('../../common/http.js');
var common = require('../../common/common.js');
var storage = require('../../common/storage.js');
export default {
    components: {
        comEleQuantity
    },
    data() {
        return {
            selectedTabIndex: 1,
            battery_sn: '',
            bmsInfo: {
                State: {
                    BMS_DateTime: 0,
                    RC: '',
                    BetteryV_All: '',
                    TemperatureCount: '',
                    TC_B_1: '',
                    TC_B_2: '',
                    TC_B_3: '',
                    TC_B_4: '',
                    BetteryA: '',
                    BXHC: '',
                    EM: '',
                    JZRL: '',
                    SBBM: '',
                    CCXX: '',
                    BetteryC: '',
                    min_v: '',
                    max_v: '',
                    span_min_max: ''
                },

                Seting: {
                    EMST: '',
                    CHON: ''
                }
            },
            batteryNumArr: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const battery_sn = options.battery_sn || '015356934636';
        if (!battery_sn) {
            uni.navigateBack({
                delta: 1
            });
        }
        this.battery_sn = battery_sn;
        console.log(battery_sn);
        this.loadBmsInfo();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    methods: {
        bindSelectedTab: function (e) {
            const idx = e.currentTarget.dataset.idx;
            this.setData({
                selectedTabIndex: idx
            });
        },

        loadBmsInfo: function () {
            common.loading();
            const pData = {
                battery_sn: this.battery_sn
            };
            const that = this;
            http.postApi(config.API_BATTERY_BMS_INFO, pData, function (resp) {
                uni.hideLoading();
                if (resp.data.code === 200) {
                    const bmsInfo = resp.data.data.bmsInfo;
                    if (!bmsInfo) {
                        common.simpleToast('BMS数据暂无');
                        setTimeout(function () {
                            uni.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    }
                    that.setData({
                        bmsInfo: resp.data.data.bmsInfo
                    });
                } else {
                    common.simpleToast(resp.data.msg);
                }
            });
        }
    }
};
</script>
<style>
@import './bmsbd02.css';
</style>
