<template>
    <div class="control-view">
        <!-- 主要内容 -->
        <div class="main-content">
            <!-- 顶部状态栏 -->
            <div class="header-status">
                <div class="time-info">
                    <span>{{ currentTime }} {{ week }}</span>
                </div>
                <div class="page-title">
                    <h1>参数设置</h1>
                </div>
                <div class="system-status">
                    <!-- <span>系统已运行：{{ systemRuntime }}</span> -->
                </div>
            </div>

            <!-- 主体区域 -->
            <div class="control-content">
                <div class="main_item">
                    <div class="main_title">
                        主控器设置
                    </div>
                    <div class="main_content">
                        <div class="line">
                            <div class="item">
                                <div class="key">经度</div>
                                <el-input v-model="formField.lat" style="width: 100px" placeholder="请输入" :formatter="(value) => `${value}˚`" :parser="(value) => value.replace(/˚/g, '')" />
                            </div>
                            <div class="item">
                                <div class="key">纬度</div>
                                <el-input v-model="formField.lgn" style="width: 100px" placeholder="请输入" :formatter="(value) => `${value}˚`" :parser="(value) => value.replace(/˚/g, '')" />
                            </div>
                        </div>
                        <div class="line">
                            <div class="item">
                                <div class="key">经纬开灯偏差</div>
                                <el-input v-model="formField.rise" style="width: 100px" placeholder="请输入" :formatter="(value) => `${value}分`" :parser="(value) => value.replace(/分/g, '')" />
                            </div>
                            <div class="item">
                                <div class="key">经纬关灯偏差</div>
                                <el-input v-model="formField.set" style="width: 100px" placeholder="请输入" :formatter="(value) => `${value}分`" :parser="(value) => value.replace(/分/g, '')" />
                            </div>
                        </div>
                        <div class="line">
                            <div class="item">
                                <div class="key">PWM频率</div>
                                <div class="value">0HZ</div>
                            </div>
                            <div class="item">
                                <div class="key">扩展外设</div>
                                <div class="button disabled">禁用</div>
                            </div>
                        </div>
                        <div class="buttonBox">
                            <div class="button save" @click="save">掉电保存</div>
                            <div class="button restart" @click="restartSystem">重启</div>
                        </div>
                    </div>
                </div>
                <div class="main_item">
                    <div class="main_title">
                        电能表设置
                    </div>
                    <div class="main_content energy_meter" style="width:300px">
                        <div class="line">
                            <div class="item">
                                <div class="key">主控器地址</div>
                                <div class="value"> </div>
                                <!-- <el-input v-model="formField.e" style="width: 100px" placeholder="请输入" /> -->
                            </div>
                        </div>
                        <div class="line">
                            <div class="item">
                                <div class="key">电能表地址</div>
                                <div class="value">{{ address }}</div>
                                <!-- <el-input v-model="formField.f" style="width: 100px" placeholder="请输入" /> -->
                            </div>
                        </div>
                        <div class="line">
                            <div class="item">
                                <div class="key">电能表型号</div>
                                <div class="value"> </div>
                                <!-- <el-input v-model="formField.g" style="width: 100px" placeholder="请输入" /> -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 底部信息 -->
        <div class="bottom-info">
            <div style="color:#fff">
                设备号：{{ commit_uid }}
            </div>
            <div class="bottom_item">
                <!-- <div class="time-item">
                    <span>日出时间：{{ sunriseTime }}</span>
                </div>
                <div class="time-item">
                    <span>日落时间：{{ sunsetTime }}</span>
                </div> -->
            </div>
            <div class="bottom_item">
                <!-- <el-button class="restart-button" @click="restartSystem">
                    <el-icon>
                        <RefreshRight />
                    </el-icon>
                    远程重启
                </el-button> -->
                <el-button class="back-button" @click="goBack">
                    <el-icon>
                        <HomeFilled />
                    </el-icon>
                    返回主界面
                </el-button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import RPC from '@/utils/rpc';

const router = useRouter()
const currentTime = ref('')
const systemRuntime = ref('38分钟')
const formField = ref({})
const commit_uid = localStorage.getItem('central_control_comm_uid')

RPC.post('/m9z/getLgnLatData', { comm_uid: commit_uid }).then(res => {
    formField.value.lat = res.lat
    formField.value.lgn = res.lgn
    formField.value.loc = res.loc
})
RPC.post('/m9z/getSunRiseSet', { comm_uid: commit_uid }).then(res => {
    formField.value.rise = res.rise
    formField.value.set = res.set
})

const address = ref('')
RPC.post('/adl400/getMsgAddress', { comm_uid: commit_uid }).then(res => {
    address.value = res
})

