<template>
    <div id="nitrogen3D" style="width: 100%; height: 100%; position: absolute; overflow: hidden"></div>
    <div id="nitrogenCss3D" class="threejs-Object-CSS"
        style="width: 100%; height: 100%; position: absolute; pointer-events: none; overflow: hidden; z-index: 2; top: 0px; left: 0px">
        <a-spin :spinning="loading" />
        <div v-for="groupNum in monitorDataGroupNum" :key="groupNum" class="modal-monitor">
            <fourBorderBg :class="`kyj${groupNum}`" :id="`nitrogenMonitor${groupNum}`">
                <div class="title">空压机{{ groupNum }} </div>
                <template v-for="(preMonitor, preMonitorIndex) in preMonitorList" :key="preMonitorIndex">
                    <div class="monitor-item">
                        <span class="monitor-title">{{ preMonitor.title }}:</span>
                        <span class="monitor-val"><span class="val">{{ monitorData[preMonitor.code] ?
            formatNum(monitorData[preMonitor.code]) : '-' }}</span><span class="unit">{{
            preMonitor.unit }}</span></span>
                    </div>

                </template>
            </fourBorderBg>
        </div>
    </div>
    <div class="nitrogen-home">
        <div class="nitrogen-container">
            <div class="top-box">

                <!-- 左边监测数据 -->
                <div class="lr-box left-box">
                    <ventBox1>
                        <template #title>
                            <div>注氮机监测</div>
                        </template>
                        <template #container>
                            <div class="input-item" v-for="(preFan, index) in nitrogenMonitorData" :key="index">
                                <div class="title">{{ preFan.title }}</div>
                                <div class="value">{{
            monitorData[preFan.code] >= 0 ? formatNum(Number(monitorData[preFan.code])) : '-'
        }}</div>

                                <div class="unit">{{ preFan.unit !== 'signal' ? `(${preFan.unit})` : '' }}</div>
                            </div>
                        </template>
                    </ventBox1>
                    <div class="item item-l" v-for="groupNum in monitorDataGroupNum" :key="groupNum">
                        <ventBox1>
                            <template #title>
                                <div>空压机{{ groupNum }}</div>
                            </template>
                            <template #container>
                                <div class="monitor-box">
                                    <div class="monitor-item">
                                        <div class="state-item" v-for="(preFan, index) in preFanMonitorData"
                                            :key="index">
                                            <div class="state-title">{{ preFan.title + (preFan.unit !== 'signal' ?
            `(${preFan.unit})` : '') }}</div>

                                            <div :class="{
            'signal-round': true,
            'signal-round-warning': monitorData[preFan.code] == '1',
            'signal-round-run': monitorData[preFan.code] == '0',
            'signal-round-gry':monitorData[preFan.code] != '0' && monitorData[preFan.code] != '1'
        }"></div>
                                        </div>
                                    </div>
                                </div>
                            </template>
                        </ventBox1>
                    </div>
                </div>
                <!-- 右边控制状态 -->
                <div class="lr-box right-box">
                    <ventBox1 class="vent-margin-t-10">
                        <template #title>
                            <div>设备实时监测曲线</div>
                        </template>
                        <template #container>
                            <BarAndLine v-if="chartsColumns.length > 0" xAxisPropType="readTime"
                                :dataSource="echartData" height="340px" :chartsColumns="chartsColumns"
                                chartsType="listMonitor" :option="echatsOption" />
                        </template>
                    </ventBox1>
                    <div ref="playerRef" style="height: auto; width: 100%; margin-top: 10px"></div>
                </div>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup name="nitrogenHome">
import { onMounted, onUnmounted, ref, watch, reactive, defineProps, nextTick, inject } from 'vue';
import ventBox1 from '/@/components/vent/ventBox1.vue';
import fourBorderBg from '../../../comment/components/fourBorderBg.vue';
import { mountedThree, destroy, setModelType } from '../nitrogen.threejs';
import { getDevice } from '../nitrogen.api';
import BarAndLine from '/@/components/chart/BarAndLine.vue';
import { preMonitorList, preFanMonitorData, nitrogenMonitorData } from '../nitrogen.data.lt';
import { formatNum } from '/@/utils/ventutil';
import { useCamera } from '/@/hooks/system/useCamera';
import lodash from 'lodash';

