<template>
    <div class="default-main">
        <div class="banner">
            <el-row :gutter="10">
                <el-col :md="24" :lg="18">
                    <div class="welcome suspension">
                        <img class="welcome-img" :src="headerSvg" alt=""/>
                        <div class="welcome-text">
                            <div class="welcome-title">{{ adminInfo.accountName }}，{{ state.greetings }}</div>
                        </div>
                    </div>
                </el-col>
                <el-col :lg="6" class="hidden-md-and-down">
                    <div class="working">
                        <img class="working-coffee" :src="coffeeSvg" alt=""/>
                        <div class="working-text">
                            您今天已工作了<span class="time">{{ state.workingTimeFormat }}</span>
                        </div>
                        <div @click="onChangeWorkState()" class="working-opt working-rest">
                            {{ state.pauseWork ? '继续工作' : '休息片刻' }}
                        </div>
                    </div>
                </el-col>
            </el-row>
        </div>
<!--        <div class="small-panel-box">-->
<!--            <el-row :gutter="20">-->
<!--                <el-col :sm="12" :lg="8">-->
<!--                    <div class="small-panel user-reg suspension">-->
<!--                        <div class="small-panel-title">用户总数/当日注册会员数</div>-->
<!--                        <div class="small-panel-content">-->
<!--                            <div class="content-left">-->
<!--                                <Icon color="#8595F4" size="20" name="fa fa-users"/>-->
<!--                                <span id="user_reg_number">{{echartsData.memberCount}}</span>-->
<!--                            </div>-->
<!--                            <div class="content-right">+{{echartsData.todayMemberCount}}</div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </el-col>-->
<!--                <el-col :sm="12" :lg="8">-->
<!--                    <div class="small-panel file suspension">-->
<!--                        <div class="small-panel-title">营业额总金额/当日销售总金额</div>-->
<!--                        <div class="small-panel-content">-->
<!--                            <div class="content-left">-->
<!--                                <Icon color="#AD85F4" size="23" name="fa fa-cny"/>-->
<!--                                <span id="file_number">{{echartsData.saleAmount}}</span>-->
<!--                            </div>-->
<!--                            <div class="content-right">+{{echartsData.todaySaleAmount}}</div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </el-col>-->
<!--                <el-col :sm="12" :lg="8">-->
<!--                    <div class="small-panel users suspension">-->
<!--                        <div class="small-panel-title">销售台数/当日销售台数</div>-->
<!--                        <div class="small-panel-content">-->
<!--                            <div class="content-left">-->
<!--                                <Icon color="#74A8B5" size="20" name="fa fa-line-chart"/>-->
<!--                                <span id="users_number">{{echartsData.saleCount}}</span>-->
<!--                            </div>-->
<!--                            <div class="content-right">+{{echartsData.todaySaleCount}}</div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </el-col>-->
<!--            </el-row>-->
<!--        </div>-->
<!--        <div class="growth-chart">-->
<!--            <el-row :gutter="20">-->
<!--                <el-col class="lg-mb-20" :xs="24" :sm="24" :md="12" :lg="8">-->
<!--                    <el-card shadow="hover" header="每月注册用户数量">-->
<!--                        <template #header>-->
<!--                            <div style="display: flex;justify-content: space-between">-->
<!--                                <span>{{ year }}年每月注册用户数量</span>-->
<!--                                <el-select size="small" style="width: 100px" v-model="year" @change="getEchartData()">-->
<!--                                    <el-option v-for="(item,index) in (d.getFullYear()-2020)"-->
<!--                                               :value="d.getFullYear()-item+1" :key="index"></el-option>-->
<!--                                </el-select>-->
<!--                            </div>-->
<!--                        </template>-->
<!--                        <userMouth :data="echartsData.memberList"></userMouth>-->
<!--                    </el-card>-->
<!--                </el-col>-->
<!--                <el-col class="lg-mb-20" :xs="24" :sm="24" :md="12" :lg="8">-->
<!--                    <el-card shadow="hover" header="一周用户访问量">-->
<!--                        <userWeek :data="echartsData.weekData"></userWeek>-->
<!--                    </el-card>-->
<!--                </el-col>-->
<!--                <el-col class="lg-mb-20" :xs="24" :sm="24" :md="12" :lg="8">-->
<!--                    <el-card class="new-user-card" shadow="hover" header="最新注册的会员">-->
<!--                        <div class="new-user-growth">-->
<!--                            <el-scrollbar>-->
<!--                                <div class="new-user-item" :key="index" v-for="(item,index) in echartsData.lastMember">-->
<!--                                    <el-image class="new-user-avatar" :src="item.headPortrait==='/'?defaultAvatar:item.headPortrait" />-->
<!--                                    <div class="new-user-base">-->
<!--                                        <div class="new-user-name">{{item.nickName}}</div>-->
<!--                                        <div class="new-user-time">{{item.createTime}} 注册</div>-->
<!--                                    </div>-->
<!--                                </div>-->
<!--                            </el-scrollbar>-->
<!--                        </div>-->
<!--                    </el-card>-->
<!--                </el-col>-->
<!--            </el-row>-->
<!--        </div>-->
<!--        <div class="growth-chart">-->
<!--            <el-row :gutter="20">-->
<!--                <el-col class="lg-mb-20" :xs="24" :sm="24" :md="12" :lg="12">-->
<!--                    <el-card shadow="hover" :header="year+'年每月营业额统计'">-->
<!--                        <saleAmount :data="echartsData.orderHistory"></saleAmount>-->
<!--                    </el-card>-->
<!--                </el-col>-->
<!--                <el-col class="lg-mb-20" :xs="24" :sm="24" :md="12" :lg="12">-->
<!--                    <el-card shadow="hover" :header="year+'每月销售台数统计'">-->
<!--                        <saleCount :data="echartsData.orderHistory"></saleCount>-->
<!--                    </el-card>-->
<!--                </el-col>-->
<!--            </el-row>-->
<!--        </div>-->


    </div>
