<template>
    <div class="tips" ref="tipsDomRef" @mousedown="dragEvent">
        <!-- 顶部面板 -->
        <div class="top-panel" v-if="ifShowTop">
            <!-- 顶部导航菜单 -->
            <div class="top-navs">
                <div class="btns">
                    <div class="btn" :class="{ 'clicked': item.code === currentBtn.code }" v-for="(item, index) in navBtns"
                        :key="index" @click="handleBtnClick(item)">
                        {{ item.name }}
                        <div v-if="item.code === 'todo'">({{ todoData.length }})</div>
                        <div v-if="item.code === 'message'">({{ msgData.length }})</div>
                    </div>
                </div>
                <div class="close-btn" @click="ifShowTop = false"></div>
            </div>
            <!-- 下方列表 -->
            <div class="tips-list">
                <!-- 消息列表 -->
                <div class="tips-item" v-if="currentBtn.code !== 'todo'">
                    <div class="msg-list list" v-for="(item, index) in msgData" :key="index">
                        <div class="list-header">
                            <div class="title">
                                <div class="msg-icon"></div>
                                {{ item.opt }}
                            </div>
                        </div>
                        <div class="list-content">
                            <div class="left-box">
                                <div>审核人：{{ item.user }}</div>
                                <div>时间：{{ item.time }}</div>
                            </div>
                            <div class="status" :class="[item.status === 1 ? 'passed' : 'nopassed']"></div>
                        </div>
                    </div>
                </div>
                <!-- 待处理 -->
                <div class="tips-item" v-else>
                    <div class="todo-list list" v-for="(item, index) in todoData" :key="index">
                        <div class="list-header">
                            <div class="title">【{{ item.title }}】</div>
                            <div class="more-btn"></div>
                        </div>
                        <div class="list-content">
                            <div class="left-box">
                                <div>{{ item.opt }}</div>
                                <div>发起人：{{ item.user }}</div>
                                <div>时间：{{ item.time }}</div>
                            </div>
                            <div class="status" :class="[item.status === 1 ? 'passed' : 'nopassed']"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 底部图标 -->
        <div class="bottom-panel" @click="openTopPanel" v-if="!ifShowTop">
            <div class="icon"></div>
            <div class="msg-num">{{ totalTipsNum }}</div>
        </div>
    </div>
</template>
<script>
import { defineComponent, reactive, ref, toRefs, computed, onMounted, watch } from 'vue';