const props = defineProps({
    deviceId: {
        type: String,
        require: true,
    },
    modalType: {
        type: String,
        require: true,
    },
});
const playerRef = ref();
const refresh = ref(false);
const loading = ref(true);

// const kyjs = ['1号空压机', '1号空压机', '1号空压机', '1号空压机'];
const flvURL1 = () => {
    // return `https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv`;
    return '';
};
const echatsOption = {
    grid: {
        top: '35%',
        left: '15%',
        right: '20px',
        bottom: '8%',
        // containLabel: true
    },
    toolbox: {
        feature: {},
    },
};

const monitorDataGroupNum = ref(1);

const chartsColumns = ref([
    {
        legend: '瞬时流量',
        seriesName: '(m³/h)',
        ymax: 2000,
        yname: 'm³/h',
        linetype: 'line',
        yaxispos: 'left',
        color: '#FDB146',
        sort: 1,
        xRotate: 0,
        dataIndex: 'InputFlux',
    },
]);
const { getCamera, removeCamera } = useCamera();

const monitorData = ref({});

//图表数据
let echartData = ref<any>([]);

// https获取监测数据
let timer: null | NodeJS.Timeout = null;
async function getMonitor(flag?) {
    if (Object.prototype.toString.call(timer) === '[object Null]') {
        return new Promise(async (resolve) => {
            timer = await setTimeout(
                async () => {
                    if (props.deviceId) {
                        await getDataSource(props.deviceId);
                    }
                    if (timer) {
                        timer = null;
                    }
                    resolve(null);
                    await getMonitor();
                },
                flag ? 0 : 3000
            );
        });
    }
}

async function getDataSource(systemID) {
    const res = await getDevice({ devicetype: 'sys', systemID, type: 'all' });
    if (res) {
        const result = res;
        if (!result || result.msgTxt.length < 1) return;
        result.msgTxt.forEach((item) => {
            if (item.type && item.type.startsWith('nitrogen')) {

                monitorData.value = Object.assign(item['datalist'][0], item['datalist'][0]['readData']);

                const airCompressor = { readTime: monitorData.value['readTime'].substring(11) };
                const dataArr = lodash.cloneDeep(echartData.value);
                //图表数据
                if (dataArr.length <= 5) {
                    airCompressor[`InputFlux`] = monitorData.value['InputFlux'] || 0;
                    dataArr.push(airCompressor);
                } else {
                    dataArr.shift();
                    dataArr.push(airCompressor);
                }
                echartData.value = dataArr;
            }
        });
        refresh.value = true;
        nextTick(() => {
            refresh.value = false;
        });
    }
}

watch([monitorDataGroupNum, loading], ([newMonitorDataGroupNum, newLoading]) => {
    nextTick(() => {
        if (newMonitorDataGroupNum && !newLoading) {
            setModelType(props.modalType, newMonitorDataGroupNum);
        }
    });
});

onMounted(async () => {
    await getMonitor(true);
    await mountedThree().then(() => {
        loading.value = false;
    });
    await getCamera(props.deviceId, playerRef.value);
});

onUnmounted(() => {
    destroy();
    removeCamera();
    if (timer) {
        clearTimeout(timer);
        timer = undefined;
    }
});
</script>

<style lang="less" scoped>
@ventSpace: zxm;

.nitrogen-box {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
}

#nitrogenCss3D {
    .modal-monitor {
        width: 200px;
        position: absolute;
        left: 0px;
        top: 0px;
    }

    &:deep(.win) {
        margin: 0 !important;
        background: #00000044;
    }
}

