<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">
                        <text class="point-name ellipsis">{{ pointInfo.pointName }}</text>
                        <view class="meta-row">
                            <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 class="params-section">
                    <view
                        v-for="(rowItems, rowIdx) in chunkedChildren(pointInfo.children || [])"
                        :key="rowIdx"
                        class="double-row"
                    >
                        <view v-for="(child, childIdx) in rowItems" :key="childIdx" class="param-group">
                            <view class="param-title">
                                <text>{{ child.name }}</text>
                            </view>
                            <view 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>
        </scroll-view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            pointInfo: {},
            phaseColors: [
                { name: 'A相', color: '#F1B22E' },
                { name: 'B相', color: '#2BA471' },
                { name: 'C相', color: '#D54941' },
            ],
        }
    },
    onLoad(options) {
        if (options.point) {
            this.pointInfo = JSON.parse(decodeURIComponent(options.point))
        }
    },
    methods: {
        // 将指标列表按每行两个分组
        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 {
    display: block;
    font-size: 30rpx;
    font-weight: 700;
    color: #333333;
    margin-bottom: 8rpx;
}

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

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

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

.legend-row {
    display: flex;
    gap: 20rpx;
    align-items: center;
    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;
}

.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: #f9fafb;
    border-radius: 16rpx;
    padding: 16rpx 8rpx 12rpx;
}

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

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

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

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

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

.ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
</style>
