<template>
    <div class="storage-item">
        <div class="container">
            <div class="top">
                <div class="container-header">
                    <div class="battery" :class="showCode == 0 ? 'showCodeActive' : ''" @click="actshowCode(0)">
                        {{ StateNum }}
                    </div>
                    <div class="a-gun battery" :class="showCode == 1 ? 'showCodeActive' : ''" @click="actshowCode(1)">
                        A枪
                    </div>
                    <div class="b-gun battery" :class="showCode == 2 ? 'showCodeActive' : ''" @click="actshowCode(2)">
                        B枪
                    </div>
                </div>
                <div class="container-code">
                    <div class="container-code-one" :class="statustext[1]" v-if="showCode == 0">
                        {{ statustext[0] }}
                    </div>
                    <div class="container-code-one" :class="statustextA[1]" v-if="showCode == 1">
                        {{ statustextA[0] }}
                    </div>
                    <div class="container-code-one" :class="statustextB[1]" v-if="showCode == 2">
                        {{ statustextB[0] }}
                    </div>
                    <div class="container-code-two" v-if="showCode == 0">
                        <img src="../assets/d5.png" mode="" v-if="chargerInfoVo.chgState === 2"
                            @click="sendCharger('断电')" />
                        <img src="../assets/c1.png" mode="" v-if="chargerInfoVo.chgState === 0 || chargerInfoVo.chgState === 3
                            " @click="sendCharger('充电')" />
                        <img src="../assets/d6.png" mode="" v-if="chargerInfoVo.chgState !== 2" />
                        <img src="../assets/c2.png" mode="" v-if="chargerInfoVo.chgState !== 0 && chargerInfoVo.chgState !== 3
                            " />
                    </div>
                    <div class="container-code-two" v-if="showCode == 1">
                        <img src="../assets/d5.png" mode="" v-if="chargerInfoOneVo.chgState === 2" @click="
                            sendChargerOut(
                                '断电',
                                batInfoOneVo.gunCode,
                                chargerInfoOneVo.chargerCode
                            )
                            " />
                        <img src="../assets/c1.png" mode="" v-if="chargerInfoOneVo.chgState === 0 ||
                            chargerInfoOneVo.chgState === 3"
                            @click="sendChargerOut('充电', batInfoOneVo.gunCode, chargerInfoOneVo.chargerCode)" />
                        <img src="../assets/d6.png" mode="" v-if="chargerInfoOneVo.chgState !== 2" />
                        <img src="../assets/c2.png" mode="" v-if="chargerInfoOneVo.chgState !== 0 &&
                            chargerInfoOneVo.chgState !== 3
                            " />
                    </div>
                    <div class="container-code-two" v-if="showCode == 2">
                        <img src="../assets/d5.png" mode="" v-if="chargerInfoTwoVo.chgState === 2"
                            @click="sendChargerOut('断电', batInfoTwoVo.gunCode, chargerInfoTwoVo.chargerCode)" />
                        <img src="../assets/c1.png" mode="" v-if="chargerInfoTwoVo.chgState === 0 ||
                            chargerInfoTwoVo.chgState === 3
                            " @click="sendChargerOut('充电', batInfoTwoVo.gunCode, chargerInfoTwoVo.chargerCode)" />
                        <img src="../assets/d6.png" mode="" v-if="chargerInfoTwoVo.chgState !== 2" />
                        <img src="../assets/c2.png" mode="" v-if="chargerInfoTwoVo.chgState !== 0 &&
                            chargerInfoTwoVo.chgState !== 3
                            " />
                    </div>
                </div>
                <BatteryStatus :storeCode="storeCode" :comState="comState" :sn="sn" :soc="soc" :showCode="showCode"
                    :chgEstimatedTime="chgEstimatedTime" v-if="showCode == 0"></BatteryStatus>
                <BatteryStatus :storeCode="batInfoOneVo.storeCode" :comState="chargerInfoOneVo.comState"
                    :sn="batInfoOneVo.sn" :soc="batInfoOneVo.soc" :chgEstimatedTime="chargerInfoOneVo.chgEstimatedTime"
                    :showCode="showCode" v-if="showCode == 1"></BatteryStatus>
                <BatteryStatus :storeCode="batInfoTwoVo.storeCode" :comState="chargerInfoTwoVo.comState"
                    :sn="batInfoTwoVo.sn" :soc="batInfoTwoVo.soc" :chgEstimatedTime="chargerInfoTwoVo.chgEstimatedTime"
                    :showCode="showCode" v-if="showCode == 2"></BatteryStatus>
                <div class="battery-info">
                    <div class="vim" v-if="showCode == 1">
                        <span>vin码</span><span>{{ batInfoOneVo.vin || "-" }}</span>
                    </div>
                    <div class="vim" v-if="showCode == 2">
                        <span>vin码</span><span>{{ batInfoTwoVo.vin || "-" }}</span>
                    </div>
                    <BatteryInfo :num="current" unit="A" text="电流" v-if="showCode == 0"></BatteryInfo>
                    <BatteryInfo :num="batInfoOneVo.current" unit="A" text="电流" v-if="showCode == 1"></BatteryInfo>
                    <BatteryInfo :num="batInfoTwoVo.current" unit="A" text="电流" v-if="showCode == 2"></BatteryInfo>
                    <BatteryInfo :num="voltage" unit="V" text="电压" v-if="showCode == 0"></BatteryInfo>
                    <BatteryInfo :num="batInfoOneVo.voltage" unit="V" text="电压" v-if="showCode == 1"></BatteryInfo>
                    <BatteryInfo :num="batInfoTwoVo.voltage" unit="V" text="电压" v-if="showCode == 2"></BatteryInfo>
                    <BatteryInfo :num="soh" unit="%" text="SOH" v-if="showCode == 0"></BatteryInfo>
                    <div class="info-right" :style="chargerColor" v-if="showCode == 0">
                        <img :src="require('../assets/' + `s${chargerState[0]}` + '.png')" /><span>{{ chargerState[1]
                        }}</span>
                    </div>

                    <div class="info-right" :style="chargerColorA" v-if="showCode == 1">
                        <img :src="require('../assets/' + `s${chargerStateA[0]}` + '.png')" /><span>{{ chargerStateA[1]
                        }}</span>
                    </div>
                    <div class="info-right" :style="chargerColorB" v-if="showCode == 2">
                        <img :src="require('../assets/' + `s${chargerStateB[0]}` + '.png')" /><span>{{ chargerStateB[1]
                        }}</span>
                    </div>
                </div>
            </div>
            <div class="battery-charger">
                <BatteryCharger :chargerCode="chargerInfoVo.chargerCode" :comState="chargerInfoVo.comState"
                    v-if="chargerInfoVo.chargerCode && showCode == 0"></BatteryCharger>
                <BatteryCharger :chargerCode="chargerInfoOneVo.chargerCode" :comState="chargerInfoOneVo.comState"
                    v-if="chargerInfoOneVo.chargerCode && showCode == 1"></BatteryCharger>
                <BatteryCharger :chargerCode="chargerInfoTwoVo.chargerCode" :comState="chargerInfoTwoVo.comState"
                    v-if="chargerInfoTwoVo.chargerCode && showCode == 2"></BatteryCharger>
                <div class="battery-info" v-if="chargerInfoVo.chargerCode && showCode == 0">
                    <BatteryInfo :num="chargerInfoVo.demandCurrent" unit="A" text="需求电流"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoVo.demandVoltage" unit="V" text="需求电压"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoVo.chgCurrent" unit="A" text="充电电流"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoVo.chgVoltage" unit="V" text="充电电压"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoVo.chgCapacity" unit="kwh" text="充电电量"></BatteryInfo>
                </div>
                <div class="battery-info" v-if="chargerInfoOneVo.chargerCode && showCode == 1">
                    <BatteryInfo :num="chargerInfoOneVo.demandCurrent" unit="A" text="需求电流"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoOneVo.demandVoltage" unit="V" text="需求电压"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoOneVo.chgCurrent" unit="A" text="充电电流"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoOneVo.chgVoltage" unit="V" text="充电电压"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoOneVo.chgCapacity" unit="kWh" text="累计充电电量"></BatteryInfo>
                </div>
                <div class="battery-info" v-if="chargerInfoTwoVo.chargerCode && showCode == 2">
                    <BatteryInfo :num="chargerInfoTwoVo.demandCurrent" unit="A" text="需求电流"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoTwoVo.demandVoltage" unit="V" text="需求电压"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoTwoVo.chgCurrent" unit="A" text="充电电流"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoTwoVo.chgVoltage" unit="V" text="充电电压"></BatteryInfo>
                    <BatteryInfo :num="chargerInfoTwoVo.chgCapacity" unit="kWh" text="累计充电电量"></BatteryInfo>
                </div>
                <div class="battery-charger-btn">
                    <div class="Plug-gun-charging" @click="ChargingMode(chgType)">
                        {{ chgType == 0 ? "插枪充电" : "线束充电" }}
                    </div>
                    <div class="Settings Plug-gun-charging" @click="set">设置</div>
                </div>
                <img src="../assets/nodata.png" class="nodata" v-show="!chargerInfoVo.chargerCode" />
            </div>
        </div>
    </div>
