<template>

  <el-row class="mt-8px" :gutter="8" justify="space-between">
    <el-col :xl="16" :lg="16" :md="24" :sm="24" :xs="24" class="mb-8px">
      <el-card shadow="never">
        <template #header>
          <div class="h-3 flex justify-between">
            <span>{{ t('workplace.project') }}</span>
            <el-link
              type="primary"
              :underline="false"
              href="https://github.com/jtcode"
              target="_blank"
            >
              {{ t('action.more') }}
            </el-link>
          </div>
        </template>
      </el-card>

      <el-card shadow="never" class="mt-8px">
        <el-skeleton :loading="loading" animated>
          <el-row :gutter="20" justify="space-between">
            <el-col :xl="10" :lg="10" :md="24" :sm="24" :xs="24">
              <el-card shadow="hover" class="mb-8px">
                <el-skeleton :loading="loading" animated>
                  <Echart :options="pieOptionsData" :height="280" />
                </el-skeleton>
              </el-card>
            </el-col>
            <el-col :xl="14" :lg="14" :md="24" :sm="24" :xs="24">
              <el-card shadow="hover" class="mb-8px">
                <el-skeleton :loading="loading" animated>
                  <Echart :options="barOptionsData" :height="280" />
                </el-skeleton>
              </el-card>
            </el-col>
          </el-row>
        </el-skeleton>
      </el-card>
    </el-col>
    <el-col :xl="8" :lg="8" :md="24" :sm="24" :xs="24" class="mb-8px">
      <el-card shadow="never">
        <template #header>
          <div class="h-3 flex justify-between">
            <span>{{ t('workplace.shortcutOperation') }}</span>
          </div>
        </template>
        <el-skeleton :loading="loading" animated>
          <el-row>
            <el-col v-for="item in shortcut" :key="`team-${item.name}`" :span="8" class="mb-8px">
              <div class="flex items-center">
                <Icon :icon="item.icon" class="mr-8px" :style="{ color: item.color }" />
                <el-link type="default" :underline="false" @click="handleShortcutClick(item.url)">
                  {{ item.name }}
                </el-link>
              </div>
            </el-col>
          </el-row>
        </el-skeleton>
      </el-card>
      <el-card shadow="never" class="mt-8px">
        <template #header>
          <div class="h-3 flex justify-between">
            <span>{{ t('workplace.notice') }}</span>
            <el-link type="primary" :underline="false">{{ t('action.more') }}</el-link>
          </div>
        </template>
        <el-skeleton :loading="loading" animated>
          <div v-for="(item, index) in notice" :key="`dynamics-${index}`">
            <div class="flex items-center">
              <el-avatar :src="avatar" :size="35" class="mr-16px">
                <img src="@/assets/imgs/avatar.png" alt="" />
              </el-avatar>
              <div>
                <div class="text-14px">
                  <Highlight :keys="item.keys.map((v) => t(v))">
                    {{ item.type }} : {{ item.title }}
                  </Highlight>
                </div>
                <div class="mt-16px text-12px text-gray-400">
                  {{ formatTime(item.date, 'yyyy-MM-dd') }}
                </div>
              </div>
            </div>
            <el-divider />
          </div>
        </el-skeleton>
      </el-card>
    </el-col>
  </el-row>
</template>
<script lang="ts" setup>
import { set } from 'lodash-es'
import { EChartsOption } from 'echarts'
import { formatTime } from '@/utils'

import { useUserStore } from '@/store/modules/user'
import type { WorkplaceTotal, Project, Notice, Shortcut } from './types'
import { pieOptions, barOptions } from './echarts-data'
import { useRouter } from 'vue-router'

defineOptions({ name: 'Index' })

const { t } = useI18n()
const router = useRouter()
const userStore = useUserStore()
// const { setWatermark } = useWatermark()
const loading = ref(true)
const avatar = userStore.getUser.avatar
const username = userStore.getUser.nickname
const pieOptionsData = reactive<EChartsOption>(pieOptions) as EChartsOption
// 获取统计数
let totalSate = reactive<WorkplaceTotal>({
  project: 0,
  access: 0,
  todo: 0
})

const getCount = async () => {
  const data = {
    project: 40,
    access: 2340,
    todo: 10
  }
  totalSate = Object.assign(totalSate, data)
}

// 获取项目数
let projects = reactive<Project[]>([])
const getProject = async () => {
  const data = [
    {
      name: '星空微服务架构',
      icon: 'material-symbols:cloud-outline',
      message: 'github.com/company/xingkong-weifuwu',
      personal: '基于Spring Cloud的微服务架构解决方案',
      time: new Date('2025-04-25'),
      color: '#1890ff'
    },
    {
      name: '晨曦Ant Design管理后台',
      icon: 'devicon:antdesign',
      message: 'github.com/company/chenxi-antd-admin',
      personal: '利用Vue3与Vben5（基于Ant Design）构建的管理后台',
      time: new Date('2025-05-30'),
      color: '#e18525'
    },
    {
      name: '夕阳管理移动端',
      icon: 'ant-design:mobile',
      message: 'github.com/company/xiyang-admin-mobile',
      personal: '采用Vue3与Uniapp技术栈开发的管理移动应用',
      time: new Date('2025-06-15'),
      color: '#2979ff'
    }
  ]
  projects = Object.assign(projects, data)
}

