<template>
  <div class="my-message-list">
    <div class="header">
      <h2>我的消息</h2>
      <div class="header-actions">
        <el-badge :value="unreadCount" :hidden="unreadCount === 0">
          <el-button @click="loadMessages">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </el-badge>
        <el-button @click="handleMarkAllRead" :disabled="selectedMessages.length === 0">
          <el-icon><Check /></el-icon>
          标记已读
        </el-button>
      </div>
    </div>

    <!-- 筛选条件 -->
    <el-form :model="filterForm" inline class="filter-form">
      <el-form-item label="消息类型">
        <el-select class="w-full" v-model="filterForm.messageType" placeholder="全部" clearable>
          <el-option label="通知" value="notice" />
          <el-option label="公告" value="announcement" />
          <el-option label="预警" value="warning" />
          <el-option label="系统消息" value="system" />
        </el-select>
      </el-form-item>
      <el-form-item label="阅读状态">
        <el-select class="w-full" v-model="filterForm.isRead" placeholder="全部" clearable>
          <el-option label="未读" :value="false" />
          <el-option label="已读" :value="true" />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级">
        <el-select class="w-full" v-model="filterForm.priority" placeholder="全部" clearable>
          <el-option label="紧急" value="urgent" />
          <el-option label="重要" value="important" />
          <el-option label="普通" value="normal" />
        </el-select>
      </el-form-item>
      <el-form-item class="filter-actions">
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 消息列表 -->
    <div ref="messageContainerRef" class="message-cards" v-loading="loading">
      <el-checkbox-group v-model="selectedMessages" class="message-group">
        <div
          v-for="msg in messageList"
          :key="msg.messageID"
          class="message-card"
          :class="{ unread: !msg.isRead, urgent: msg.priority === 'urgent' }"
        >
          <div class="card-header">
            <el-checkbox :value="msg.messageID" class="message-checkbox" />
            <div class="card-title-area" @click="handleViewDetail(msg)">
              <span v-if="!msg.isRead" class="unread-dot"></span>
              <h3 class="card-title">{{ msg.title }}</h3>
              <MessagePriorityBadge :priority="msg.priority || ''" />
            </div>
            <div class="card-actions">
              <el-button
                v-if="!msg.isRead"
                type="primary"
                size="small"
                link
                @click.stop="handleMarkAsRead(msg.messageID)"
              >
                标记已读
              </el-button>
              <el-button
                type="primary"
                size="small"
                link
                @click="handleViewDetail(msg)"
              >
                查看详情
              </el-button>
            </div>
          </div>

          <div class="card-body" @click="handleViewDetail(msg)">
            <div class="message-meta">
              <el-tag :type="getMessageTypeTag(msg.messageType)" size="small">
                {{ getMessageTypeText(msg.messageType) }}
              </el-tag>
              <span class="sender-info">
                <el-icon><User /></el-icon>
                {{ msg.senderName }} · {{ msg.senderDeptName }}
              </span>
              <span class="send-time">
                <el-icon><Clock /></el-icon>
                {{ formatDateTime(msg.actualSendTime) }}
              </span>
              <el-tag
                v-if="msg.isRead"
                type="success"
                size="small"
              >
                已读于 {{ formatDateTime(msg.readTime) }}
              </el-tag>
            </div>

            <div class="message-preview">
              {{ getPreviewText(msg.content) }}
            </div>
          </div>
        </div>
      </el-checkbox-group>

      <el-empty
        v-if="messageList.length === 0 && !loading"
        description="暂无消息"
      />

      <!-- 移动端加载更多提示 -->
      <div v-if="isMobileDevice" class="mobile-load-more">
        <div v-if="loadingMore" class="loading-tip">
          <el-icon class="is-loading"><Loading /></el-icon>
          <span>加载中...</span>
        </div>
        <div v-else-if="!hasMore && messageList.length > 0" class="no-more-tip">
          没有更多了
        </div>
      </div>
    </div>

    <!-- 分页组件 (移动端隐藏) -->
    <div v-if="!isMobileDevice" class="pagination-container">
      <el-pagination
        v-model:current-page="pageParam.pageNumber"
        v-model:page-size="pageParam.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { Refresh, Check, User, Clock, Loading } from '@element-plus/icons-vue';
