<template>
    <div class="hc-layout h-full w-full">
        <div class="hc-header hc-flex">
            <div class="name hc-flex" @click="handleProject">
                <i class="i-iconoir-play-solid" />
                <span class="ml-1">{{ projectInfo.name }}-档案管理</span>
            </div>
            <div class="extra hc-flex">
                <div class="hc-extra-bar">
                    <el-dropdown>
                        <div class="title hc-flex">
                            <span class="mr-1">更多</span>
                            <i class="i-iconoir-nav-arrow-down" />
                        </div>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item @click="handleProject">项目管理</el-dropdown-item>
                                <el-dropdown-item divided @click="checkUpdates">检测更新</el-dropdown-item>
                                <el-dropdown-item divided @click="handleOnline">在线档案管理</el-dropdown-item>
                                <el-dropdown-item divided @click="handleAbout">关于我们</el-dropdown-item>
                                <el-dropdown-item divided @click="ClearingData">清除数据</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>
        </div>
        <div id="hc-layout-main" class="hc-main">
            <router-view v-if="isMain" />
        </div>
        <!-- 弹窗 -->
        <hc-new-dialog v-model="updateModal" widths="400px" title="检测到新版本" save-text="更新" @save="updateClick">
            <hc-list-item title="系统平台:" :content="updateInfo.platform_arch" />
            <hc-list-item title="当前版本:" :content="config.version" />
            <hc-list-item title="最新版本:" :content="updateInfo.version" />
            <hc-list-item title="发布日期:" :content="updateInfo.date" />
            <hc-list-item title="更新说明:" :content="updateInfo.log" />
        </hc-new-dialog>
    </div>
</template>

<script setup>
import { hcAlert } from 'hc-vue3-ui'
import { onMounted, ref, watch } from 'vue'
import { useAppStore } from '~src/store'
import { useRoute } from 'vue-router'
import { getProjectInfo } from '../store/app'
import { openExternal, toPage } from '../utils/tools'
import { arrIndex, getArrValue, getObjValue, isNullES } from 'js-fast-way'
import config from '../config'

//初始组合式
const store = useAppStore()
const useRoutes = useRoute()

//项目信息
const projectInfo = ref(store.projectInfo)

//监听
watch(() => store.projectInfo, (val) => {
    projectInfo.value = val
})

//渲染完成
const isMain = ref(false)
onMounted(async () => {
    const { code } = await getProjectInfo()
    if (code !== 200) {
        toPage({
            page: '/project',
            query: { source: '' },
        })
    }
    setTimeout(() => {
        isMain.value = true
    }, 500)
})

//项目管理
const handleProject = () => {
    const { path } = useRoutes
    toPage({
        page: '/project',
        query: { source:  path ?? '' },
    })
}

//在线档案管理
const handleOnline = () => {
    openExternal('https://archives.hczcxx.cn')
}

//关于我们
const handleAbout = () => {
    openExternal('http://hczc.hcxxy.com')
}

//检查更新
const checkUpdates = () => {
    hcAlert({
        title:'检测软件是否有新版本',
        content: '检测前，请确保您的电脑已连接网络，并且，可正常访问外网',
        confirm: '确定检测',
        cbk: (res) => {
            if (res) getCheckForUpdates()
        },
    })
}

//检测更新
const updateModal = ref(false)
const updateInfo = ref({})
const getCheckForUpdates = async () => {
    //platform：arm64 x64，arch：darwin win32
    console.log('当前版本：', config.version)
    console.log('platform-arch：', window.api.platform, window.api.arch)
    const http = 'https://archives.hczcxx.cn'
    const url = `${http}/local-app.json?time=${new Date().getTime()}`
    try {
        const { code, data } = await window.electron.ipcRenderer.invoke('require', { url: url })
        if (code !== 200) return
        const appArr = getArrValue(data)
        if (appArr.length <= 0) return
        const PIndex = arrIndex(appArr, 'platform', window.api.platform)
        if (PIndex < 0) return
        const PInfo = getObjValue(appArr[PIndex])
        const PInfoArr = getArrValue(PInfo.data)
        const index = arrIndex(PInfoArr, 'arch', window.api.arch)
        if (index < 0) return
        const app = getObjValue(PInfoArr[index])
        app.platform_arch = `${PInfo.platform}_${app.arch}`
        if (config.version < app.version) {
            updateInfo.value = app
            updateModal.value = true
        }
    } catch (error) {
        console.log(error)
    }
}

//确定更新
const updateClick = () => {
    const { url } = updateInfo.value
    if (isNullES(url)) return
    openExternal(url)
}

//清除数据
const ClearingData = () => {

}
</script>

<style lang="scss" scoped>
i {
    display: inline-flex;
}
.hc-layout {
    position: relative;
    .hc-header {
        height: 44px;
        padding: 0 14px;
        background: white;
        box-shadow: 0 2px 3px 0 #d9d8d8;
        .name {
            flex: 1;
            font-size: 18px;
            font-weight: 500;
            color: #333;
        }
        .extra {
            position: relative;
            height: 100%;
        }
    }
    .hc-main {
        position: relative;
        height: calc(100% - 44px);
        overflow: hidden;
        padding: 14px;
    }
}
</style>
