<template>
  <view class="detail-page">
    <!-- 顶部状态栏 -->
    <view class="header-section">
      <view class="header-content">
        <view class="title-group">
          <text class="main-title">黑名单企业库</text>
          <text class="sub-title">实时监控异常经营企业</text>
        </view>
        <view class="stat-box">
          <text class="stat-num">{{ totalCount }}</text>
          <text class="stat-unit">家</text>
        </view>
      </view>
      <view class="update-tip">
        <text class="tip-icon">🕒</text>
        <text
          >数据更新时间：{{
            formatDate(
              new Date().setDate(new Date().getDate() - 1),
              "YYYY-MM-DD",
            ) || "--"
          }}</text
        >
      </view>
      <!-- 背景装饰 -->
      <view class="header-circle circle-1"></view>
      <view class="header-circle circle-2"></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 rows"
          :key="index"
          @click="toDetail(item)"
        >
          <view class="card-header">
            <view class="header-left">
              <text class="index-num">{{ index + 1 }}</text>
              <text class="company-name">{{ item.receiverName }}</text>
            </view>
            <view class="header-right">
              <text class="status-tag">黑名单</text>
            </view>
          </view>

          <view class="card-body">
            <view class="code-row">
              <text class="label">信用代码：</text>
              <text class="value code-text">{{ item.companyCode }}</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>
          </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 && rows.length === 0" class="empty-data">
          <EmptyView text="无相关数据" />
        </view>

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

    <view class="footer-btn-area">
      <button class="action-btn export-btn" @click="handleExport">
        导出至邮箱
      </button>
    </view>
  </view>
</template>

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

const PROVINCES = ["北京市", "上海市", "广东省", "浙江省", "江苏省"];
const NATURES = ["协议客户", "非协议客户", "商业客户", "终端客户"];

export default {
  components: {
    EmptyView,
  },
  data() {
    return {
      isRefreshing: false,
      loading: false,
      rows: [],
      totalCount: 60, // Simulated total count
      hasMore: true,
      pageNum: 1,
      pageSize: 20,
    };
  },
  created() {
    this.resetFetch();
  },
  methods: {
    formatDate,

    generateFakeData() {
      const newRows = [];
      const codes = [
        "91370104771001730R",
        "91420105783155681H",
        "91330110MA2CCJE32Y",
        "91441581761581268X",
        "91510106768621824L",
      ];

      const startIdx = (this.pageNum - 1) * this.pageSize;
      const endIdx = Math.min(startIdx + this.pageSize, this.totalCount);

      if (startIdx >= this.totalCount) {
        this.hasMore = false;
        return [];
      }

      for (let i = startIdx; i < endIdx; i++) {
        newRows.push({
          id: i,
          receiverName: `测试收货企业${i + 1}有限公司`,
          companyCode: codes[i % codes.length],
          receiverProvince: PROVINCES[i % PROVINCES.length],
          customerNature: NATURES[i % NATURES.length],
        });
      }
      return newRows;
    },

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

    onLoadMore() {
      if (this.loading || !this.hasMore) return;
      this.loading = true;
      this.pageNum++;

      // Simulate network request
      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;
      // Simulate initial load
      setTimeout(() => {
        this.rows = this.generateFakeData();
        this.loading = false;
      }, 500);
    },

    toDetail(item) {
      uni.navigateTo({
        url: `/traceCodePackages/traceabilityReport/pages/blacklist/detail/index?id=${item.id}&name=${encodeURIComponent(item.receiverName)}`,
      });
    },

    handleExport() {
      uni.showModal({
        title: "导出至邮箱",
        editable: true,
        placeholderText: "请输入邮箱地址",
        success: (res) => {
          if (res.confirm) {
            if (!res.content) {
              uni.showToast({
                title: "请输入邮箱",
                icon: "none",
              });
              return;
            }
            uni.showToast({
              title: "导出请求已发送",
              icon: "success",
            });
          }
        },
      });
    },
  },
};
</script>

<style scoped>
.detail-page {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 116rpx - env(safe-area-inset-bottom));
  box-sizing: border-box;
  background: #f5f7fa;
}