import { messageApi } from '@/apis/message';
import type { MessageUserVO, PageParam } from '@/types';
import { useDevice } from '@/utils/deviceUtils';
import { MessagePriorityBadge } from '@/components/message';

defineOptions({
  name: 'MyMessageListView'
});

const router = useRouter();
const loading = ref(false);

// 移动端相关
const { isMobileDevice } = useDevice();
const loadingMore = ref(false);
const messageContainerRef = ref<HTMLElement | null>(null);

const messageList = ref<MessageUserVO[]>([]);
const selectedMessages = ref<string[]>([]);
const unreadCount = ref(0);

// 分页参数
const pageParam = ref<PageParam>({
  pageNumber: 1,
  pageSize: 10
});
const total = ref(0);
const hasMore = computed(() => messageList.value.length < total.value);

// 筛选表单
const filterForm = ref<Partial<MessageUserVO>>({
  messageType: undefined,
  priority: undefined,
  isRead: undefined
});

/** 获取消息类型标签 */
function getMessageTypeTag(type?: string): string {
  const map: Record<string, string> = {
    notice: 'info',
    announcement: 'warning',
    warning: 'danger',
    system: 'success'
  };
  return map[type || ''] || '';
}

/** 获取消息类型文本 */
function getMessageTypeText(type?: string): string {
  const map: Record<string, string> = {
    notice: '通知',
    announcement: '公告',
    warning: '预警',
    system: '系统'
  };
  return map[type || ''] || type || '';
}

/** 格式化日期时间 */
function formatDateTime(dateStr?: string): string {
  if (!dateStr) return '-';
  const date = new Date(dateStr);
  const now = new Date();
  const diff = now.getTime() - date.getTime();

  // 1分钟内
  if (diff < 60000) {
    return '刚刚';
  }
  // 1小时内
  if (diff < 3600000) {
    return `${Math.floor(diff / 60000)}分钟前`;
  }
  // 24小时内
  if (diff < 86400000) {
    return `${Math.floor(diff / 3600000)}小时前`;
  }
  // 7天内
  if (diff < 604800000) {
    return `${Math.floor(diff / 86400000)}天前`;
  }

  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
}

/** 获取预览文本 */
function getPreviewText(content?: string): string {
  if (!content) return '';
  return content.length > 100 ? content.substring(0, 100) + '...' : content;
}

/** 加载消息列表 */
async function loadMessages() {
  loading.value = true;
  try {
    const result = await messageApi.getMyMessages(pageParam.value, filterForm.value);
    if (result.success) {
      messageList.value = result.dataList || [];
      total.value = result.pageParam?.totalElements || 0;
    } else {
      ElMessage.error(result.message || '加载失败');
    }
  } catch (error) {
    console.error('加载消息列表失败:', error);
    ElMessage.error('加载失败');
  } finally {
    loading.value = false;
  }
}

/** 加载未读数量 */
async function loadUnreadCount() {
  try {
    const result = await messageApi.getUnreadCount();
    if (result.success) {
      unreadCount.value = result.data || 0;
    }
  } catch (error) {
    console.error('加载未读数量失败:', error);
  }
}

/** 搜索 */
function handleSearch() {
  pageParam.value.pageNumber = 1;
  messageList.value = []; // 清空列表
  loadMessages();
}

/** 重置 */
function handleReset() {
  filterForm.value = {
    isRead: undefined
  };
  messageList.value = []; // 清空列表
  handleSearch();
}

/** 分页大小变更 */
function handleSizeChange() {
  pageParam.value.pageNumber = 1;
  loadMessages();
}

/** 页码变更 */
function handlePageChange() {
  loadMessages();
}

/** 查看详情 */
async function handleViewDetail(msg: MessageUserVO) {
  // 如果是未读消息，先标记为已读
  if (!msg.isRead && msg.messageID) {
    await handleMarkAsRead(msg.messageID, false);
  }

  router.push(`/user/message/detail/${msg.messageID}`);
}

