<template>
  <view class="history-container">
    <common-header title="接管历史" theme="distiller" @back="goBack" />
    
    <view class="history-content">
      <view class="filter-section">
        <uni-search-bar 
          v-model="searchText" 
          placeholder="搜索操作记录" 
          @confirm="searchHistory" 
          @clear="clearSearch" />
      </view>
      
      <scroll-view 
        class="scroll-view" 
        scroll-y 
        :refresher-enabled="true"
        :refresher-triggered="refreshing"
        @refresherrefresh="onRefresh"
        @scrolltolower="onLoadMore">
        
        <view v-if="!loading && historyList.length === 0" class="empty-state">
          <uni-icons type="list" size="80" color="#ddd" />
          <text class="empty-text">暂无历史记录</text>
        </view>
        
        <view v-else class="history-list">
          <view 
            v-for="(item, index) in historyList" 
            :key="item.id || index" 
            class="history-item">
            
            <view class="item-header">
              <text class="operation-type">{{ item.operationType }}</text>
              <text class="operation-time">{{ formatTime(item.operationTime) }}</text>
            </view>
            
            <view class="item-content">
              <text class="distiller-name">{{ item.distillerName }}</text>
              <text class="operation-desc">{{ item.description }}</text>
            </view>
            
            <view class="item-footer">
              <text class="operator">操作人: {{ item.operator }}</text>
              <view class="status-badge" :class="{ 
                'success': item.status === 1,
                'pending': item.status === 0,
                'failed': item.status === 2
              }">
                {{ getStatusText(item.status) }}
              </view>
            </view>
          </view>
        </view>
        
        <uni-load-more 
          :status="loadStatus" 
          :content-text="loadText" 
          @clickLoadMore="onLoadMore" />
      </scroll-view>
    </view>
  </view>
</template>

<script>
import CommonHeader from '@/components/common-header/common-header.vue'

export default {
  name: 'DistillerHistory',
  components: {
    CommonHeader
  },
  
  data() {
    return {
      historyList: [],
      loading: false,
      refreshing: false,
      searchText: '',
      page: 1,
      pageSize: 20,
      hasMore: true,
      loadStatus: 'more',
      loadText: {
        contentdown: '上拉加载更多',
        contentrefresh: '正在加载...',
        contentnomore: '没有更多了'
      }
    }
  },
  
  onLoad() {
    this.loadHistoryList()
  },
  
  methods: {
    async loadHistoryList(isRefresh = false) {
      if (isRefresh) {
        this.page = 1
        this.hasMore = true
      }
      
      if (!this.hasMore && !isRefresh) return
      
      this.loading = true
      this.loadStatus = 'loading'
      
      try {
        const params = {
          page: this.page,
          pageSize: this.pageSize,
          search: this.searchText
        }
        
        // TODO: Replace with actual API call
        const response = await this.$http.get('/brewery/distiller/history', { params })
        const { list, total } = response.data || {}
        
        if (isRefresh) {
          this.historyList = list || []
        } else {
          this.historyList = [...this.historyList, ...(list || [])]
        }
        
        this.hasMore = this.historyList.length < total
        this.loadStatus = this.hasMore ? 'more' : 'noMore'
        
      } catch (error) {
        console.error('加载历史记录失败:', error)
        this.$modal.showToast('加载失败')
        this.loadStatus = 'more'
      } finally {
        this.loading = false
        this.refreshing = false
      }
    },
    
    onRefresh() {
      this.refreshing = true
      this.loadHistoryList(true)
    },
    
    onLoadMore() {
      if (this.hasMore && !this.loading) {
        this.page++
        this.loadHistoryList()
      }
    },
    
    searchHistory() {
      this.loadHistoryList(true)
    },
    
    clearSearch() {
      this.searchText = ''
      this.loadHistoryList(true)
    },
    
    getStatusText(status) {
      const statusMap = {
        0: '处理中',
        1: '成功',
        2: '失败'
      }
      return statusMap[status] || '未知'
    },
    
    formatTime(time) {
      if (!time) return ''
      return time.substring(0, 16)
    },
    
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="scss" scoped>
.history-container {
  min-height: 100vh;
  background: #f5f7fa;
}

.history-content {
  padding: 20rpx;
}

.filter-section {
  margin-bottom: 20rpx;
}

.scroll-view {
  height: calc(100vh - 200rpx);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;
  
  .empty-text {
    font-size: 28rpx;
    color: #999;
    margin-top: 20rpx;
  }
}

.history-list {
  .history-item {
    background: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    
    .item-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16rpx;
      
      .operation-type {
        font-size: 30rpx;
        font-weight: bold;
        color: #333;
      }
      
      .operation-time {
        font-size: 24rpx;
        color: #999;
      }
    }
    
    .item-content {
      margin-bottom: 16rpx;
      
      .distiller-name {
        display: block;
        font-size: 28rpx;
        color: #666;
        margin-bottom: 8rpx;
      }
      
      .operation-desc {
        font-size: 26rpx;
        color: #999;
        line-height: 1.5;
      }
    }
    
    .item-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .operator {
        font-size: 24rpx;
        color: #999;
      }
      
      .status-badge {
        padding: 6rpx 12rpx;
        border-radius: 16rpx;
        font-size: 22rpx;
        
        &.success {
          background: #e8f5e8;
          color: #52c41a;
        }
        
        &.pending {
          background: #fff2e8;
          color: #fa8c16;
        }
        
        &.failed {
          background: #ffebee;
          color: #f44336;
        }
      }
    }
  }
}
</style> 