</template>

<script setup lang="ts">
import {onMounted, onUnmounted, reactive, nextTick, onActivated, watch, onBeforeMount, ref} from 'vue'
import headerSvg from '/@/assets/dashboard/header-1.svg'
import coffeeSvg from '/@/assets/dashboard/coffee.svg'
import {CountUp} from 'countup.js'
import * as echarts from 'echarts'
import {useNavTabs} from '/@/stores/navTabs'
import {useAdminInfo} from "/@/stores/adminInfo";
import {useTemplateRefsList} from "@vueuse/core";
import {Local} from "/@/utils/storage";
import {WORKING_TIME} from "/@/stores/constant/cacheKey";
import userWeek from './component/userWeek.vue'
import userMouth from './component/userMouth.vue'
import saleAmount from './component/saleAmount.vue'
import saleCount from './component/saleCount.vue'
import request from "/@/api/request";
import {statistics, statistics_week_member} from "/@/api/controllerUrls"
import defaultAvatar from '/@/assets/defaultAvatar.svg'

var workTimer: NodeJS.Timer


const d = new Date()
const navTabs = useNavTabs()
const adminInfo = useAdminInfo()
const chartRefs = useTemplateRefsList<HTMLDivElement>()

let year = ref<number>(d.getFullYear())

const state: {
    charts: any[]
    greetings: string
    remark: string
    workingTimeFormat: string
    pauseWork: boolean
} = reactive({
    charts: [],
    greetings: '您好',
    remark: '加载中...',
    workingTimeFormat: '',
    pauseWork: false,
})


const greetingsFun = () => {
    const now = new Date()
    const hour = now.getHours()
    let greetings = ''
    if (hour < 5) {
        greetings = '夜深了，注意身体哦！'
    } else if (hour < 9) {
        greetings = '早上好！欢迎回来。'
    } else if (hour < 12) {
        greetings = '上午好！欢迎回来。'
    } else if (hour < 14) {
        greetings = '中午好！欢迎回来。'
    } else if (hour < 18) {
        greetings = '下午好！欢迎回来。'
    } else if (hour < 24) {
        greetings = '晚上好！欢迎回来。'
    } else {
        greetings = '您好！欢迎回来。'
    }

    state.greetings = greetings
}

