<template>
    <div class="page-container">
        <div class="left-container">
            <div class="robot-container">
                <div class="robot-state">机器人状态</div>
                <div class="robot-info" v-for="item, index in robotList">
                    <div class="title-box">
                        <div><SvgIcon name="bot" :color="index === 0 ? '#00D4FF' : '#FFB300'" /></div>
                        <div class="title">{{ item.robotName }}</div>
                        <div class="state online">{{ robotStatus[item.status] }}</div>
                    </div>
                    <div class="info-box">
                        <div class="item-box">
                            <div class="item-title">编号</div>
                            <div class="item-detail">{{ item.robotCode }}</div>
                        </div>
                        <div class="item-box">
                            <div class="item-title">当前任务</div>
                            <div class="item-detail">{{ getRunningTaskByRobotId(item.id) }}</div>
                        </div>
                        <div class="item-box">
                            <div class="item-title">电量</div>
                            <div class="item-detail">{{ item.batteryLevel }}%</div>
                        </div>
                        <div class="item-box">
                            <div class="item-title">IP端口</div>
                            <div class="item-detail">{{ `${item.ipAddress}: ${item.port}` }}</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="split-line"></div>
            <div class="task-container">
                <div class="container-title">当前任务</div>
                <div class="task-box" v-for="item, index in taskList">
                    <div class="title">{{ item.taskName }}</div>
                    <div class="task-info">
                        <div>{{ taskStatus[item.status] }}</div>
                        <div>{{ item.remark }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="map-container">
            <div class="title-container">
                <div class="title">
                    <SvgIcon name="map" color='#00D4FF' />
                    <span>实时地图 — 工厂园区</span>
                </div>
                <div class="map-controller">
                    <div class="btn-container">
                        <SvgIcon name="zoom-in" color='#00D4FF' :size="16" @click="zoomCanvas(true)" />
                    </div>
                    <div class="btn-container">
                        <SvgIcon name="zoom-out" color='#00D4FF' :size="16" @click="zoomCanvas(false)" />
                    </div>
                    <div class="btn-container">
                        <SvgIcon name="locate-fixed" color='#00D4FF' :size="16" @click="centerCanvasView" />
                    </div>
                </div>
            </div>

            <div class="map-box">
                <MapCanvas :robotList="robotList" ref="mapCanvasRef" />
            </div>
        </div>
        <div class="right-container">
            <!-- <div class="sensor-container">
                <div class="sensor-title">传感器数据</div>
            </div> -->
            <!-- <div class="split-line"></div> -->
            <div class="alarm-container">
                <div class="container-title">告警记录</div>
                <div class="alarm-box" v-for="item, index in 2">
                    <div class="title">#01 检测到异常噪声</div>
                    <div class="alarm-info">14:28:33 — 厂区A 东侧走廊</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import SvgIcon from "@/components/SvgIcon";
import MapCanvas from "./MapCanvas.vue";
import { onMounted, onUnmounted } from "vue";
import { getRobotList } from '@/api/inspection/robot'
import { getRunTaskList } from '@/api/inspection/runTask'

console.log('sss', import.meta.env.VITE_INSPECTION_TYPE)

const robotList = ref([])
const robotStatus = {
    "0": "在线",
    "1": "离线",
    "2": "充电中",
    "3": "巡检中"
}
const getRobot = async () => {
    let robotType
    if (import.meta.env.VITE_INSPECTION_TYPE = 'inspection') {
        robotType = '2'
    } else {
        robotType = '1'
    }

    const res = await getRobotList({
        robotType
    })

    if (res.code === 200) {
        robotList.value = res.rows
    }
}

const taskList = ref([])
const taskStatus = {
    "0": "已完成",
    "1": "执行中",
    "2": "失败",
    "3": "已暂停",
    "4": "已终止",
    "5": "未执行"
}
const getRunningTask = async () => {
    let taskType
    if (import.meta.env.VITE_INSPECTION_TYPE = 'inspection') {
        taskType = '1'
    } else {
        taskType = '2'
    }
    const res = await getRunTaskList({
        taskType,
        statusList: ['1', '3']
    })
    if (res.code === 200) {
        taskList.value = res.rows
    }
}

const getRunningTaskByRobotId = (robotId) => {
    const task = taskList.value.find(item => item.robotId === robotId)
    if (task) {
        return task.taskName
    }
    return '暂无'
}

const mapCanvasRef = ref(null)

const zoomCanvas = (isZoomIn) => {
    if (mapCanvasRef.value) {
        mapCanvasRef.value.zoomCanvas(isZoomIn);
    }
}

const centerCanvasView = () => {
    if (mapCanvasRef.value) {
        mapCanvasRef.value.centerCanvasView();
    }
}

onMounted(() => {
    getRobot()
    getRunningTask()
})
</script>

<style lang="scss">
.page-container {
    height: 100%;
    width: 100%;
    display: flex;

    .left-container {
        width: 300px;
        height: 100%;
        padding: 16px;

        .robot-container {
            max-height: 50%;
            overflow-y: auto;

            .robot-state {
                color: #fff;
                font-size: 16px;
                font-weight: bold;
                margin-bottom: 16px;
            }

            .robot-info {
                width: 268px;
                height: 148px;
                background: #0F1E38;
                border-radius: 10px;
                // border: 1px solid #00D4FF;
                padding: 16px;
                margin-bottom: 16px;

                .title-box {
                    display: flex;
                    justify-content: space-between;
                    margin-bottom: 12px;

                    .title {
                        color: #fff;
                        font-size: 14px;
                        font-family: Inter;
                        font-weight: bold;
                    }

                    .state {
                        font-size: 12px;
                        border-radius: 12px;
                        padding: 0 8px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                    }

                    .online {
                        background: #0f83;
                        color: #0f8;
                    }
                }

                .info-box {
                    .item-box {
                        display: flex;
                        justify-content: space-between;
                        margin: 6px 0;

                        .item-title {
                            color: #89B;
                            font-size: 12px;
                        }

                        .item-detail {
                            color: #e0e8f0;
                            font-size: 12px;
                        }
                    }
                }
            }

            .robot-info.last-child {
                margin-bottom: 0;
            }
        }

        .split-line {
            width: 100%;
            height: 1px;
            margin: 16px 0;
            background: #1E3A5F;
        }

        .task-container {
            max-height: 50%;
            overflow-y: auto;

            .container-title {
                color: #fff;
                font-size: 16px;
                font-weight: bold;
                margin-bottom: 16px;
            }

            .task-box {
                height: 72px;
                background: #0F1E38;
                border-radius: 8px;
                padding: 16px;
                margin-bottom: 16px;

                .title {
                    color: #fff;
                    text-align: center;
                    font-size: 14px;
                    font-family: Inter;
                    font-weight: bold;
                }

                .task-info {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin: 10px 0;
                    font-size: 12px;
                    color: #E0E8F0;
                }
            }
        }
    }

    .map-container {
        flex: 1;
        border: 1px solid #1E3A5F;
        border-top: 0;

        .title-container {
            height: 52px;
            padding: 0 16px;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .title {
                color: #fff;
                font-size: 16px;
                font-weight: bold;
                display: flex;
                align-items: center;
                gap: 12px;
            }

            .map-controller {
                display: flex;
                align-items: center;
                gap: 12px;

                .btn-container {
                    width: 32px;
                    height: 32px;
                    border-radius: 8px;
                    background: #1E3A5F;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    border: 1px solid transparent;

                    &:hover {
                        border-color: #00D4FF;
                        cursor: pointer;
                    }
                }
            }
        }

        .map-box {
            height: calc(100% - 52px);
        }
    }

    .right-container {
        width: 280px;
        height: 100%;
        padding: 16px;

        .sensor-container {
            max-height: 50%;
            height: 45%;
            overflow-y: auto;

            .sensor-title {
                color: #fff;
                font-size: 16px;
                font-weight: bold;
                margin-bottom: 16px;
            }
        }

        .split-line {
            width: 100%;
            height: 1px;
            margin: 16px 0;
            background: #1E3A5F;
        }

        .alarm-container {
            max-height: 50%;
            overflow-y: auto;

            .container-title {
                color: #fff;
                font-size: 16px;
                font-weight: bold;
                margin-bottom: 16px;
            }

            .alarm-box {
                height: 72px;
                background: #0F1E38;
                border-radius: 8px;
                padding: 16px;
                margin-bottom: 16px;

                .title {
                    color: #FF6B6B;
                    font-size: 14px;
                    font-family: Inter;
                    font-weight: bold;
                }

                .alarm-info {
                    margin: 10px 0;
                    font-size: 12px;
                    color: #8899BB;
                }
            }
        }
    }
}
</style>