<template>
  <view class="history-page">
    <view class="header-section">
      <view class="header-title">历史记录</view>
      <view class="filter-tabs">
        <view
          class="tab-item"
          v-for="(tab, index) in timeFilters"
          :key="index"
          :class="{ active: currentFilter === tab.value }"
          @click="switchFilter(tab.value)"
        >
          {{ tab.label }}
        </view>
      </view>
    </view>

    <scroll-view
      class="list-scroll"
      scroll-y="true"
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
      @scrolltolower="onLoadMore"
    >
      <view class="list-container">
        <view
          class="card-item"
          v-for="(item, index) in displayRows"
          :key="index"
        >
          <view class="card-header">
            <view class="header-left">
              <text class="date-text">{{ item.date }}</text>
            </view>
            <view class="header-right">
              <text class="alert-count" v-if="item.alertCount > 0"
                >{{ item.alertCount }}次预警</text
              >
            </view>
          </view>

          <view class="card-body">
            <view class="row">
              <text class="label">公司名称：</text>
              <text class="value">{{ item.receiverName }}</text>
              <text
                class="level-tag"
                :class="getLevelClass(item.customerLevel)"
              >
                {{ item.customerLevel }}
              </text>
            </view>
            <view class="info-grid">
              <view class="info-item">
                <text class="label">省份</text>
                <text class="value">{{ item.receiverProvince }}</text>
              </view>
              <view class="info-item">
                <text class="label">性质</text>
                <text class="value">{{ item.customerNature }}</text>
              </view>
              <view class="info-item">
                <text class="label">责任人</text>
                <text class="value">{{ item.responderName }}</text>
              </view>
            </view>
          </view>
        </view>

        <view class="loading-more" v-if="loading">
          <image
            class="loading-icon"
            src="../../../../static/images/loading.png"
          />
          <text class="loading-text">加载中...</text>
        </view>

        <view v-if="!loading && displayRows.length === 0" class="empty-data">
          <EmptyView text="暂无历史记录" />
        </view>

        <view v-if="!hasMore && displayRows.length > 0" class="no-more">
          <text>没有更多数据了</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import EmptyView from "../../../../wigets/empty.vue";
import { formatDate } from "../../../../utils/utils.js";

const PROVINCES = ["北京市", "上海市", "广东省", "浙江省", "江苏省"];

export default {
  components: {
    EmptyView,
  },
  data() {
    return {
      isRefreshing: false,
      loading: false,
      rows: [],
      hasMore: true,
      pageNum: 1,
      pageSize: 20,
      currentFilter: "all",
      timeFilters: [
        { label: "全部", value: "all" },
        { label: "近7天", value: "7" },
        { label: "近15天", value: "15" },
        { label: "近30天", value: "30" },
      ],
    };
  },
  computed: {
    displayRows() {
      return this.rows;
    },
  },
  created() {
    this.resetFetch();
  },
  methods: {
    formatDate,

    getLevelClass(level) {
      if (level === "VIP") return "tag-vip";
      if (level === "二级") return "tag-l2";
      if (level === "三级") return "tag-l3";
      return "tag-default";
    },

    switchFilter(value) {
      if (this.currentFilter === value) return;
      this.currentFilter = value;
      this.resetFetch();
    },

    generateFakeData() {
      const newRows = [];
      const count = 15;
      const today = new Date();

      for (let i = 0; i < count; i++) {
        const date = new Date(today);
        // Random date within last 30 days
        const daysBack = Math.floor(Math.random() * 30);

        // Filter logic simulation
        if (this.currentFilter !== "all") {
          const limit = parseInt(this.currentFilter);
          if (daysBack > limit) continue;
        }

        date.setDate(date.getDate() - daysBack);

        newRows.push({
          id: Math.random().toString(36).substr(2, 9),
          date: formatDate(date, "YYYY-MM-DD"),
          receiverName: `历史收货企业${Math.floor(Math.random() * 100)}有限公司`,
          receiverProvince:
            PROVINCES[Math.floor(Math.random() * PROVINCES.length)],
          customerNature: Math.random() > 0.5 ? "协议客户" : "非协议客户",
          alertCount: Math.floor(Math.random() * 5),
          customerLevel: ["VIP", "二级", "三级"][Math.floor(Math.random() * 3)],
        });
      }

      // Sort by date descending
      newRows.sort((a, b) => new Date(b.date) - new Date(a.date));

      return newRows;
    },

    async onRefresh() {
      this.isRefreshing = true;
      this.pageNum = 1;
      this.hasMore = true;
      setTimeout(() => {
        this.rows = this.generateFakeData();
        this.isRefreshing = false;
      }, 1000);
    },

    onLoadMore() {
      if (this.loading || !this.hasMore) return;
      this.loading = true;
      this.pageNum++;
      setTimeout(() => {
        const more = this.generateFakeData();
        if (more.length > 0) {
          this.rows = [...this.rows, ...more];
        } else {
          this.hasMore = false;
        }
        this.loading = false;
      }, 800);
    },

    resetFetch() {
      this.loading = true;
      this.pageNum = 1;
      this.hasMore = true;
      this.rows = [];
      setTimeout(() => {
        this.rows = this.generateFakeData();
        this.loading = false;
      }, 500);
    },
  },
};
</script>