/** 标记单条消息为已读 */
async function handleMarkAsRead(messageID?: string, showMessage = true) {
  if (!messageID) return;

  try {
    const result = await messageApi.markAsRead(messageID);
    if (result.success) {
      if (showMessage) {
        ElMessage.success('已标记为已读');
      }
      await loadMessages();
      await loadUnreadCount();
    } else {
      ElMessage.error(result.message || '操作失败');
    }
  } catch (error) {
    console.error('标记已读失败:', error);
    ElMessage.error('操作失败');
  }
}

/** 批量标记为已读 */
async function handleMarkAllRead() {
  if (selectedMessages.value.length === 0) {
    ElMessage.warning('请选择要标记的消息');
    return;
  }

  try {
    const result = await messageApi.batchMarkAsRead(selectedMessages.value);
    if (result.success) {
      ElMessage.success('已标记为已读');
      selectedMessages.value = [];
      await loadMessages();
      await loadUnreadCount();
    } else {
      ElMessage.error(result.message || '操作失败');
    }
  } catch (error) {
    console.error('批量标记已读失败:', error);
    ElMessage.error('操作失败');
  }
}

onMounted(() => {
  loadMessages();
  loadUnreadCount();
  // 移动端添加滚动监听
  nextTick(() => {
    if (isMobileDevice.value && messageContainerRef.value) {
      setupInfiniteScroll();
    }
  });
});

onUnmounted(() => {
  if (messageContainerRef.value) {
    messageContainerRef.value.removeEventListener('scroll', handleScroll);
  }
});

// 监听设备变化
watch(isMobileDevice, (isMobile) => {
  if (isMobile) {
    nextTick(() => {
      if (messageContainerRef.value) {
        setupInfiniteScroll();
      }
    });
  } else {
    if (messageContainerRef.value) {
      messageContainerRef.value.removeEventListener('scroll', handleScroll);
    }
  }
});

// 设置无限滚动
function setupInfiniteScroll() {
  if (!messageContainerRef.value) return;
  messageContainerRef.value.addEventListener('scroll', handleScroll, { passive: true });
}

// 滚动加载更多
function handleScroll() {
  if (!isMobileDevice.value || loading.value || loadingMore.value || !hasMore.value) return;
  
  const container = messageContainerRef.value;
  if (!container) return;
  
  const { scrollTop, scrollHeight, clientHeight } = container;
  const distanceFromBottom = scrollHeight - scrollTop - clientHeight;
  
  if (distanceFromBottom <= 50) {
    loadMoreMessages();
  }
}

// 加载更多消息
async function loadMoreMessages() {
  if (!hasMore.value || loadingMore.value) return;
  
  loadingMore.value = true;
  try {
    const nextPage = pageParam.value.pageNumber + 1;
    const result = await messageApi.getMyMessages(
      { ...pageParam.value, pageNumber: nextPage },
      filterForm.value
    );
    if (result.success && result.dataList) {
      messageList.value = [...messageList.value, ...result.dataList];
      pageParam.value.pageNumber = nextPage;
      total.value = result.pageParam?.totalElements || 0;
    }
  } catch (error) {
    console.error('加载更多消息失败:', error);
  } finally {
    loadingMore.value = false;
  }
}
</script>

<style lang="scss" scoped>

