<template>
    <header v-if="props.showAdjust">
        <div v-if="project">{{ project.projectName }}</div>
        <div class="sync">
            <button @click="onCancel">取消</button>
            <button type="submit" @click="onSubmit" :class="{ active: points.p1 && points.p2 }">同步</button>
        </div>
    </header>
    <header v-else>
        <div v-if="project">{{ project.projectName }}</div>
        <div class="user">
            <ul>
                <li>
                    <i class="iconfont icon-share"></i>
                </li>
                <li><em></em></li>
                <li v-if="user" class="uinfo" @click="showDrop = true">
                    <img :src="user.head + '&x-oss-process=image/resize,m_fill,w_64,h_64/quality,q_70'" alt="" />
                    <div class="menu">
                        <ul>
                            <li><a href="/">个人信息</a></li>
                            <li class="split"></li>
                            <li><a href="javascript:;" @click="onLogout">退出登录</a></li>
                        </ul>
                    </div>
                </li>
                <li v-else class="login" @click="showLogin = true"><span>登录</span></li>
            </ul>
        </div>
        <Login v-if="showLogin" @close="showLogin = false" @user="info => (user = info)" />
    </header>
    <footer v-if="props.showAdjust">
        <h4>为场景设置关联位置</h4>
        <div>请选择位置，确认左右视图中的场景在同一位置后，单击右侧按钮将其设为关联位置。</div>
        <div class="points">
            <button @click="onSetP1" :class="{ active: points.p1 }">设为P1</button>
            <button @click="onSetP2" :class="{ active: points.p2 }">设为P2</button>
        </div>
    </footer>
</template>
<script setup>
import { ref, defineProps, onMounted } from 'vue'
import { http } from '@/utils/request'
import browser from '@/utils/browser'
import Login from './Login'
const props = defineProps({
    project: Object,
    showAdjust: Boolean,
})
const emits = defineEmits(['update'])

const user = ref(null)
const points = ref({ p1: null, p2: null })
const showLogin = ref(false)

const onSetP1 = () => {
    points.value.p1 = { id: 0, position: { x: 1, y: 1, z: 1 } }
    emits('update','p1')

}
const onSetP2 = () => {
    points.value.p2 = { id: 0, position: { x: 1, y: 1, z: 1 } }
    emits('update','p2')
}

const getUserInfo = () => {
    http.post(`smart-site/getUserInfo`)
        .then(response => {
            if (response.success) {
                user.value = {
                    head: response.data.head,
                    nickName: response.data.nickName,
                }
            } else {
                if (response.code == 4008) {
                    // 未登录
                }
            }
        })
        .catch(() => {})
}
const onLogout = () => [
    http
        .post(`smart-site/fdLogout`)
        .then(response => {
            if (response.success) {
                localStorage.removeItem('token')
                localStorage.removeItem('remember')
                localStorage.removeItem('username')
                localStorage.removeItem('password')
                user.value = null
            }
        })
        .catch(() => {}),
]
const onCancel = () => {
    window.location.href = window.location.href.replace('&adjust', '')
}
const onSubmit = () => {
    http.post(`smart-site/project/updatePanos`, {
        projet_id: props.project.projectId,
        panos: JSON.stringify(points.value),
    })
        .then(response => {
            if (response.success) {
                window.location.href = window.location.href.replace('&adjust', '&split')
            }
        })
        .catch(() => {})
}

onMounted(() => {
    if (localStorage.getItem('token')) {
        getUserInfo()
    }
})
</script>
<style lang="scss" scoped>
ul,
li {
    margin: 0;
    padding: 0;
    list-style: none;
}
header {
    position: relative;
    height: 60px;
    background-color: #1a1b1d;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
}
footer {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 90px;
    color: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(4px);
    background: rgba(27, 27, 28, 0.8);
    z-index: 99999;
    padding: 20px 24px;
    h4 {
        font-size: 20px;
        margin: 0;
        padding: 0;
        color: #fff;
        margin-bottom: 14px;
    }
    .points {
        position: absolute;
        top: 20px;
        right: 0;
        bottom: 20px;
        display: flex;
        align-items: center;
        button {
            cursor: pointer;
            outline: none;
            color: #0076f6;
            margin-right: 24px;
            width: 160px;
            height: 36px;
            background: transparent;
            border: 1px solid #0076f6;
            border-radius: 4px;
            &.active {
                background: #0076f6;
                color: #fff;
            }
        }
    }
}
.user {
    position: absolute;
    top: 0;
    right: 20px;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    ul {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    i {
        font-size: 18px;
    }
    em {
        margin: 0 20px;
        display: inline-block;
        width: 1px;
        height: 16px;
        vertical-align: -2px;
        background-color: rgba(255, 255, 255, 0.16);
    }

    .login {
        cursor: pointer;
        color: rgba(0, 118, 246, 1);
    }
    .uinfo {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        font-size: 14px;
        &::after {
            content: '';
            position: absolute;
            right: 3px;
            bottom: 4px;
            width: 0;
            height: 0;
            border-color: rgba(255, 255, 255, 0.6) transparent;
            border-width: 0 0 5px 5px;
            border-style: solid;
        }
        &:hover {
            .menu {
                display: block;
            }
        }
        img {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background-size: cover;
            border: none;
            outline: none;
            overflow: hidden;
            cursor: pointer;
        }
        .menu {
            display: none;
            position: absolute;
            top: 40px;
            right: 0;
            z-index: 2000;
            a {
                color: #fff;
                text-decoration: none;
            }
            ul {
                background-color: #343535;
                width: 128px;
                flex-direction: column;
                border-radius: 4px;
                box-shadow: 0 0 4px #333;
                padding: 0 15px;
                margin-top: 15px;
            }
            li {
                height: 45px;
                line-height: 45px;
                text-align: center;
                &.split {
                    width: 100%;
                    height: 1px;
                    background-color: rgba(255, 255, 255, 0.16);
                }
            }
        }
    }
}
.sync {
    position: absolute;
    right: 0;
    top: 0;
    height: 100%;
    display: flex;
    align-items: center;
    button {
        cursor: pointer;
        width: 90px;
        height: 35px;
        background: #313131;
        color: #fff;
        border: none;
        outline: none;
        border-radius: 4px;
        margin-right: 10px;
        &[type='submit'] {
            background: #0076f6;
            opacity: 0.5;
            pointer-events: none;
        }
        &[type='submit'].active {
            opacity: 1;
            pointer-events: all;
        }
    }
}
</style>