<style scoped>
.level-tag {
  font-size: 20rpx;
  padding: 4rpx 12rpx;
  border-radius: 8rpx;
  white-space: nowrap;
  line-height: 1.2;
  margin-left: 10rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-right {
  flex-shrink: 0;
}

.tag-vip {
  background: linear-gradient(135deg, #e6f7ff, #bae7ff);
  color: #096dd9;
}

.tag-l2 {
  background: linear-gradient(135deg, #f6ffed, #d9f7be);
  color: #389e0d;
}

.tag-l3 {
  background: linear-gradient(135deg, #fff7e6, #ffe7ba);
  color: #d46b08;
}
.history-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: #f5f7fa;
}

.header-section {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  padding: 30rpx 30rpx 60rpx;
  color: #fff;
  border-bottom-left-radius: 40rpx;
  border-bottom-right-radius: 40rpx;
  box-shadow: 0 10rpx 30rpx rgba(24, 144, 255, 0.2);
  z-index: 10;
  position: relative;
}

.header-title {
  font-size: 36rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 30rpx;
}

.filter-tabs {
  display: flex;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 16rpx;
  padding: 8rpx;
}

.tab-item {
  flex: 1;
  text-align: center;
  font-size: 26rpx;
  padding: 12rpx 0;
  border-radius: 12rpx;
  color: rgba(255, 255, 255, 0.8);
  transition: all 0.3s;
}

.tab-item.active {
  background: #fff;
  color: #1890ff;
  font-weight: 600;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.list-scroll {
  flex: 1;
  height: 0;
  margin-top: -40rpx;
  z-index: 11;
  padding: 0 24rpx;
  box-sizing: border-box;
}

.list-container {
  padding-bottom: 40rpx;
}

.card-item {
  background: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #f5f5f5;
}

.date-text {
  font-size: 28rpx;
  font-weight: 600;
  color: #333;
}

.alert-count {
  font-size: 22rpx;
  color: #ff4d4f;
  background: rgba(255, 77, 79, 0.1);
  padding: 4rpx 12rpx;
  border-radius: 8rpx;
}

.card-body {
  font-size: 26rpx;
}

.row {
  display: flex;
  margin-bottom: 16rpx;
}

.info-grid {
  display: flex;
  justify-content: space-between;
}

.info-item {
  display: flex;
  align-items: center;
  width: 48%;
}

.label {
  color: #999;
  margin-right: 12rpx;
}

.value {
  color: #666;
  font-weight: 500;
}

.loading-more,
.no-more,
.empty-data {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30rpx 0;
  color: #999;
  font-size: 24rpx;
}

.loading-icon {
  width: 32rpx;
  height: 32rpx;
  margin-right: 12rpx;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