const week = ref('')
const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
// 更新时间
const updateTime = () => {
    const now = new Date()
    const year = now.getFullYear()
    const month = String(now.getMonth() + 1).padStart(2, '0')
    const day = String(now.getDate()).padStart(2, '0')
    const hours = String(now.getHours()).padStart(2, '0')
    const minutes = String(now.getMinutes()).padStart(2, '0')
    const seconds = String(now.getSeconds()).padStart(2, '0')

    week.value = weekDays[now.getDay()]
    currentTime.value = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

const restartSystem = () => {
    RPC.post('/m9z/setRestart', { comm_uid: commit_uid }).then(res => {
        ElMessage.success('重启成功')
    })
}

const save = () => {
    const params = {
        "comm_uid": commit_uid,
        "lat": Number(formField.value.lat),
        "lgn": Number(formField.value.lgn),
        "loc_str": formField.value.loc_str,
        "rise": Number(formField.value.rise),
        "set": Number(formField.value.set)
    }
    RPC.post('/m9z/setDeviceLgnLatAndSunRsTime', params).then(res => {
        ElMessage.success('操作成功')
    })
}



// 返回主界面
const goBack = () => {
    router.push('/')
}

let timeInterval = null

onMounted(() => {
    updateTime()
    timeInterval = setInterval(updateTime, 1000)
})

onUnmounted(() => {
    if (timeInterval) {
        clearInterval(timeInterval)
    }
})
</script>

<style scoped lang="scss">
.control-view {
    width: 100vw;
    height: 100vh;
    box-sizing: border-box;
    position: relative;
    overflow: auto;
    background: center / cover url('@/assets/images/handle_control_bg.png') no-repeat;
}

.main-content {
    position: relative;
    z-index: 2;
    width: 100%;
    height: 100%;
    padding: 10px 20px;
    box-sizing: border-box;
}

.header-status {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    color: rgba(255, 255, 255, 0.9);
}

.time-info {
    font-size: 18px;
    font-weight: 300;
    width: 270px;
}

.page-title h1 {
    font-size: 30px;
    font-weight: bold;
    margin: 0;
    color: #ffffff;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}

.system-status {
    min-width: 200px;
    font-size: 18px;
    font-weight: 300;
    width: 270px;
}

.control-content {
    display: flex;
    width: 100%;
    gap: 30px;
    box-sizing: border-box;

    .main_item {
        border: .5px solid #ddd;
        display: flex;
        flex-direction: column;

        .main_title {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 50px;
            font-size: 16px;
            background-color: #21232A;
            color: #8088AA;
            border-bottom: .5px solid #ddd;
        }

        .main_content {
            padding: 20px;
            font-size: 14px;
            background-color: #282d36;
            flex: 1;

            &.energy_meter {
                .value {
                    width: 100px;
                }
            }

            .key {
                color: #fff;
                margin-right: 10px;
            }

            .value {
                text-align: center;
                background-color: #21232A;
                color: #8088AA;
                padding: 5px 10px;
                min-height: 32px;
                box-sizing: border-box;
                border-radius: 5px;
                border: 1px solid #ccc;
            }

            .line {
                display: flex;
                align-items: center;
                justify-content: space-between;
                gap: 20px;
                margin-bottom: 20px;

                .item {
                    flex: 1;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                }
            }

            .button {
                width: 50px;
                height: 30px;
                background-color: #939393;
                color: #fff;
                border-radius: 10px;
                display: flex;
                align-items: center;
                justify-content: center;
            }

            .buttonBox {
                display: flex;
                justify-content: center;

                .button {
                    width: 70px;
                    margin: 0 10px;

                    &.save {
                        background-color: #7fe669;
                    }

                    &.restart {
                        background-color: #d42d1f;
                    }
                }
            }
        }
    }
}


.bottom-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-top: 1px solid #eee;
    width: 100vw;
    padding: 0 30px;
    height: 50px;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 999;
    box-sizing: border-box;

    .bottom_item {
        display: flex;
        align-items: center;
    }

    .time-item {
        color: rgba(255, 255, 255, 0.7);
        font-size: 16px;
        margin: 0 20px;
    }

    .back-button,
    .restart-button {
        width: 100%;
        height: 38px;
        border-radius: 19px;
        font-size: 16px;
        font-weight: 500;
        transition: all 0.3s ease;
    }

    .back-button {
        background-color: #000;
        color: #fff;
        border: #000;
    }
}

.el-input {
    background-color: #21232A;

    :deep(.el-input__wrapper) {
        background-color: #212329;
        box-shadow: none;
        border: 1px soild #3B3D45;

        input {
            color: #8088AA;
        }
    }
}
</style>