.my-message-list {
    padding: 20px;
    width: 90%;
    // max-width: 1200px;
    margin: 0 auto;
    min-height: calc(100vh - 76px - 40px);
    display: flex;
    flex-direction: column;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    h2 {
      margin: 0;
      font-size: 24px;
      font-weight: 600;
      color: #333;
    }

    .header-actions {
      display: flex;
      gap: 10px;
    }
  }

  .filter-form {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f9f9f9;
    border-radius: 8px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px 24px;

    ::v-deep(.el-form-item) {
      margin-bottom: 0;
    }

    ::v-deep(.el-select) {
      width: 180px;
    }

    .filter-actions {
      display: flex;
      gap: 12px;

      .el-button {
        min-width: 88px;
      }
    }
  }

  .message-cards {
    .message-group {
      display: flex;
      flex-direction: column;
      gap: 16px;
      width: 100%;
    }

    .message-card {
      background: white;
      border: 1px solid #e0e0e0;
      border-radius: 8px;
      padding: 20px;
      transition: all 0.3s;
      cursor: pointer;

      &:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        border-color: #c8232c;
      }

      &.unread {
        border-left: 4px solid #409eff;
        background-color: #f0f9ff;
      }

      &.urgent {
        border-left: 4px solid #f56c6c;
      }

      .card-header {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 12px;

        .message-checkbox {
          flex-shrink: 0;
        }

        .card-title-area {
          flex: 1;
          display: flex;
          align-items: center;
          gap: 8px;
          cursor: pointer;
          min-height: 32px;
          padding: 4px 0;
          flex-wrap: wrap;

          .unread-dot {
            width: 8px;
            height: 8px;
            background-color: #f56c6c;
            border-radius: 50%;
            flex-shrink: 0;
          }

          .card-title {
            margin: 0;
            font-size: 16px;
            font-weight: 600;
            color: #333;
            flex: 1 1 auto;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            line-height: 1.4;
          }
        }

        .card-actions {
          display: flex;
          gap: 8px;
          flex-shrink: 0;
        }
      }

      .card-body {
        .message-meta {
          display: flex;
          align-items: center;
          gap: 12px;
          margin-bottom: 12px;
          flex-wrap: wrap;
          font-size: 14px;
          color: #666;

          .sender-info,
          .send-time {
            display: flex;
            align-items: center;
            gap: 4px;
          }
        }

        .message-preview {
          font-size: 14px;
          color: #666;
          line-height: 1.6;
        }
      }
    }
  }

  // 移动端加载更多提示
  .mobile-load-more {
    padding: 20px 0;
    text-align: center;

    .loading-tip {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      color: #c8232c;
      font-size: 14px;

      .is-loading {
        animation: rotating 1s linear infinite;
      }
    }

    .no-more-tip {
      color: #9CA3AF;
      font-size: 13px;
    }
  }

  @keyframes rotating {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
  }
}

// 移动端样式适配
@media screen and (max-width: 768px) {
  .my-message-list {
    padding: 12px;
    width: 100%;

    .header {
      flex-direction: column;
      align-items: flex-start;
      gap: 12px;
      margin-bottom: 16px;

      h2 {
        font-size: 20px;
      }

      .header-actions {
        width: 100%;
        justify-content: flex-end;

        .el-button {
          padding: 8px 12px;
          font-size: 13px;
        }
      }
    }

    .filter-form {
      padding: 12px;
      gap: 12px;
      flex-direction: column;
      align-items: stretch;

      :deep(.el-form-item) {
        width: 100%;
        margin-right: 0;

        .el-form-item__label {
          width: 70px;
          font-size: 13px;
        }

        .el-form-item__content {
          flex: 1;
        }
      }

      :deep(.el-select) {
        width: 100% !important;
      }

      .filter-actions {
        width: 100%;
        justify-content: flex-end;

        .el-button {
          min-width: 70px;
        }
      }
    }

    .message-cards {
      max-height: calc(100vh - 280px);
      overflow-y: auto;

      .message-group {
        gap: 12px;
      }

      .message-card {
        padding: 12px;

        .card-header {
          flex-wrap: wrap;
          gap: 8px;

          .card-title-area {
            flex: 1 1 calc(100% - 40px);
            min-width: 0;

            .card-title {
              font-size: 14px;
            }
          }

          .card-actions {
            width: 100%;
            justify-content: flex-end;
            margin-top: 4px;

            .el-button {
              font-size: 12px;
            }
          }
        }

        .card-body {
          .message-meta {
            gap: 8px;
            font-size: 12px;

            .sender-info {
              max-width: 150px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }

          .message-preview {
            font-size: 13px;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            overflow: hidden;
          }
        }
      }
    }

  }
}
</style>
