<template>
  <div class="app-container home">
    <header>
      <div class="icon">
        <el-avatar :size="60" :src="avatar" />
      </div>
      <div class="info">
        <h3>{{ warmMessage }}</h3>
        <p>
          <span>{{ nickname }}</span>
          <el-divider direction="vertical" />
          <span>{{ userinfo.userName }}</span>
          <el-divider direction="vertical" />
          <span>{{ userinfo.dept.deptName || $t('home.dept.noSet') }}</span>
        </p>
        <p v-if="hasBirthday" class="birthday-message">
          🎂{{ birthdayMessage }}🎉🎁🎈
        </p>
      </div>
      <div v-show="false" class="count">
        <el-statistic
          :value="0"
          :value-style="{ fontSize: '32px' }"
          title="我的待办"
        />
        <el-divider direction="vertical" />
        <el-statistic
          :value="0"
          :value-style="{ fontSize: '32px' }"
          title="我的已办"
        />
      </div>
    </header>
    <section>
      <div v-if="noticeList.length > 0" class="notice" v-show="false">
        <div class="notice-title">
          <span>公告</span>
        </div>
        <div class="notice-content">
          <div
            v-if="noticeList.length > 0"
            class="notice-content-item"
            @click="showNoticeDetail(noticeList[currentNoticeIndex])"
          >
            <div class="notice-title-text">
              {{ noticeList[currentNoticeIndex].noticeTitle }}
            </div>
            <div class="notice-time">
              {{ formatTime(noticeList[currentNoticeIndex].createTime) }}
            </div>
          </div>
          <div v-else class="notice-content-item no-notice">暂无公告</div>
        </div>
      </div>
      <div class="apps">
        <router-link to="/m/checkin" class="app-card">
          <svg-icon icon-class="icon-one" style="font-size: 24px" />
          <span>{{ $t('home.checkin.title') }}</span>
          <i class="el-icon-arrow-right" />
        </router-link>
      </div>
    </section>

    <!-- 通知详情弹窗 -->
    <el-dialog
      title="公告详情"
      :visible.sync="noticeDetailVisible"
      width="960px"
      :fullscreen="!$_isMobile() ? false : true"
      :before-close="closeNoticeDetail"
    >
      <div v-if="selectedNotice" class="notice-detail">
        <div class="notice-detail-title">
          {{ selectedNotice.noticeTitle }}
        </div>
        <div class="notice-detail-meta">
          <span>发布时间：{{ formatTime(selectedNotice.createTime) }}</span>
        </div>
        <div
          class="notice-detail-content"
          v-html="selectedNotice.noticeContent"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeNoticeDetail">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import messageGenerator from '@/utils/warmMessageGenerator'