/* Header Section */
.header-section {
  background: linear-gradient(135deg, #2b32b2 0%, #1488cc 100%);
  padding: 40rpx 40rpx 80rpx; /* Extra padding at bottom for overlap effect */
  position: relative;
  z-index: 20;
  color: #fff;
  border-bottom-left-radius: 40rpx;
  border-bottom-right-radius: 40rpx;
  overflow: hidden;
  box-shadow: 0 10rpx 30rpx rgba(20, 136, 204, 0.2);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 2;
  margin-bottom: 24rpx;
}

.title-group {
  display: flex;
  flex-direction: column;
}

.main-title {
  font-size: 40rpx;
  font-weight: bold;
  margin-bottom: 8rpx;
  letter-spacing: 2rpx;
}

.sub-title {
  font-size: 24rpx;
  opacity: 0.8;
}

.stat-box {
  display: flex;
  align-items: baseline;
}

.stat-num {
  font-size: 56rpx;
  font-weight: bold;
  margin-right: 8rpx;
  font-family: "DINAlternate-Bold", sans-serif;
}

.stat-unit {
  font-size: 24rpx;
  opacity: 0.8;
}

.update-tip {
  display: inline-flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.15);
  padding: 8rpx 20rpx;
  border-radius: 30rpx;
  font-size: 22rpx;
  backdrop-filter: blur(10px);
  position: relative;
  z-index: 2;
}

.tip-icon {
  margin-right: 8rpx;
  font-size: 20rpx;
}

/* Header Decor Circles */
.header-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.05);
}

.circle-1 {
  width: 300rpx;
  height: 300rpx;
  top: -100rpx;
  right: -50rpx;
}

.circle-2 {
  width: 200rpx;
  height: 200rpx;
  bottom: -50rpx;
  left: -50rpx;
}

/* List Scroll */
.list-scroll {
  flex: 1;
  height: 0;
  padding: 0 24rpx; /* Remove vertical padding from scroll container */
  box-sizing: border-box;
  margin-top: -50rpx; /* Negative margin to pull list up */
  position: relative;
  z-index: 21;
}

.list-container {
  padding-top: 10rpx;
  padding-bottom: calc(200rpx + env(safe-area-inset-bottom));
}

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

.card-item:active {
  transform: scale(0.99);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24rpx;
}

.header-left {
  flex: 1;
  display: flex;
  align-items: center;
  margin-right: 20rpx;
}

.index-num {
  font-size: 24rpx;
  color: #999;
  margin-right: 12rpx;
  font-family: monospace;
}

.company-name {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
}

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

.status-tag {
  font-size: 20rpx;
  padding: 6rpx 16rpx;
  border-radius: 20rpx;
  background: #fff1f0;
  color: #ff4d4f;
  font-weight: 600;
  border: 1rpx solid rgba(255, 77, 79, 0.2);
}

.card-body {
  background: #f9fbfd;
  border-radius: 12rpx;
  padding: 24rpx;
}

.code-row {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #edf0f5;
}

.code-row:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.code-text {
  font-family: monospace;
  color: #333 !important;
  font-weight: 600 !important;
  letter-spacing: 1rpx;
}

.info-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 24rpx;
}

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

.info-item .label,
.code-row .label {
  font-size: 24rpx;
  color: #999;
  margin-right: 12rpx;
}

.info-item .value,
.code-row .value {
  font-size: 26rpx;
  color: #666;
  font-weight: 500;
}

/* Loading & Footer */
.loading-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30rpx 0;
}

.loading-text {
  font-size: 24rpx;
  color: #999;
}

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

.empty-data {
  padding-top: 120rpx;
}

.no-more {
  text-align: center;
  color: #ccc;
  font-size: 24rpx;
  padding: 30rpx 0;
}

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

.footer-btn-area {
  padding: 24rpx 32rpx calc(24rpx + env(safe-area-inset-bottom));
  background: #fff;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.08);
  position: fixed;
  z-index: 100;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
}

.action-btn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 44rpx;
  font-size: 30rpx;
  font-weight: 600;
  text-align: center;
  border: none;
  transition: all 0.3s;
}

.action-btn::after {
  border: none;
}

.action-btn:active {
  transform: scale(0.96);
}

.export-btn {
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  color: #fff;
  box-shadow: 0 6rpx 16rpx rgba(24, 144, 255, 0.35);
}
</style>
