<template>
    <view class="target-info-page">
        <scroll-view class="target-info-scroll" scroll-y :show-scrollbar="false">
            <view class="monitor-card card">
                <view class="card-header">
                    <view class="event-icon">
                        <Cn-icon-transient name="监测点" />
                    </view>
                    <view class="card-header-info">
                        <view class="point-name-row">
                            <text class="point-name ellipsis">{{ pointInfo.pointName || '-' }}</text>
                            <text class="event-tag ml10" :class="pointInfo.runStatus == 0 ? 'zx-tag' : 'lx-tag'">
                                {{ pointInfo.runStatus == 0 ? '在线' : '离线' }}
                            </text>
                        </view>
                        <view class="meta-row">
                            <text v-if="pointInfo.engineeringName" class="meta-item ellipsis">
                                工程：{{ pointInfo.engineeringName }}
                            </text>
                            <text class="meta-item ellipsis">项目：{{ pointInfo.projectName || '-' }}</text>
                            <text class="meta-item ellipsis">设备：{{ pointInfo.deviceName || '-' }}</text>
                        </view>
                        <text class="meta-time ellipsis">最新数据时间：{{ pointInfo.dataTime || '-' }}</text>
                    </view>
                </view>

                <view class="legend-row">
                    <view class="legend-item" v-for="phase in phaseColors" :key="phase.name">
                        <view class="legend-dot" :style="{ background: phase.color }" />
                        <text class="legend-text">{{ phase.name }}</text>
                    </view>
                </view>

                <view v-if="moreChildren.length" class="params-section">
                    <view v-for="(rowItems, rowIdx) in chunkedChildren(moreChildren)" :key="rowIdx" class="double-row">
                        <view v-for="(child, childIdx) in rowItems" :key="child.targetId || childIdx"
                            class="param-group">
                            <view class="param-title">
                                <text>{{ child.name }}{{ child.unit ? ` (${child.unit})` : '' }}</text>
                            </view>
                            <view v-if="hasTPhaseData(child)" class="phase-single">
                                <text class="phase-value-vertical phase-value-vertical--neutral">{{ child.T }}</text>
                            </view>
                            <view v-else class="phase-vertical">
                                <view class="phase-item-vertical">
                                    <text class="phase-value-vertical" :style="{ color: phaseColors[0].color }">{{
                                        child.A }}</text>
                                </view>
                                <view class="phase-divider" />
                                <view class="phase-item-vertical">
                                    <text class="phase-value-vertical" :style="{ color: phaseColors[1].color }">{{
                                        child.B }}</text>
                                </view>
                                <view class="phase-divider" />
                                <view class="phase-item-vertical">
                                    <text class="phase-value-vertical" :style="{ color: phaseColors[2].color }">{{
                                        child.C }}</text>
                                </view>
                            </view>
                        </view>
                    </view>
                </view>
                <view v-else class="empty-wrap">
                    <Cn-empty msg="暂无指标数据" :paddingTop="120"></Cn-empty>
                </view>
            </view>
        </scroll-view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            pointInfo: {},
            phaseColors: [
                { name: 'A相', color: '#DAA520' },
                { name: 'B相', color: '#2E8B57' },
                { name: 'C相', color: '#D54941' },
            ],
        }
    },
    computed: {
        moreChildren() {
            return this.pointInfo.children || []
        },
    },
    onLoad() {
        const point = uni.getStorageSync('monitorPointDetail')
        if (point) {
            this.pointInfo = { ...point }
            if (point.pointName) {
                uni.setNavigationBarTitle({ title: point.pointName })
            }
        }
    },
    onUnload() {
        uni.removeStorageSync('monitorPointDetail')
    },
    methods: {
        hasTPhaseData(child) {
            return child.T !== undefined && child.T !== null && child.T !== '-'
        },
        chunkedChildren(children) {
            const result = []
            for (let i = 0; i < children.length; i += 2) {
                result.push(children.slice(i, i + 2))
            }
            return result
        },
    },
}
</script>

<style lang="scss" scoped>
.target-info-page {
    min-height: 100vh;
    background: #f7f8fa;
}

.target-info-scroll {
    height: 100vh;
    box-sizing: border-box;
    padding: 20rpx 0 40rpx;
}

.card {
    background: #ffffff;
    border-radius: 10px;
    box-shadow: rgba(0, 0, 0, 0.08) 0px 0px 1px 1px;
    margin: 0 10px;
    overflow: hidden;
}

.monitor-card {
    border: 1rpx solid #eef2f6;
}

.card-header {
    padding: 20rpx 20rpx 12rpx;
    display: flex;
    align-items: center;
    border-bottom: 1rpx solid #eef2f6;
}

.event-icon {
    width: 90rpx;
    height: 90rpx;
    border-radius: 16rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 20rpx;
    background-color: #376cf320;
    flex-shrink: 0;
}

.card-header-info {
    flex: 1;
    min-width: 0;
}

.point-name-row {
    display: flex;
    align-items: center;
    gap: 12rpx;
    margin-bottom: 8rpx;
    min-width: 0;
}

.point-name {
    // flex: 1;
    min-width: 0;
    font-size: 30rpx;
    font-weight: 700;
    color: #333333;
}

.meta-row {
    display: grid;
    grid-template-columns: 1fr ;
    gap: 6rpx 12rpx;
}

.meta-item {
    font-size: 24rpx;
    color: #666666;
    line-height: 1.2;
}

.meta-time {
    display: block;
    margin-top: 8rpx;
    font-size: 24rpx;
    color: #666666;
    line-height: 1.2;
}

.legend-row {
    display: flex;
    gap: 20rpx;
    align-items: center;
    justify-content: end;
    padding: 16rpx 20rpx;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8rpx;
}

.legend-dot {
    width: 16rpx;
    height: 16rpx;
    border-radius: 50%;
}

.legend-text {
    font-size: 24rpx;
    color: #666666;
}

.params-section {
    padding: 0 16rpx 20rpx;
}

.empty-wrap {
    position: relative;
    min-height: 320rpx;
}

.double-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16rpx;
    margin-bottom: 16rpx;

    &:last-child {
        margin-bottom: 0;
    }
}

.param-group {
    min-width: 0;
    background: #f3f3f3;
    border-radius: 16rpx;
    padding: 16rpx 8rpx 12rpx;
}

.param-title {
    font-size: 24rpx;
    color: #666666;
    margin-bottom: 8rpx;
    padding-left: 4rpx;
}

.phase-vertical {
    display: flex;
    align-items: stretch;
}

.phase-single {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 4rpx;
}

.phase-item-vertical {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 4rpx;
}

.phase-value-vertical {
    font-size: 26rpx;
    font-weight: 700;

    &--neutral {
        color: #333333;
    }
}

.phase-divider {
    width: 1px;
    background: #d2d2d2;
    margin: 8rpx 0;
}

.ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.event-tag {
    flex-shrink: 0;
    font-size: 22rpx;
    padding: 2rpx 10rpx;
    border-radius: 8rpx;
}

.zx-tag {
    background-color: #10b98120;
    color: #10b981;
}

.lx-tag {
    background-color: #ff3b3020;
    color: #ff3b30;
}
</style>