</template>

<script>
import BatteryInfo from "./BatteryInfo.vue";
import BatteryStatus from "./BatteryStatus.vue";
import BatteryCharger from "./BatteryCharger.vue";
import { gettoggleChargeWay, getgunStartCharge, gunStopCharge } from "@/api/powerCharge.js";
import { startCharge, stopCharge } from '@/api/equipment'
export default {
    name: "StorageInfo",
    props: [
        "storeCode",
        "comState",
        "sn",
        "soc",
        "chgEstimatedTime",
        "current",
        "voltage",
        "minTemperature",
        "maxTemperature",
        "maxCellVoltage",
        "minCellVoltage",
        "soh",
        "storeState",
        "chargerInfoVo",
        "chgType",
        "batInfoOneVo",
        "batInfoTwoVo",
        "chargerInfoOneVo",
        "chargerInfoTwoVo",
        "showCodeNum",
    ],
    components: {
        BatteryInfo,
        BatteryStatus,
        BatteryCharger,
    },
    data() {
        return {
            showCode: 0, //0电池1A枪2B枪
            Markers: 0, //标记 0代表没点击过 1代表点击过
        };
    },
    mounted() {
        this.showCode = this.showCodeNum; //根据后端返的数据动态渲染当前状态
    },
    watch: {
        showCodeNum(newshowCodeNum, oldshowCodeNum) {
            if (this.Markers == 0) {
                this.showCode = newshowCodeNum; //没点击过  根据后端数据实时渲染不同的数据
            }
        },
    },
    methods: {
        //页面切换
        actshowCode(code) {
            this.showCode = code;
            this.Markers = 1;
        },
        //打开弹窗设置
        set() {
            let obj = {
                comState: this.chgType,
                cangnum: this.chargerInfoVo.chargerCode,
            };
            this.$emit("showDislog", obj);
        },
        //切换仓内仓外
        ChargingMode(chgType) {
            //停止充电后才能切换
            let data = {
                chargerId: this.chargerInfoVo.chargerCode,
                chargerWay: chgType == 1 ? "0" : "1",
                controlPriority: 0,
                powerControl: 0,
                powerToggle: 0,
            };
            if (chgType == 0 && this.chargerInfoVo.chgState == 2) {
                //仓内充电 充电时不允许切换
                this.$message({type: 'warning',message:"请断电后再切换",});
            } else if (
                (chgType == 1 && this.chargerInfoOneVo.chgState == 2) ||
                this.chargerInfoTwoVo.chgState == 2
            ) {
                this.$message({type: 'warning',message:"请断电后再切换",});
            } else {
                gettoggleChargeWay(data).then((res) => {
                    this.$message({
                        type: res.code === 0 ? "success" : "error",
                        message: res.code === 0 ? "切换成功!" : "切换失败!",
                    });
                });
            }
        },
        //仓内充电、断电操作
        sendCharger(name) {
            let params = {};
            if (
                this.chargerInfoVo.chgState === 0 ||
                this.chargerInfoVo.chgState === 3
            ) {
                params = {
                    chargerId: this.chargerInfoVo.chargerCode * 1,
                    sn: this.sn,
                };
            } else if (this.chargerInfoVo.chgState === 2) {
                params = {
                    chargerId: this.chargerInfoVo.chargerCode * 1,
                };
            }
            this.$confirm(`请确认是否执行${name}操作？`, `${name}确认`, {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    //充电操作
                    if (this.chargerInfoVo.chgState === 0 || this.chargerInfoVo.chgState === 3) {
                        this.$message({
                            type: 'success',
                            message: '充电指令已下发!',
                        })
                        const res = await startCharge(params)
                        // console.log(res.code)
                        this.$message({
                            type: res.code === 0 ? 'success' : 'warning',
                            message: res.code === 0 ? '充电成功!' : '充电失败!',
                        })
                    }
                    //断电操作
                    else if (this.chargerInfoVo.chgState === 2) {
                        this.$message({
                            type: 'success',
                            message: '断电指令已下发!',
                        })
                        const res = await stopCharge(params)
                        this.$message({
                            type: res.code === 0 ? 'success' : 'warning',
                            message: res.code === 0 ? '断电成功!' : '断电失败!',
                        })
                    }
                })
                .catch(err => {
                    console.log(err)

                })

        },
        //仓外充电·断电操作
        sendChargerOut(name, guncode, chargerId) {
            let params = {};
            if (
                this.chargerInfoOneVo.chgState === 0 ||
                this.chargerInfoOneVo.chgState === 3 ||
                this.chargerInfoTwoVo.chgState === 0 ||
                this.chargerInfoTwoVo.chgState === 3
            ) {
                params = {
                    chargerId: chargerId * 1,
                    gunNo: guncode,
                    strategy: 4,
                    strategyParam: 0,
                };
            } else if (
                this.chargerInfoOneVo.chgState === 2 ||
                this.chargerInfoTwoVo.chgState === 2
            ) {
                params = {
                    chargerId: chargerId * 1,
                    gunNo: guncode,
                };
            }
            this.$confirm(`请确认是否执行${name}操作？`, `${name}确认`, {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                //充电操作
                if (this.chargerInfoOneVo.chgState === 0 ||
                    this.chargerInfoOneVo.chgState === 3 ||
                    this.chargerInfoTwoVo.chgState === 0 ||
                    this.chargerInfoTwoVo.chgState === 3) {
                    this.$message({
                        type: 'success',
                        message: '充电指令已下发!',
                    })
                    const res = await getgunStartCharge(params)
                    // console.log(res.code)
                    this.$message({
                        type: res.code === 0 ? 'success' : 'warning',
                        message: res.code === 0 ? '充电成功!' : '充电失败!',
                    })
                }
                //断电操作
                else if (this.chargerInfoOneVo.chgState === 2 ||
                    this.chargerInfoTwoVo.chgState === 2) {
                    this.$message({
                        type: 'success',
                        message: '断电指令已下发!',
                    })
                    const res = await getgunStopCharge(params)
                    this.$message({
                        type: res.code === 0 ? 'success' : 'warning',
                        message: res.code === 0 ? '断电成功!' : '断电失败!',
                    })
                }
            })
                .catch(err => {
                    console.log(err)

                })
        },
    },
    computed: {
        chargerState() {
            if (this.chargerInfoVo.chgState === 1) {
                return [3, " 准备充电"];
            } else if (this.chargerInfoVo.chgState === 2) {
                return [1, " 充电中"];
            } else if (this.chargerInfoVo.chgState === 3) {
                return [2, " 充电完成"];
            } else if (this.chargerInfoVo.chgState === 4) {
                return [8, " 充电失败"];
            } else if (this.chargerInfoVo.chgState === 5) {
                return [6, " 预约"];
            } else if (this.chargerInfoVo.chgState === 6) {
                return [5, " 故障"];
            } else if (this.chargerInfoVo.chgState === 0) {
                return [7, " 空闲"];
            } else {
                return [4, " 无法获取"];
            }
        },
        chargerStateA() {
            if (this.chargerInfoOneVo.chgState === 1) {
                return [3, " 准备充电"];
            } else if (this.chargerInfoOneVo.chgState === 2) {
                return [1, " 充电中"];
            } else if (this.chargerInfoOneVo.chgState === 3) {
                return [2, " 充电完成"];
            } else if (this.chargerInfoOneVo.chgState === 4) {
                return [8, " 充电失败"];
            } else if (this.chargerInfoOneVo.chgState === 5) {
                return [6, " 预约"];
            } else if (this.chargerInfoOneVo.chgState === 6) {
                return [5, " 故障"];
            } else if (this.chargerInfoOneVo.chgState === 0) {
                return [7, " 空闲"];
            } else {
                return [4, " 无法获取"];
            }
        },
        chargerStateB() {
            if (this.chargerInfoTwoVo.chgState === 1) {
                return [3, " 准备充电"];
            } else if (this.chargerInfoTwoVo.chgState === 2) {
                return [1, " 充电中"];
            } else if (this.chargerInfoTwoVo.chgState === 3) {
                return [2, " 充电完成"];
            } else if (this.chargerInfoTwoVo.chgState === 4) {
                return [8, " 充电失败"];
            } else if (this.chargerInfoTwoVo.chgState === 5) {
                return [6, " 预约"];
            } else if (this.chargerInfoTwoVo.chgState === 6) {
                return [5, " 故障"];
            } else if (this.chargerInfoTwoVo.chgState === 0) {
                return [7, " 空闲"];
            } else {
                return [4, " 无法获取"];
            }
        },
        chargerColor() {
            if (this.chargerInfoVo.chgState === 3) {
                return "background-color:rgb(19,33,39,0.9);border:#477c62 solid 1px";
            } else if (
                this.chargerInfoVo.chgState === 1 ||
                this.chargerInfoVo.chgState === 5
            ) {
                return "background-color:rgb(20,38,50,0.9);border:#4a9dab solid 1px";
            } else if (
                this.chargerInfoVo.chgState === 2 ||
                this.chargerInfoVo.chgState === 0
            ) {
                return "";
            } else {
                return "background-color:rgb(32,25,36,0.9);border:#94494d solid 1px";
            }
        },
        chargerColorA() {
            if (this.chargerInfoOneVo.chgState === 3) {
                return "background-color:rgb(19,33,39,0.9);border:#477c62 solid 1px";
            } else if (
                this.chargerInfoOneVo.chgState === 1 ||
                this.chargerInfoOneVo.chgState === 5
            ) {
                return "background-color:rgb(20,38,50,0.9);border:#4a9dab solid 1px";
            } else if (
                this.chargerInfoOneVo.chgState === 2 ||
                this.chargerInfoOneVo.chgState === 0
            ) {
                return "";
            } else {
                return "background-color:rgb(32,25,36,0.9);border:#94494d solid 1px";
            }
        },
        chargerColorB() {
            if (this.chargerInfoTwoVo.chgState === 3) {
                return "background-color:rgb(19,33,39,0.9);border:#477c62 solid 1px";
            } else if (
                this.chargerInfoTwoVo.chgState === 1 ||
                this.chargerInfoTwoVo.chgState === 5
            ) {
                return "background-color:rgb(20,38,50,0.9);border:#4a9dab solid 1px";
            } else if (
                this.chargerInfoTwoVo.chgState === 2 ||
                this.chargerInfoTwoVo.chgState === 0
            ) {
                return "";
            } else {
                return "background-color:rgb(32,25,36,0.9);border:#94494d solid 1px";
            }
        },
        StateNum() {
            if (this.storeState === 0) {
                return "电池(无)";
            } else if (this.storeState === 1) {
                return "电池(有)";
            } else {
                return "异常";
            }
        },
        statustext() {
            if (this.comState === 1) {
                return ["通讯正常", "success"];
            } else {
                return ["通讯异常", "warning"];
            }
        },
        statustextA() {
            if (this.chargerInfoOneVo.comState === 1) {
                return ["已连接", "success"];
            } else {
                return ["未连接", "warning"];
            }
        },
        statustextB() {
            if (this.chargerInfoTwoVo.comState === 1) {
                return ["已连接", "success"];
            } else {
                return ["未连接", "warning"];
            }
        },
    },
};
</script>
<style lang="scss" scoped>
.storage-item {
    width: 19%;
    background-color: #0a101c;
    min-height: 100%;
    margin-right: 22px;

    .container {
        padding: 12px 23px 20px 23px;
        padding-bottom: 0px;
        color: #404a63;
        font-size: 15px;

        .top {
            height: 340px;
        }

        .container-header {
            display: flex;
            justify-content: space-between;

            .battery {
                width: 112px;
                height: 32px;
                background: #1c263a;
                color: #deeff9;
                line-height: 32px;
                text-align: center;
                margin-bottom: 13px;
                cursor: pointer;
            }

            .a-gun {
                width: 78px;
                margin-left: 15px;
            }

            .b-gun {
                width: 78px;
                margin-left: 15px;
            }

            .showCodeActive {
                background-color: #636c97;
            }
        }

        .container-code {
            display: flex;
            margin-bottom: 10px;
            justify-content: space-between;

            .container-code-one {
                font-size: 16px;
                color: #91fdb9;
            }

            .success {
                color: #92fdb9;
            }

            .warning {
                color: #ff4d4f;
            }

            .container-code-two {
                display: flex;

                img {
                    width: 60px;
                    height: 24px;
                    cursor: pointer;
                }

                img:first-child {
                    margin-right: 20px;
                }
            }
        }

        .battery-info {
            display: flex;
            margin-top: 16px;
            justify-content: space-between;
            flex-wrap: wrap;

            .vim {
                width: 100%;
                margin: 0px 0px 12px 0px;

                span {
                    font-size: 14px;
                    color: #414a63;
                }

                span:last-child {
                    display: inline-block;
                    margin-left: 10px;
                    font-weight: 600;
                    color: #fff;
                }
            }

            .info-right {
                width: 46%;
                height: 34px;
                line-height: 32px;
                text-align: center;
                font-size: 14px;
                font-weight: 500;
                color: white;
                background-color: #0e1f31;
                border: #1b71a4 solid 1px;
                display: flex;
                justify-content: center;
                align-items: center;

                span {
                    white-space: nowrap;
                }

                img {
                    width: 22px;
                    height: 22px;
                }
            }

            .info-warning {
                width: 46%;
                background-color: #201823;
                border: #834246 solid 1px;
                height: 34px;
                line-height: 32px;
                text-align: center;
                font-size: 14px;
                font-weight: 500;
                color: white;

                img {
                    vertical-align: middle;
                }
            }
        }

        .battery-charger {
            width: 100%;
            min-height: 228px;

            .nodata {
                position: relative;
                left: 40%;
                margin-top: 60px;
                width: 66px;
                height: 66px;
            }
        }
    }
}

.battery-charger-btn {
    display: flex;
    position: absolute;
    bottom: 20px;

    .Plug-gun-charging {
        width: 135px;
        height: 32px;
        background: #69b889;
        text-align: center;
        line-height: 32px;
        font-size: 16px;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 500;
        color: #deeff9;
        cursor: pointer;
    }

    .Settings {
        background: #636c97;
        margin-left: 20px;
        cursor: pointer;
    }
}
</style>
