<template>
  <view class="system-log-page">
    <!-- 固定头部 -->
    <view class="fixed-header">
      <text class="header-title">移动式检修车间</text>
    </view>
    
    <!-- 内容区域 -->
    <view class="tabbar-content">
      <!-- 日期查询区域 -->
      <!-- <view class="query-section">
        <view class="query-row">
          <view class="query-item">
            <text class="query-label">查询日期</text>
            <picker mode="date" :value="selectedDate" @change="onDateChange">
              <view class="picker-view">
                <text>{{ selectedDate }}</text>
                <text class="picker-arrow">📅</text>
              </view>
            </picker>
          </view>
          <button class="query-button" @click="queryLogs">查询</button>
        </view>
      </view> -->
      
      <SystemLog ref="systemLog" :query-date="selectedDate" />
    
    <!-- 筛选区域 -->
    <!-- <view class="filter-section">
      <view class="filter-row">
        <view class="filter-item">
          <text class="filter-label">日志级别</text>
          <picker :value="levelIndex" :range="levelOptions" @change="onLevelChange">
            <view class="picker-view">
              <text>{{ levelOptions[levelIndex] }}</text>
              <text class="picker-arrow">▼</text>
            </view>
          </picker>
        </view>
        
      </view>
      
      <view class="filter-row">
        <view class="filter-item">
          <text class="filter-label">关键词</text>
          <input class="search-input" v-model="searchKeyword" placeholder="输入关键词搜索" />
        </view>
        
        <button class="search-button" @click="searchLogs">搜索</button>
      </view>
    </view> -->
    
    <!-- 日志表格 -->
    <!-- <view class="logs-container">
      <view class="logs-header">
        <text class="logs-title">系统日志</text>
        <view class="header-actions">
          <button class="action-button" @click="refreshLogs">刷新</button>
          <button class="action-button" @click="exportLogs">导出</button>
          <button class="action-button clear" @click="clearLogs">清空</button>
        </view>
      </view> -->
      
      <!-- 表格头部 -->
      <!-- <view class="table-header">
        <view class="table-cell event-header">事件</view>
        <view class="table-cell time-header">时间</view>
        <view class="table-cell status-header">状态</view>
      </view> -->
      
      <!-- 表格内容 -->
      <!-- <scroll-view class="table-content" scroll-y="true" @scrolltolower="loadMoreLogs">
        <view class="table-row" v-for="(log, index) in logs" :key="index" :class="log.level">
          <view class="table-cell event-cell">
            <text class="event-message">{{ log.message }}</text>
            <text class="event-details" v-if="log.details">{{ log.details }}</text>
          </view>
          <view class="table-cell time-cell">
            <text class="time-text">{{ log.time }}</text>
          </view>
          <view class="table-cell status-cell">
            <view class="status-badge" :class="log.level">
              <text class="status-text">{{ log.levelText }}</text>
            </view>
          </view>
        </view>
        
        <view class="load-more" v-if="hasMore">
          <text class="load-more-text">加载更多...</text>
        </view>
      </scroll-view>
    </view> -->
    </view>
  </view>
</template>