const onChangeWorkState = () => {
    const time = parseInt((new Date().getTime() / 1000).toString())
    const workingTime = Local.get(WORKING_TIME)
    if (state.pauseWork) {
        // 继续工作
        workingTime.pauseTime += time - workingTime.startPauseTime
        workingTime.startPauseTime = 0
        Local.set(WORKING_TIME, workingTime)
        state.pauseWork = false
        startWork()
    } else {
        // 暂停工作
        workingTime.startPauseTime = time
        Local.set(WORKING_TIME, workingTime)
        clearInterval(workTimer)
        state.pauseWork = true
    }
}

const startWork = () => {
    const workingTime = Local.get(WORKING_TIME) || {date: '', startTime: 0, pauseTime: 0, startPauseTime: 0}
    const currentDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
    const time = parseInt((new Date().getTime() / 1000).toString())

    if (workingTime.date != currentDate) {
        workingTime.date = currentDate
        workingTime.startTime = time
        workingTime.pauseTime = workingTime.startPauseTime = 0
        Local.set(WORKING_TIME, workingTime)
    }

    let startPauseTime = 0
    if (workingTime.startPauseTime <= 0) {
        state.pauseWork = false
        startPauseTime = 0
    } else {
        state.pauseWork = true
        startPauseTime = time - workingTime.startPauseTime // 已暂停时间
    }

    let workingSeconds = time - workingTime.startTime - workingTime.pauseTime - startPauseTime

    state.workingTimeFormat = formatSeconds(workingSeconds)
    if (!state.pauseWork) {
        workTimer = setInterval(() => {
            workingSeconds++
            state.workingTimeFormat = formatSeconds(workingSeconds)
        }, 1000)
    }
}

const formatSeconds = (seconds: number) => {
    var secondTime = 0 // 秒
    var minuteTime = 0 // 分
    var hourTime = 0 // 小时
    var dayTime = 0 // 天
    var result = ''

    if (seconds < 60) {
        secondTime = seconds
    } else {
        // 获取分钟，除以60取整数，得到整数分钟
        minuteTime = Math.floor(seconds / 60)
        // 获取秒数，秒数取佘，得到整数秒数
        secondTime = Math.floor(seconds % 60)
        // 如果分钟大于60，将分钟转换成小时
        if (minuteTime >= 60) {
            // 获取小时，获取分钟除以60，得到整数小时
            hourTime = Math.floor(minuteTime / 60)
            // 获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = Math.floor(minuteTime % 60)
            if (hourTime >= 24) {
                // 获取天数， 获取小时除以24，得到整数天
                dayTime = Math.floor(hourTime / 24)
                // 获取小时后取余小时，获取分钟除以24取余的分；
                hourTime = Math.floor(hourTime % 24)
            }
        }
    }

    result =
        hourTime +
        '小时' +
        ((minuteTime >= 10 ? minuteTime : '0' + minuteTime) + '分钟') +
        ((secondTime >= 10 ? secondTime : '0' + secondTime) + '秒')
    if (dayTime > 0) {
        result = dayTime + '天' + result
    }
    return result
}

let echartsData = ref<any>({})

function getEchartData(){
    request.index(statistics,{yearly:year.value}).then(res=>{
        echartsData.value = res.data
    })

    request.index(statistics,{yearly:year.value},'member/activity').then(res=>{
        echartsData.value.weekData = res.data
    })
}


onMounted(() => {
    // startWork()
    // greetingsFun()
    // initCountUp()
    // getEchartData()
})

//字动画
const initCountUp = () => {
    countUpFun('user_reg_number')
    countUpFun('file_number')
    countUpFun('users_number')
    countUpFun('addons_number')
}

const countUpFun = (id: string) => {
    nextTick(() => {
        let value = document.getElementById(id)?.innerText
        if (value) {
            new CountUp(id, parseInt(value), {
                startVal: 0,
                duration: 3,
            }).start()
        }
    })
}
</script>

<style scoped lang="scss">
.welcome {
    transition: all 0.3s ease;
    background: #e1eaf9;
    border-radius: 6px;
    display: flex;
    align-items: center;
    padding: 15px 20px !important;
    box-shadow: 0 0 30px 0 rgba(82, 63, 105, 0.05);

    .welcome-img {
        height: 100px;
        margin-right: 10px;
        user-select: none;
    }

    .welcome-title {
        font-size: 1.5rem;
        line-height: 30px;
        color: var(--color-primary-sub-0);
    }

    .welcome-note {
        padding-top: 6px;
        font-size: 15px;
        color: var(--color-text-primary);
    }
}