.nitrogen-home {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    pointer-events: none;
    top: 20px;

    .nitrogen-container {
        width: 100%;
        height: calc(100% - 100px);
        display: flex;
        justify-content: space-between;
        margin-bottom: 100px;

        .device-state {
            width: 100%;
            position: absolute;
            top: 20px;
            color: #e90000;
            display: flex;
            justify-content: center;
            font-size: 20px;
        }

        .top-box {
            width: 100%;
            padding: 10px;
            overflow: hidden;
            display: flex;
            justify-content: space-between;
            // margin-top: 40px;

            .center-item-box {
                position: absolute;
                left: 50%;
                top: 50px;
                transform: translate(-48%, 0);
                width: calc(100% - 720px);
                height: 50px;
                display: flex;
                align-items: center;
                pointer-events: auto;

                .top-left {
                    display: flex;
                    flex: 1.5;
                    color: #fff;

                    .button-box {
                        position: relative;
                        width: auto;
                        height: 32px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        border-radius: 5px;
                        color: #fff;
                        padding: 10px 15px;
                        margin: 0px 10px;
                        box-sizing: border-box;
                        cursor: pointer;
                        background: linear-gradient(#1fa6cb, #127cb5);

                        &:hover {
                            background: linear-gradient(#1fa6cbcc, #127cb5cc);
                        }
                    }
                }

                .top-center {
                    display: flex;
                    flex: 1;
                    justify-content: center;
                    align-items: center;
                    font-size: 20px;
                    color: #fff;

                    .top-c-label {
                        color: yellow;
                    }
                }

                .top-right {
                    display: flex;
                    flex: 2;
                    justify-content: right;
                    align-items: center;
                    color: #fff;

                    .control-type {
                        display: flex;
                        color: #fff;

                        .control-title {
                            color: #73e8fe;
                        }
                    }
                }
            }

            .lr-box {
                height: 90%;
                display: flex;
                flex-direction: column;
                position: relative;
                // overflow: hidden;
                z-index: 9999;
                pointer-events: auto;
                overflow-y: auto;
                overflow-x: hidden;
            }

            .item {
                width: 305px;
                height: 450px;
                position: relative;
                border-radius: 5px;
                margin-top: 10px;
                margin-bottom: 0px;
                pointer-events: auto;
                color: #fff;
                // overflow: hidden;

                &:first-child {
                    margin-top: 0px;
                }

                .base-title {
                    color: #fff;
                    margin-bottom: 8px;
                    padding-left: 10px;
                    position: relative;
                    font-size: 16px;

                    &::after {
                        content: '';
                        position: absolute;
                        display: block;
                        width: 4px;
                        height: 12px;
                        top: 7px;
                        left: 0px;
                        background: #45d3fd;
                        border-radius: 4px;
                    }
                }

                .monitor-item {
                    width: 100%;
                    display: flex;
                    flex-direction: row;
                    flex-wrap: wrap;

                    .state-item {
                        width: 50%;
                        padding: 5px;
                        display: flex;
                        align-items: center;
                        justify-content: space-between;

                        .state-title {
                            width: 100px;
                            color: #ffffffdd;
                            flex: 9;
                            font-size: 14px;

                            .unit {
                                // color: #ffffffbb;
                            }
                        }

                        .state-val {
                            flex: 1;
                            color: #e4a300;
                            margin-right: 5px;
                            text-align: right;
                            font-size: 14px;
                        }
                    }
                }

                .signal-box {
                    margin: 5px 0;
                    display: flex;
                    align-items: center;

                    .signal-title {
                        color: #7af5ff;
                        margin: 0 5px;
                    }

                    &:last-child {
                        margin-right: 0px;
                    }
                }

                .list-item {
                    padding: 0 10px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    .item-data-key {
                        color: #ffffff99;
                    }
                }

                .item-data-box {
                    color: #fff;

                    .state-icon {
                        display: inline-block;
                        width: 12px;
                        height: 12px;
                        border-radius: 12px;
                    }

                    .open {
                        border: 5px solid #133a56;
                        background: #4ecb73;
                    }

                    .close {
                        border: 5px solid #192961;
                        background: #6d7898;
                    }
                }
            }

            .item-l {
                width: 100%;

                .monitor-box {
                    width: 100%;

                    .parameter-title {
                        position: relative;
                        width: 100%;
                        height: 14px;
                        margin-top: 10px;

                        .icon,
                        span {
                            position: absolute;
                            top: -10px;
                        }
                    }

                    .group-parameter-title {
                        background-image: linear-gradient(to right, #39a3ff50, #39a3ff00);

                        .icon {
                            left: -12px;
                            top: -17px;
                        }

                        span {
                            left: 18px;
                        }

                        .item-col {
                            background-image: linear-gradient(to right, #39a3ff00, #39a3ff10);
                        }
                    }

                    .device-parameter-title {
                        background-image: linear-gradient(to right, #3df6ff40, #3df6ff00);

                        .icon {
                            left: -10px;
                            top: -14px;
                        }

                        span {
                            left: 18px;
                        }

                        .item-col {
                            background-image: linear-gradient(to right, #3df6ff10, #3df6ff00);
                        }
                    }
                }
            }

            .right-box {
                width: 300px;
                margin-top: 50px;
            }

            .left-box {
                width: 335px;
                margin-top: 80px;

                .input-item {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    padding: 4px 8px;
                    margin: 6px 0;
                    background-image: linear-gradient(to right, #39deff15, #3977e500);

                    .title {
                        width: 200px;
                    }

                    .title-auto {
                        width: auto;
                    }

                    .input-value {
                        width: 80px;
                        height: 28px;
                        line-height: 28px !important;
                        background: transparent !important;
                        border-color: #228da2 !important;
                        color: #fff !important;
                    }

                    .value {
                        width: 100px;
                        color: #00d8ff;
                        padding-right: 20px;
                    }

                    .unit {
                        width: 80px;
                    }
                }
            }
        }

        &:deep(.win) {
            width: 100%;
            margin: 0 !important;
        }
    }
}

&:deep(.main) {
    .title {
        height: 34px;
        text-align: center;
        font-weight: 600;
        color: #7af5ff;
        // background-image: url('../../../assets/img/yfj/light.png');
        background-repeat: no-repeat;
        background-position-x: center;
        background-position-y: 100%;
        background-size: 80%;
        font-size: 16px;
    }

    .monitor-item {
        width: 200px;
        display: flex;
        flex-direction: row;
        width: auto;
        margin-bottom: 3px;

        .monitor-val {
            color: #ffb700;
            display: flex;
            width: auto;

            .val {
                width: 80px;
                font-size: 14px;
            }

            .unit {
                color: #ffffffbb;
                font-size: 14px;
            }
        }
    }

    .monitor-title {
        width: 100px;
        color: #7af5ff;
        font-weight: 400;
        font-size: 14px;
    }

    .signal-item {
        display: flex;
        justify-content: space-between;

        // margin-bottom: 5px;
        .signal-round {
            display: inline-block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            margin: 0 10px;
            position: relative;

            &::after {
                display: block;
                content: '';
                position: absolute;
                width: 12px;
                height: 12px;
                top: -2px;
                left: -2px;
                border-radius: 50%;
            }
        }

        .signal-round-gry {
            background-color: #858585;

            &::after {
                background-color: #85858544;
                box-shadow: 0 0 1px 1px #85858599;
            }
        }

        .signal-round-run {
            background-color: #67fc00;

            &::after {
                background-color: #67fc0044;
                box-shadow: 0 0 1px 1px #c6ff77;
            }
        }

        .signal-round-warning {
            background-color: #e9170b;

            &::after {
                background-color: #e9170b44;
                box-shadow: 0 0 1px 1px #e9170b;
            }
        }
    }
}

:deep(.zxm-radio-wrapper) {
    color: #fff !important;
}
</style>