<script>
import SystemLog from '@/components/SystemLog.vue'
export default {
  components: {
    SystemLog
  },
  data() {
    return {
      selectedDate: this.getTodayDate(), // 默认选择今天
      levelIndex: 0,
      levelOptions: ['全部级别', '错误', '警告', '信息', '调试'],
      logDate: '2025-09-29',
      searchKeyword: '',
      totalLogs: 0,
      errorLogs: 0,
      warningLogs: 0,
      infoLogs: 0,
      hasMore: true,
      logs: [],
    }
  },
  onLoad() {
    console.log('系统日志页面加载')
  },
  onShow() {
    console.log('📱 系统日志页面显示，触发页面更新')
    // 触发子组件重新初始化
    this.$refs.systemLog?.refreshData?.()
  },
  methods: {
    // 获取今天的日期
    getTodayDate() {
      const today = new Date()
      const year = today.getFullYear()
      const month = String(today.getMonth() + 1).padStart(2, '0')
      const day = String(today.getDate()).padStart(2, '0')
      return `${year}-${month}-${day}`
    },
    
    // 日期选择变化
    onDateChange(e) {
      this.selectedDate = e.detail.value
      console.log('选择日期:', this.selectedDate)
    },
    
    // 查询日志
    queryLogs() {
      console.log('查询日期:', this.selectedDate)
      
      // 显示加载提示
      uni.showLoading({
        title: '查询中...'
      })
      
      // 调用子组件的查询方法
      if (this.$refs.systemLog && this.$refs.systemLog.queryByDate) {
        this.$refs.systemLog.queryByDate(this.selectedDate).finally(() => {
          uni.hideLoading()
        })
      } else {
        // 如果子组件方法不存在，则刷新数据
        if (this.$refs.systemLog && this.$refs.systemLog.refreshData) {
          this.$refs.systemLog.refreshData().finally(() => {
            uni.hideLoading()
          })
        } else {
          uni.hideLoading()
        }
      }
    },
    
    onLevelChange(e) {
      this.levelIndex = e.detail.value
    },
    searchLogs() {
      console.log('搜索日志', {
        level: this.levelOptions[this.levelIndex],
        date: this.logDate,
        keyword: this.searchKeyword
      })
      uni.showToast({
        title: '搜索中...',
        icon: 'loading'
      })
    },
    refreshLogs() {
      uni.showToast({
        title: '刷新日志',
        icon: 'success'
      })
    },
    exportLogs() {
      uni.showToast({
        title: '导出功能开发中',
        icon: 'none'
      })
    },
    clearLogs() {
      uni.showModal({
        title: '确认清空',
        content: '确定要清空所有日志吗？此操作不可恢复。',
        success: (res) => {
          if (res.confirm) {
            this.logs = []
            this.totalLogs = 0
            this.errorLogs = 0
            this.warningLogs = 0
            this.infoLogs = 0
            uni.showToast({
              title: '清空成功',
              icon: 'success'
            })
          }
        }
      })
    },
    loadMoreLogs() {
      if (this.hasMore) {
        uni.showToast({
          title: '加载更多...',
          icon: 'loading'
        })
        // 模拟加载更多数据
        setTimeout(() => {
          this.hasMore = false
          uni.hideToast()
        }, 1000)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.system-log-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 查询区域样式 */
.query-section {
  background: white;
  border-radius: 12rpx;
  padding: 25rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
}

.query-row {
  display: flex;
  align-items: flex-end;
  gap: 20rpx;
}

.query-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.query-label {
  font-size: 24rpx;
  color: #666;
  font-weight: 500;
}

.picker-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx;
  background-color: #f8f8f8;
  border-radius: 10rpx;
  border: 2rpx solid #e0e0e0;
  transition: all 0.2s ease;
}

.picker-view:active {
  background-color: #f0f0f0;
  border-color: #3f51b5;
}

.picker-arrow {
  color: #3f51b5;
  font-size: 24rpx;
}

.query-button {
  background: linear-gradient(135deg, #3f51b5 0%, #5c6bc0 100%);
  color: white;
  padding: 20rpx 30rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  font-weight: 500;
  border: none;
  box-shadow: 0 4rpx 12rpx rgba(63, 81, 181, 0.3);
  transition: all 0.2s ease;
}

.query-button:active {
  transform: translateY(1rpx);
  box-shadow: 0 2rpx 8rpx rgba(63, 81, 181, 0.4);
}

.query-button::after {
  border: none;
}

.filter-section {
  background: white;
  border-radius: 12rpx;
  padding: 25rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
}

.filter-row {
  display: flex;
  gap: 20rpx;
  margin-bottom: 20rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.filter-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.filter-label {
  font-size: 24rpx;
  color: #666;
}

.picker-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx;
  background-color: #f8f8f8;
  border-radius: 10rpx;
  border: 2rpx solid #e0e0e0;
}

.picker-arrow {
  color: #999;
  font-size: 24rpx;
}

.search-input {
  padding: 20rpx;
  background-color: #f8f8f8;
  border-radius: 10rpx;
  border: 2rpx solid #e0e0e0;
  font-size: 28rpx;
}

.search-button {
  background-color: #3f51b5;
  color: white;
  padding: 20rpx 30rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  align-self: flex-end;
}

.statistics-section {
  background: white;
  border-radius: 12rpx;
  padding: 25rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  display: flex;
  justify-content: space-around;
}

.stat-item {
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.stat-value {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  
  &.error {
    color: #ff4444;
  }
  
  &.warning {
    color: #ff9800;
  }
  
  &.info {
    color: #2196f3;
  }
}

.logs-container {
  background: white;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  overflow: hidden;
}

.logs-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx;
  border-bottom: 2rpx solid #f0f0f0;
}

.logs-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.header-actions {
  display: flex;
  gap: 15rpx;
}

.action-button {
  background-color: #3f51b5;
  color: white;
  padding: 10rpx 20rpx;
  border-radius: 6rpx;
  font-size: 24rpx;
  
  &.clear {
    background-color: #ff4444;
  }
}

/* 表格样式 */
.table-header {
  display: flex;
  background-color: #f8f9fa;
  border-bottom: 2rpx solid #e0e0e0;
}

.table-cell {
  padding: 20rpx 15rpx;
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  border-right: 1rpx solid #e0e0e0;
  
  &:last-child {
    border-right: none;
  }
}

.event-header {
  flex: 2;
  text-align: left;
}

.time-header {
  flex: 1.2;
  text-align: center;
}

.status-header {
  flex: 0.8;
  text-align: center;
}

.table-content {
  height: 600rpx;
}

.table-row {
  display: flex;
  border-bottom: 1rpx solid #f0f0f0;
  
  &:last-child {
    border-bottom: none;
  }
  
  &:hover {
    background-color: #f8f9fa;
  }
  
  &.error {
    background-color: #fff5f5;
  }
  
  &.warning {
    background-color: #fffbf0;
  }
  
  &.info {
    background-color: #f0f8ff;
  }
}

.event-cell {
  flex: 2;
  padding: 20rpx 15rpx;
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.event-message {
  font-size: 28rpx;
  color: #333;
  line-height: 1.4;
}

.event-details {
  font-size: 24rpx;
  color: #666;
  line-height: 1.3;
  background-color: #f8f8f8;
  padding: 8rpx 12rpx;
  border-radius: 6rpx;
}

.time-cell {
  flex: 1.2;
  padding: 20rpx 15rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.time-text {
  font-size: 24rpx;
  color: #666;
  text-align: center;
}

.status-cell {
  flex: 0.8;
  padding: 20rpx 15rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-badge {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 22rpx;
  font-weight: bold;
  
  &.error {
    background-color: #ffebee;
    color: #ff4444;
  }
  
  &.warning {
    background-color: #fff3e0;
    color: #ff9800;
  }
  
  &.info {
    background-color: #e3f2fd;
    color: #2196f3;
  }
}

.status-text {
  font-size: 22rpx;
}

.load-more {
  padding: 30rpx;
  text-align: center;
}

.load-more-text {
  font-size: 28rpx;
  color: #666;
}
</style>