.working {
    height: 130px;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    height: 100%;
    position: relative;

    &:hover {
        .working-coffee {
            -webkit-transform: translateY(-4px) scale(1.02);
            -moz-transform: translateY(-4px) scale(1.02);
            -ms-transform: translateY(-4px) scale(1.02);
            -o-transform: translateY(-4px) scale(1.02);
            transform: translateY(-4px) scale(1.02);
            z-index: 999;
        }
    }

    .working-coffee {
        transition: all 0.3s ease;
        width: 80px;
    }

    .working-text {
        display: block;
        width: 100%;
        font-size: 15px;
        text-align: center;
        color: var(--color-text-primary);
    }

    .working-opt {
        position: absolute;
        top: -40px;
        right: 10px;
        background-color: rgba($color: #000000, $alpha: 0.3);
        padding: 10px 20px;
        border-radius: 20px;
        color: #fff;
        transition: all 0.3s ease;
        cursor: pointer;
        opacity: 0;
        z-index: 999;

        &:active {
            background-color: rgba($color: #000000, $alpha: 0.6);
        }
    }

    &:hover {
        .working-opt {
            opacity: 1;
            top: 0px;
        }

        .working-done {
            opacity: 1;
            top: 50px;
        }
    }
}

.small-panel-box {
    margin-top: 20px;
}

.small-panel {
    background-color: #e9edf2;
    border-radius: var(--el-border-radius-base);
    transition: all 0.3s ease;
    padding: 25px;
    margin-bottom: 20px;

    .small-panel-title {
        color: #92969a;
        font-size: 15px;
    }

    .small-panel-content {
        display: flex;
        align-items: flex-end;
        margin-top: 20px;
        color: #2c3f5d;

        .content-left {
            font-size: 24px;

            .icon {
                margin-right: 10px;
            }

            span {
                display: inline-block;
                font-size: 28px;
            }
        }

        .content-right {
            font-size: 18px;
            margin-left: auto;
        }

        .color-success {
            color: var(--color-success);
        }

        .color-warning {
            color: var(--color-warning);
        }

        .color-danger {
            color: var(--color-danger);
        }

        .color-info {
            color: var(--color-info);
        }
    }
}

.growth-chart {
    margin-bottom: 20px;
}

.suspension:hover {
    -webkit-transform: translateY(-4px) scale(1.02);
    -moz-transform: translateY(-4px) scale(1.02);
    -ms-transform: translateY(-4px) scale(1.02);
    -o-transform: translateY(-4px) scale(1.02);
    transform: translateY(-4px) scale(1.02);
    -webkit-box-shadow: 0 14px 24px rgba(0, 0, 0, 0.2);
    box-shadow: 0 14px 24px rgba(0, 0, 0, 0.2);
    z-index: 999;
    border-radius: 6px;
}

.user-growth-chart,
.file-growth-chart {
    height: 260px;
}

.new-user-growth {
    height: 300px;
}

.user-source-chart,
.user-surname-chart {
    height: 400px;
}

.new-user-item {
    display: flex;
    align-items: center;
    padding: 20px;
    margin: 10px 15px;
    box-shadow: 0 0 30px 0 rgba(82, 63, 105, 0.05);
    background-color: #fff;

    .new-user-avatar {
        height: 48px;
        width: 48px;
        border-radius: 50%;
    }

    .new-user-base {
        margin-left: 10px;
        color: #2c3f5d;

        .new-user-name {
            font-size: 15px;
        }

        .new-user-time {
            font-size: 13px;
        }
    }

    .new-user-arrow {
        margin-left: auto;
    }
}

.new-user-card :deep(.el-card__body) {
    padding: 0;
}

@media screen and (max-width: 425px) {
    .welcome-img {
        display: none;
    }
}

@media screen and (max-width: 1200px) {
    .lg-mb-20 {
        margin-bottom: 20px;
    }
}
</style>