// 获取通知公告
let notice = reactive<Notice[]>([])
const getNotice = async () => {
  // const data = [
  //   {
  //     title: '系统支持 JDK 8/17/21，Vue 2/3',
  //     type: '技术兼容性',
  //     keys: ['JDK', 'Vue'],
  //     date: new Date()
  //   },
  //   {
  //     title: '后端提供 Spring Boot 2.7/3.2 + Cloud 双架构',
  //     type: '架构灵活性',
  //     keys: ['Boot', 'Cloud'],
  //     date: new Date()
  //   },
  //   {
  //     title: '全部开源，个人与企业可 100% 直接使用，无需授权',
  //     type: '开源免授权',
  //     keys: ['无需授权'],
  //     date: new Date()
  //   },
  //   {
  //     title: '国内使用最广泛的快速开发平台，远超 10w+ 企业使用',
  //     type: '广泛企业认可',
  //     keys: ['最广泛', '10w+'],
  //     date: new Date()
  //   }
  // ]
  const data = [
    // {
    //   title: '关于系统将于本周末进行维护升级的通知',
    //   type: '系统维护',
    //   keys: ['维护', '升级'],
    //   date: new Date('2025-07-19'),
    //   content:
    //     '为提升系统稳定性，将于本周六（2025年7月20日）凌晨 02:00 - 06:00 进行例行维护升级，期间部分服务将不可用。'
    // },
    // {
    //   title: '新员工培训安排及入职准备提醒',
    //   type: '人事通知',
    //   keys: ['培训', '新员工'],
    //   date: new Date('2025-07-17'),
    //   content:
    //     '2025年第二批新员工培训将于7月22日正式开始，请相关同事提前完成入职资料准备与系统账号申请。'
    // },
    // {
    //   title: '公司将于下周举办技术分享交流会',
    //   type: '活动通知',
    //   keys: ['分享会', '技术交流'],
    //   date: new Date('2025-07-16'),
    //   content:
    //     '为加强团队技术交流，定于7月22日下午14:00在3楼会议室举办技术分享会，欢迎各位同事积极参与。'
    // },
    // {
    //   title: '关于加强信息安全与数据保护的通知',
    //   type: '安全通知',
    //   keys: ['安全', '数据保护'],
    //   date: new Date('2025-07-15'),
    //   content:
    //     '近期发现多起外部攻击尝试，请全体成员加强账号密码管理，定期更新敏感数据访问权限，确保公司信息安全。'
    // },
    // {
    //   title: 'Q3产品迭代计划已发布，请相关团队查阅',
    //   type: '产品通知',
    //   keys: ['迭代', 'Q3'],
    //   date: new Date('2025-07-13'),
    //   content:
    //     '2025年第三季度产品迭代计划已完成评审并发布，请产品、研发、测试等相关团队及时查阅并做好排期准备。'
    // }
  ]
  notice = Object.assign(notice, data)
}

// 获取快捷入口
let shortcut = reactive<Shortcut[]>([])

const getShortcut = async () => {
  const data = [
    // {
    //   name: '首页',
    //   icon: 'ion:home-outline',
    //   url: '/',
    //   color: '#1fdaca'
    // },
    // {
    //   name: '商城中心',
    //   icon: 'ep:shop',
    //   url: '/mall/home',
    //   color: '#ff6b6b'
    // },
    // {
    //   name: 'AI 大模型',
    //   icon: 'tabler:ai',
    //   url: '/ai/chat',
    //   color: '#7c3aed'
    // },
    // {
    //   name: 'ERP 系统',
    //   icon: 'simple-icons:erpnext',
    //   url: '/erp/home',
    //   color: '#3fb27f'
    // },
    // {
    //   name: 'CRM 系统',
    //   icon: 'simple-icons:civicrm',
    //   url: '/crm/backlog',
    //   color: '#4daf1bc9'
    // },
    // {
    //   name: 'IoT 物联网',
    //   icon: 'fa-solid:hdd',
    //   url: '/iot/home',
    //   color: '#1a73e8'
    // }
  ]
  shortcut = Object.assign(shortcut, data)
}

// 用户来源
const getUserAccessSource = async () => {
  const data = [
    { value: 335, name: 'analysis.directAccess' },
    { value: 310, name: 'analysis.mailMarketing' },
    { value: 234, name: 'analysis.allianceAdvertising' },
    { value: 135, name: 'analysis.videoAdvertising' },
    { value: 1548, name: 'analysis.searchEngines' }
  ]
  set(
    pieOptionsData,
    'legend.data',
    data.map((v) => t(v.name))
  )
  pieOptionsData!.series![0].data = data.map((v) => {
    return {
      name: t(v.name),
      value: v.value
    }
  })
}
const barOptionsData = reactive<EChartsOption>(barOptions) as EChartsOption

// 周活跃量
const getWeeklyUserActivity = async () => {
  const data = [
    { value: 13253, name: 'analysis.monday' },
    { value: 34235, name: 'analysis.tuesday' },
    { value: 26321, name: 'analysis.wednesday' },
    { value: 12340, name: 'analysis.thursday' },
    { value: 24643, name: 'analysis.friday' },
    { value: 1322, name: 'analysis.saturday' },
    { value: 1324, name: 'analysis.sunday' }
  ]
  set(
    barOptionsData,
    'xAxis.data',
    data.map((v) => t(v.name))
  )
  set(barOptionsData, 'series', [
    {
      name: t('analysis.activeQuantity'),
      data: data.map((v) => v.value),
      type: 'bar'
    }
  ])
}

const getAllApi = async () => {
  await Promise.all([
    getCount(),
    getProject(),
    getNotice(),
    getShortcut(),
    getUserAccessSource(),
    getWeeklyUserActivity()
  ])
  loading.value = false
}

const handleProjectClick = (message: string) => {
  window.open(`https://${message}`, '_blank')
}

const handleShortcutClick = (url: string) => {
  router.push(url)
}

getAllApi()
</script>
