<script setup>
import LightCard from "@/components/LightCard.vue";
import {Calendar, Clock, CollectionTag, EditPen, Link} from "@element-plus/icons-vue";
import {computed, reactive, ref} from "vue"
import Weather from "@/components/Weather.vue";
import {get} from "@/net";
import {ElMessage} from "element-plus";
import TopicEditor from "@/components/TopicEditor.vue";
import {useStore} from "@/store";
import axios from "axios";

const store = useStore()

const weather = reactive({
    now: {},
    location: {},
    hourly: [],
    success: false
})
const editor = ref(false)
const list = ref(null)

const today = computed(() => {
    const date = new Date()
    return `${date.getFullYear()} 年 ${date.getMonth() + 1} 月 ${date.getDate()} 日`
})

function updateList() {
    get('/api/forum/list-topic?page=0&type=0', data => list.value = data)
}

updateList()
get('/api/forum/types', data => store.forum.types = data)

navigator.geolocation.getCurrentPosition(position => {
    const latitude = position.coords.latitude
    const longitude = position.coords.longitude
    get(`/api/forum/weather?latitude=${latitude}&longitude=${longitude}`, data => {
        Object.assign(weather, data)
        weather.success = true
    })
}, error => {
    console.info(error)
    ElMessage.warning('获取位置信息超时，请检测浏览器设置')
    get(`/api/forum/weather?latitude=39.90499&longitude=116.40529`, data => {
        Object.assign(weather, data)
        weather.success = true
    })
}, {
    timeout: 3000,
    enableHighAccuracy: true
})
</script>

<template>
    <div style="display: flex;margin: 20px auto;gap: 20px;max-width: 1000px">
        <div style="flex: 1">
            <light-card>
                <div class="edit-topic">
                    <div @click="editor = true">
                        <el-icon>
                            <EditPen/>
                        </el-icon>
                        点击发表主题...
                    </div>
                </div>
            </light-card>
            <light-card v-for="item in list" class="topic-card">
                <div style="display: flex">
                    <div>
                        <el-avatar :size="30" :src="`${axios.defaults.baseURL}/images${item.avatar}`"/>
                    </div>
                    <div style="margin-left: 7px;transform: translateY(-2px)">
                        <div style="font-size: 13px;font-weight: bold">{{item.username}}</div>
                        <div style="font-size: 12px;color: grey">
                            <el-icon><Clock/></el-icon>
                            <div style="margin-left: 2px;display: inline-block;transform: translateY(-2px)">{{new Date(item.time).toLocaleString()}}</div>
                        </div>
                    </div>
                </div>
                <div style="margin-top: 5px">
                    <div class="topic-type"
                         :style="{color: store.findTypeById(item.type)?.color+'DD',
                         'border-color': store.findTypeById(item.type)?.color+'EE',
                         'background-color': store.findTypeById(item.type)?.color+'22'}">
                        {{ store.findTypeById(item.type)?.name }}
                    </div>
                    <span style="margin-left: 7px;font-weight: bold">{{ item.title }}</span>
                </div>
                <div class="topic-preview-content">{{ item.text }}</div>
                <div style="display: grid;grid-template-columns: repeat(3, 1fr);grid-gap: 10px;margin: 0 10px">
                    <el-image v-for="img in item.images" fit="cover" :src="img" class="topic-image"></el-image>
                </div>
            </light-card>
        </div>
        <div style="width: 280px">
            <div style="position: sticky;top: 20px">
                <light-card>
                    <div style="font-weight: bold;">
                        <el-icon>
                            <CollectionTag/>
                        </el-icon>
                        论坛公告
                    </div>
                    <el-divider style="margin: 10px 0"/>
                    <div style="margin: 10px;font-size: 14px;color: grey">
                        为认真学习宣传贯彻党的二十大精神,深入贯彻习近平强军思想,
                        作为迎接办学70周年系列学术活动之一,国防科技大学将于2022年11月24日至26日在长沙举办“国防科技
                    </div>
                </light-card>
                <light-card style="margin-top: 10px">
                    <div style="font-weight: bold;">
                        <el-icon>
                            <Calendar/>
                        </el-icon>
                        天气信息
                    </div>
                    <el-divider style="margin: 10px 0"/>
                    <weather :data="weather"/>
                </light-card>
                <light-card style="margin-top: 10px">
                    <div style="display: flex;justify-content: space-between;color: grey;font-size: 14px">
                        <div>当前日期</div>
                        <div>{{ today }}</div>
                    </div>
                    <div style="display: flex;justify-content: space-between;color: grey;font-size: 14px">
                        <div>当前IP地址</div>
                        <div><b>127.0.0.1</b></div>
                    </div>
                </light-card>
                <div style="margin-top: 10px;color: grey;font-size: 14px">
                    <el-icon>
                        <Link/>
                    </el-icon>
                    友情链接
                    <el-divider style="margin: 10px 0"/>
                </div>
                <div style="display: grid;grid-template-columns: repeat(2, 1fr);grid-gap: 10px;margin-top: 10px">
                    <div class="friend-link">
                        <el-image src="https://element-plus.org/images/js-design-banner.jpg" style="height: 100%"/>
                    </div>
                    <div class="friend-link">
                        <el-image src="https://element-plus.org/images/vform-banner.png" style="height: 100%"/>
                    </div>
                    <div class="friend-link">
                        <el-image src="https://element-plus.org/images/sponsors/jnpfsoft.jpg" style="height: 100%"/>
                    </div>
                </div>
            </div>
        </div>
        <topic-editor :show="editor" @close="editor = false" @created="editor = false;updateList()"/>
    </div>
</template>

<style lang="less" scoped>
.topic-card {
    margin-top: 10px;
    transition: scale .3s;
    padding: 20px;

    &:hover {
        scale: 1.01;
        cursor: pointer;
    }

    .topic-image{
        width: 100%;
        height: 100%;
        max-height: 120px;
        border-radius: 5px;
    }

    .topic-type {
        display: inline-block;
        border: solid 0.5px grey;
        border-radius: 5px;
        font-size: 12px;
        padding: 0 5px;
        height: 18px;
    }

    .topic-preview-content {
        font-size: 13px;
        color: grey;
        margin: 10px;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}


.friend-link {
    border-radius: 5px;
    overflow: hidden;
}

.edit-topic {
    background-color: #ececec;
    border-radius: 5px;
    height: 40px;
    color: grey;
    font-size: 14px;
    line-height: 40px;
    padding: 0 10px;

    :hover {
        cursor: pointer;
    }
}

.dark .edit-topic {
    background-color: #282828;
}
</style>