export default defineComponent({
    name: 'tips',
    components: {},
    setup() {
        const tipsDomRef = ref(null)

        const navBtns = reactive([
            {
                name: `待办信息`,
                code: "todo"
            },
            {
                name: "通知消息",
                code: "message"
            }
        ])
        const currentBtn = reactive({
            name: "待办信息",
            code: "todo"
        })
        const ifShowTop = ref(false)
        const handleBtnClick = (item) => {
            currentBtn.code = item.code
            currentBtn.name = item.name
        }
        const todoData = reactive([
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 0,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            }
        ])
        const msgData = reactive([
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 1,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 1,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 1,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 1,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            },
            {
                title: "资源申请",
                opt: "xx地图资源授权申请",
                status: 1,
                user: "张某某",
                time: "2023-07-06  14:30:00"
            }
        ])

        const totalTipsNum = computed(() => {
            let totalNum = todoData.length + msgData.length
            return totalNum <= 99 ? totalNum === 0 ? '' : totalNum : `10+`
        })

        const ifMove = ref(false)

        const dragEvent = (e) => {
            // tipsDomRef.value.stopPropagation()
            //算出鼠标相对元素的位置
            let disX = e.clientX - tipsDomRef.value.offsetLeft;
            let disY = e.clientY - tipsDomRef.value.offsetTop;
            //自身宽高
            let moveW = tipsDomRef.value.offsetWidth;
            let moveH = tipsDomRef.value.offsetHeight;
            //父级宽高
            let parentW = window.innerWidth;//父盒子的width
            let parentH = window.innerHeight;//父盒子的height

            let move = function (move) {
                ifMove.value = true;
                //用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
                let left = move.clientX - disX;
                let top = move.clientY - disY;
                let resW = parentW - moveW;//父盒子宽减去移动盒子的宽 = 最大可移动的区域
                let resH = parentH - moveH;//父盒子高减去移动盒子的高 = 可最大移动的区域
                left = left >= resW ? resW : left <= 0 ? 0 : left;
                top = top >= resH ? resH : top <= 0 ? 0 : top;
                //移动当前元素
                tipsDomRef.value.style.left = left + "px";
                tipsDomRef.value.style.top = top + "px";
            }

            document.addEventListener('mousemove', move)

            document.addEventListener('mouseup', (e) => {
                //鼠标弹起来的时候不再移动
                document.removeEventListener('mousemove', move)
            })
        }

        const openTopPanel = (e) => {
            if(ifMove.value){
                ifMove.value = false;
                return;
            }
            ifShowTop.value = !ifShowTop.value
            // let disX = e.clientX - tipsDomRef.value.offsetLeft;
            // let disY = e.clientY - tipsDomRef.value.offsetTop;
            setTimeout(() => {
                //自身宽高
                let moveW = tipsDomRef.value.offsetWidth;
                let moveH = tipsDomRef.value.offsetHeight;
                //父级宽高
                let parentW = window.innerWidth;//父盒子的width
                let parentH = window.innerHeight;//父盒子的height
                //用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
                let left = tipsDomRef.value.offsetLeft;
                let top = tipsDomRef.value.offsetTop;
                let resW = parentW - moveW;//父盒子宽减去移动盒子的宽 = 最大可移动的区域
                let resH = parentH - moveH;//父盒子高减去移动盒子的高 = 可最大移动的区域
                left = left >= resW ? resW : left <= 0 ? 0 : left;
                top = top >= resH ? resH : top <= 0 ? 0 : top;
                //移动当前元素
                tipsDomRef.value.style.left = left + "px";
                tipsDomRef.value.style.top = top + "px";
            }, 100)
        }

        return {
            tipsDomRef,
            navBtns,
            currentBtn,
            ifShowTop,
            todoData,
            msgData,
            totalTipsNum,
            //func
            handleBtnClick,
            dragEvent,
            openTopPanel
        };
    },
});
</script>
<style lang="less" scoped>
.tips {
    position: fixed;
    left: calc(100% - 106px);
    top: calc(100% - 80px);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    user-select: none;

    .top-panel {
        margin-bottom: 24px;
        width: 430px;
        height: 820px;
        padding: 0 16px 20px;
        background: #FFFFFF;
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
        border-radius: 6px;

        .top-navs {
            width: 100%;
            height: 50px;
            border-bottom: 1px solid #DEDEDE;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .btns {
                display: flex;

                .btn {
                    display: flex;
                    margin-right: 32px;
                    width: 100px;
                    height: 50px;
                    font-family: Source Han Sans CN;
                    font-size: 16px;
                    font-weight: 500;
                    line-height: 50px;
                    color: #333333;
                    user-select: none;
                    text-align: center;

                    &:hover {
                        cursor: pointer;
                        color: #0671DD;
                        border-bottom: 2px solid #0671DD;
                    }
                }

                .clicked {
                    color: #0671DD;
                    border-bottom: 2px solid #0671DD;
                }
            }

            .close-btn {
                width: 20px;
                height: 20px;
                margin-right: 10px;
                user-select: none;
                cursor: pointer;
                border-radius: 50%;
                text-align: center;
                line-height: 20px;
                background: url('/@/assets/images/close.png') no-repeat;
                background-size: 100% 100%;
            }
        }

        .tips-list {
            padding-top: 18px;
            width: 100%;
            height: calc(100% - 50px);
            overflow: auto;

            &::-webkit-scrollbar {
                width: 3px;
            }

            &::-webkit-scrollbar-thumb {
                border-radius: 2px;
                background: #ccd5df;
            }

            &::-webkit-scrollbar-track {
                display: none;
            }

            .tips-item {
                .list {
                    padding: 0 12px 12px;
                    width: 100%;
                    height: 130px;
                    border-radius: 6px;
                    background: linear-gradient(252deg, rgba(238, 247, 255, 0.76) 0%, #EDEDED 100%);
                    margin-bottom: 10px;

                    .list-header {
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        width: 100%;
                        height: 40px;
                        border-bottom: 1px solid #dedede;

                        .title {
                            max-width: 70%;
                            font-family: Source Han Sans CN;
                            font-size: 16px;
                            font-weight: 500;
                            line-height: 40px;
                            color: #333333;
                        }

                        .more-btn {
                            width: 24px;
                            height: 24px;
                            background: url('/@/assets/images/goto.png') no-repeat;
                            background-size: 100% 100%;
                            cursor: pointer;
                        }
                    }

                    .list-content {
                        height: 89px;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        .left-box {
                            height: 100%;
                            display: flex;
                            flex-direction: column;
                            justify-content: space-evenly;
                        }

                        .status {
                            width: 56px;
                            height: 56px;
                        }

                        .passed {
                            background: url('/@/assets/images/passed.png') no-repeat;
                            background-size: 100% 100%;
                        }

                        .nopassed {
                            background: url('/@/assets/images/no-passed.png') no-repeat;
                            background-size: 100% 100%;
                        }
                    }
                }

                .msg-list {
                    .list-header {


                        .title {
                            display: flex;
                            align-items: center;

                            .msg-icon {
                                margin-right: 10px;
                                width: 21px;
                                height: 20px;
                                background: url('/@/assets/images/tips.png') no-repeat;
                                background-size: 100% 100%;
                            }
                        }
                    }
                }
            }
        }
    }

    .bottom-panel {
        position: relative;
        width: 60px;
        height: 60px;
        border-radius: 6px;
        background: #FFFFFF;
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
        cursor: pointer;

        .icon {
            position: absolute;
            left: 14px;
            top: 15px;
            width: 33px;
            height: 31px;
            background: url('/@/assets/images/tips.png') no-repeat;
            background-size: 100% 100%;
        }

        .msg-num {
            position: absolute;
            top: 5px;
            right: 5px;
            color: #F8252C;
            font-family: Source Han Sans CN;
            font-size: 14px;
            font-weight: bold;
            line-height: 18.72px;
        }
    }
}
</style>