import { listNotice } from '@/api/system/notice'
import moment from 'moment'
import ResizeMixin from '@/layout/mixin/ResizeHandler'
export default {
  name: 'Index',
  mixins: [ResizeMixin],
  data() {
    return {
      warmMessage: '',
      noticeList: [],
      currentNoticeIndex: 0,
      noticeDetailVisible: false,
      selectedNotice: null,
      autoScrollTimer: null
    }
  },
  computed: {
    ...mapGetters(['userinfo', 'avatar', 'nickname', 'language']),
    hasBirthday() {
      if (!this.userinfo.birthday) return false
      // this.userinfo.birthday 格式为 2025-07-24, 比对月、日
      const today = new Date().toISOString().split('T')[0]
      const birthday = this.userinfo.birthday.split('-')

      const month = birthday[1]
      const day = birthday[2]

      const todayMonth = today.split('-')[1]
      const todayDay = today.split('-')[2]

      console.log(month, todayMonth)

      return month == todayMonth && day == todayDay
    },
    birthdayMessage() {
      if (!this.userinfo.birthday) return ''
      return this.$t('home.birthday', {
        name: this.nickname,
        birthday: moment(this.userinfo.birthday).format(`M/D`)
      })
    }
  },
  watch: {
    language: {
      handler(newVal) {
        console.log(newVal)
        this.warmMessage = messageGenerator(
          this.userinfo.nickName
        ).generateMessage()
      },
      immediate: true
    }
  },
  created() {
    this.getNoticeList()
  },
  beforeDestroy() {
    this.clearAutoScroll()
  },
  methods: {
    getNoticeList() {
      listNotice({
        pageNum: 1,
        pageSize: 20,
        status: '0'
      }).then((response) => {
        this.noticeList = response.rows.filter((item) => item.status === '0')
        // 数据加载完成后启动自动滚动
        this.$nextTick(() => {
          this.startAutoScroll()
        })
      })
    },
    goTarget(href) {
      window.open(href, '_blank')
    },
    /**
     * 开始自动滚动通知
     * 每3秒切换到下一条通知
     */
    startAutoScroll() {
      if (this.noticeList.length <= 1) return

      this.autoScrollTimer = setInterval(() => {
        this.currentNoticeIndex =
          (this.currentNoticeIndex + 1) % this.noticeList.length
      }, 3000)
    },
    /**
     * 清除自动滚动定时器
     */
    clearAutoScroll() {
      if (this.autoScrollTimer) {
        clearInterval(this.autoScrollTimer)
        this.autoScrollTimer = null
      }
    },
    /**
     * 显示通知详情
     * @param {Object} notice 通知对象
     */
    showNoticeDetail(notice) {
      this.selectedNotice = notice
      this.noticeDetailVisible = true
      // 暂停自动滚动
      this.clearAutoScroll()
    },
    /**
     * 关闭通知详情弹窗
     */
    closeNoticeDetail() {
      this.noticeDetailVisible = false
      this.selectedNotice = null
      // 重新开始自动滚动
      this.startAutoScroll()
    },
    /**
     * 格式化时间显示
     * @param {String} time 时间字符串
     * @returns {String} 格式化后的时间
     */
    formatTime(time) {
      if (!time) return ''
      return moment(time).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>

<style scoped lang="scss">
.home {
  width: 100%;
  padding: 0;
  ul {
    padding: 0;
    margin: 0;
    list-style-type: none;
  }
  p {
    padding: 0;
    margin: 0;
  }
  & > header {
    height: 150px;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 16px;
    border-radius: 0 0 8px 8px;

    .icon {
      margin-right: 20px;
    }

    .info {
      flex-grow: 1;
      width: 0;
      h3 {
        margin: 0;
        margin-bottom: 10px;
        font-size: 20px;
      }
    }

    .count {
      display: flex;
      align-items: center;
      ::v-deep {
        .el-divider {
          margin: 0 25px;
          height: 3em;
        }
        .el-statistic {
          cursor: pointer;
        }
      }
    }
  }

  & > section {
    padding: 16px;
  }
}
.birthday-message {
  margin: 10px 0 0 0 !important;
  overflow: hidden !important;
  color: #e6a23c;
}
.apps {
  display: grid;
  grid-template-columns: repeat(5, 1fr);

  @media (max-width: 1024px) {
    grid-template-columns: repeat(4, 1fr);
  }

  @media (max-width: 768px) {
    grid-template-columns: repeat(2, 1fr);
  }

  @media (max-width: 480px) {
    grid-template-columns: repeat(1, 1fr);
  }

  gap: 16px;
  .app-card {
    background: linear-gradient(to top, #fff, #f1f1f1);
    border-radius: 8px;
    padding: 30px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #333;
    text-decoration: none;
    border: 1px solid #e0e0e0;
    span {
      font-size: 16px;
      font-weight: 600;
    }
  }
}

/* 通知相关样式 */
.notice {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 16px;
  overflow: hidden;
  border: 1px solid #e0e0e0;
  width: 50%;
  @media (max-width: 768px) {
    width: 100%;
  }

  .notice-title {
    background: linear-gradient(135deg, #409eff, #67c23a);
    color: #fff;
    padding: 8px 16px;
    font-weight: 600;
    font-size: 14px;
  }

  .notice-content {
    height: 80px;
    position: relative;
    overflow: hidden;

    .notice-content-item {
      height: 80px;
      padding: 16px;
      cursor: pointer;
      display: flex;
      flex-direction: column;
      justify-content: center;
      transition: all 0.3s ease;
      animation: slideInUp 0.5s ease-out;

      &:hover {
        background-color: #f5f7fa;
        transform: translateY(-2px);
      }

      .notice-title-text {
        font-size: 14px;
        font-weight: 500;
        color: #303133;
        margin-bottom: 8px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .notice-time {
        font-size: 12px;
        color: #909399;
      }

      &.no-notice {
        color: #909399;
        text-align: center;
        cursor: default;

        &:hover {
          background-color: transparent;
          transform: none;
        }
      }
    }
  }
}

/* 通知详情弹窗样式 */
.notice-detail {
  .notice-detail-title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid #e4e7ed;
  }

  .notice-detail-meta {
    margin-bottom: 16px;
    color: #909399;
    font-size: 14px;
  }

  .notice-detail-content {
    font-size: 14px;
    line-height: 1.6;
    color: #606266;
    min-height: 100px;
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

/* 滑入动画 */
@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 弹窗样式覆盖 */
::v-deep .el-dialog {
  .el-dialog__header {
    background: linear-gradient(135deg, #409eff, #67c23a);
    color: #fff;
    padding: 20px;

    .el-dialog__title {
      color: #fff;
      font-weight: 600;
    }

    .el-dialog__close {
      color: #fff;

      &:hover {
        color: #f0f0f0;
      }
    }
  }

  .el-dialog__body {
    padding: 20px;
  }

  .el-dialog__footer {
    padding: 10px 20px 20px;
    text-align: right;
  }
}
</style>
