'use client';

/**
 * 合同管理页面
 * [2026-02-05] 新增：全局合同查询、筛选、批量下载功能
 * 回滚方式：删除此目录即可
 */

import { useState, useEffect, useCallback } from 'react';
import { Button, toast } from '@/components/common';
import { PageContainer } from '@/components/layout';
import { cn } from '@/utils/helpers';
import { formatNumber } from '@/utils/formatters';
import {
  contractService,
  CONTRACT_STATUS_LABELS,
  type ContractsListResponse,
  type ContractStatisticsResponse,
  type ContractQueryParams,
} from '@/services/contractService';
import { PROVINCE_CODE_NAMES } from '@/types';
import styles from './contracts.module.scss';

// 获取合同状态对应的样式类
const getStatusStyleClass = (status: string): string => {
  switch (status) {
    case 'SIGNED':
      return styles.contracts__statusGreen;
    case 'PENDING':
    case 'SCROLLED':
    case 'ACKNOWLEDGED':
      return styles.contracts__statusYellow;
    case 'EXPIRED':
    case 'TIMEOUT':
      return styles.contracts__statusRed;
    default:
      return styles.contracts__statusGray;
  }
};

// 省份列表（用于筛选）- 只使用 6 位代码（数据库中存的是 6 位）
const PROVINCE_OPTIONS = Object.entries(PROVINCE_CODE_NAMES)
  .filter(([code]) => code.length === 6)
  .map(([code, name]) => ({
    value: code,
    label: name,
  }));

/**
 * 合同管理页面
 */
export default function ContractsPage() {
  // 筛选状态
  const [filters, setFilters] = useState<ContractQueryParams>({
    page: 1,
    pageSize: 20,
    provinceCode: '',
    cityCode: '',
    status: '',
    signedAfter: '',
    signedBefore: '',
    orderBy: 'signedAt',
    orderDir: 'desc',
  });

  // 数据状态
  const [data, setData] = useState<ContractsListResponse | null>(null);
  const [statistics, setStatistics] = useState<ContractStatisticsResponse | null>(null);
  const [loading, setLoading] = useState(true);
  const [statsLoading, setStatsLoading] = useState(true);

  // 增量下载状态
  const [lastDownloadTime, setLastDownloadTime] = useState<string | null>(() => {
    if (typeof window !== 'undefined') {
      return localStorage.getItem('contract_last_download_time');
    }
    return null;
  });

  // 加载合同列表
  const loadContracts = useCallback(async () => {
    setLoading(true);
    try {
      const result = await contractService.getContracts({
        ...filters,
        provinceCode: filters.provinceCode || undefined,
        cityCode: filters.cityCode || undefined,
        status: filters.status || undefined,
        signedAfter: filters.signedAfter || undefined,
        signedBefore: filters.signedBefore || undefined,
      });
      setData(result);
    } catch (error) {
      console.error('获取合同列表失败:', error);
      toast.error('获取合同列表失败');
    } finally {
      setLoading(false);
    }
  }, [filters]);

  // 加载统计信息
  const loadStatistics = useCallback(async () => {
    setStatsLoading(true);
    try {
      const result = await contractService.getStatistics({
        provinceCode: filters.provinceCode || undefined,
        cityCode: filters.cityCode || undefined,
      });
      setStatistics(result);
    } catch (error) {
      console.error('获取统计信息失败:', error);
    } finally {
      setStatsLoading(false);
    }
  }, [filters.provinceCode, filters.cityCode]);

  useEffect(() => {
    loadContracts();
  }, [loadContracts]);

  useEffect(() => {
    loadStatistics();
  }, [loadStatistics]);

  // 格式化日期
  const formatDate = (dateStr: string | null) => {
    if (!dateStr) return '-';
    return new Date(dateStr).toLocaleString('zh-CN');
  };

  // 格式化金额
  const formatAmount = (amount: number) => {
    return amount.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
  };

  // 下载单个合同
  const handleDownloadContract = (orderNo: string) => {
    const downloadUrl = contractService.getDownloadUrl(orderNo);
    window.open(downloadUrl, '_blank');
  };

  // 批量下载状态
  const [batchDownloading, setBatchDownloading] = useState(false);
  const [downloadProgress, setDownloadProgress] = useState(0);

  // 轮询任务状态直到完成
  const pollTaskUntilComplete = async (taskNo: string): Promise<string | null> => {
    const maxAttempts = 120; // 最多轮询2分钟
    for (let i = 0; i < maxAttempts; i++) {
      try {
        const status = await contractService.getBatchDownloadStatus(taskNo);
        // 打包进度（未使用）

        if (status.status === 'COMPLETED' && status.resultFileUrl) {
          return status.resultFileUrl;
        } else if (status.status === 'FAILED') {
          throw new Error('任务处理失败');
        }

        // 等待1秒后继续轮询
        await new Promise(resolve => setTimeout(resolve, 1000));
      } catch (error) {
        console.error('查询任务状态失败:', error);
        throw error;
      }
    }
    throw new Error('任务处理超时');
  };

  // 下载进度详情
  const [downloadedCount, setDownloadedCount] = useState(0);
  const [totalCount, setTotalCount] = useState(0);

  // 批量下载（先选择保存目录，再逐个下载合同）
  const handleBatchDownload = async () => {
    // 检查浏览器是否支持选择目录
    if (!('showDirectoryPicker' in window)) {
      toast.error('当前浏览器不支持选择保存目录，请使用 Chrome 或 Edge 浏览器');
      return;
    }

    // 第一步：让用户选择保存目录
    let dirHandle: FileSystemDirectoryHandle;
    try {
      dirHandle = await (window as unknown as { showDirectoryPicker: () => Promise<FileSystemDirectoryHandle> }).showDirectoryPicker();
    } catch (error: unknown) {
      const err = error as Error;
      if (err.name === 'AbortError') {
        toast.info('已取消');
      } else {
        toast.error('选择保存目录失败');
      }
      return;
    }

    // 第二步：获取已签署的合同列表
    setBatchDownloading(true);
    setDownloadProgress(0);
    setDownloadedCount(0);
    setTotalCount(0);

    try {
      toast.info('正在获取合同列表...');

      // 获取所有已签署的合同
      const result = await contractService.getContracts({
        signedAfter: filters.signedAfter || undefined,
        signedBefore: filters.signedBefore || undefined,
        provinceCode: filters.provinceCode || undefined,
        cityCode: filters.cityCode || undefined,
        status: 'SIGNED',
        page: 1,
        pageSize: 10000, // 获取所有
      });

      const contracts = result.items.filter(c => c.signedPdfUrl);
      const total = contracts.length;
      setTotalCount(total);

      if (total === 0) {
        toast.warning('没有可下载的已签署合同');
        return;
      }

      toast.info(`开始下载 ${total} 份合同...`);

      // 第三步：逐个下载合同到用户选择的目录（支持断点续传）
      let downloaded = 0;
      let skipped = 0;
      for (const contract of contracts) {
        try {
          // 生成文件名
          const fileName = `${contract.contractNo}_${contract.userRealName || '未实名'}_${contract.treeCount}棵.pdf`;

          // 检查文件是否已存在（断点续传）
          try {
            await dirHandle.getFileHandle(fileName, { create: false });
            // 文件已存在，跳过
            skipped++;
            downloaded++;
            setDownloadedCount(downloaded);
            setDownloadProgress(Math.round((downloaded / total) * 100));
            continue;
          } catch {
            // 文件不存在，继续下载
          }

          // 下载 PDF
          const downloadUrl = contractService.getDownloadUrl(contract.orderNo);
          const response = await fetch(downloadUrl);
          if (!response.ok) {
            console.error(`下载失败: ${contract.orderNo}`);
            continue;
          }

          const blob = await response.blob();

          // 保存到用户选择的目录
          const fileHandle = await dirHandle.getFileHandle(fileName, { create: true });
          const writable = await fileHandle.createWritable();
          await writable.write(blob);
          await writable.close();

          downloaded++;
          setDownloadedCount(downloaded);
          setDownloadProgress(Math.round((downloaded / total) * 100));
        } catch (err) {
          console.error(`下载合同失败: ${contract.orderNo}`, err);
        }
      }

      if (skipped > 0) {
        toast.success(`下载完成！新下载 ${downloaded - skipped} 份，跳过已存在 ${skipped} 份`);
      } else {
        toast.success(`下载完成！成功 ${downloaded}/${total} 份`);
      }

      // 保存当前时间作为最后下载时间
      const now = new Date().toISOString();
      localStorage.setItem('contract_last_download_time', now);
      setLastDownloadTime(now);
    } catch (error) {
      console.error('批量下载失败:', error);
      toast.error('批量下载失败，请重试');
    } finally {
      setBatchDownloading(false);
                  setDownloadProgress(0);
    }
  };

  // 增量下载（只下载上次之后签署的合同）
  const handleIncrementalDownload = async () => {
    if (!lastDownloadTime) {
      toast.error('未找到上次下载记录，请先执行全量下载');
      return;
    }

    // 检查浏览器是否支持选择目录
    if (!('showDirectoryPicker' in window)) {
      toast.error('当前浏览器不支持选择保存目录，请使用 Chrome 或 Edge 浏览器');
      return;
    }

    // 第一步：让用户选择保存目录
    let dirHandle: FileSystemDirectoryHandle;
    try {
      dirHandle = await (window as unknown as { showDirectoryPicker: () => Promise<FileSystemDirectoryHandle> }).showDirectoryPicker();
    } catch (error: unknown) {
      const err = error as Error;
      if (err.name === 'AbortError') {
        toast.info('已取消');
      } else {
        toast.error('选择保存目录失败');
      }
      return;
    }

    // 第二步：获取增量合同列表
    setBatchDownloading(true);
            setDownloadProgress(0);
    setDownloadedCount(0);
    setTotalCount(0);

    try {
      toast.info('正在获取新增合同列表...');

      // 获取上次下载后新签署的合同
      const result = await contractService.getContracts({
        signedAfter: lastDownloadTime,
        provinceCode: filters.provinceCode || undefined,
        cityCode: filters.cityCode || undefined,
        status: 'SIGNED',
        page: 1,
        pageSize: 10000,
      });

      const contracts = result.items.filter(c => c.signedPdfUrl);
      const total = contracts.length;
      setTotalCount(total);

      if (total === 0) {
        toast.info('没有新增的合同需要下载');
        return;
      }

      toast.info(`开始下载 ${total} 份新增合同...`);

      // 第三步：逐个下载合同（支持断点续传）
      let downloaded = 0;
      let skipped = 0;
      for (const contract of contracts) {
        try {
          const fileName = `${contract.contractNo}_${contract.userRealName || '未实名'}_${contract.treeCount}棵.pdf`;

          // 检查文件是否已存在（断点续传）
          try {
            await dirHandle.getFileHandle(fileName, { create: false });
            skipped++;
            downloaded++;
            setDownloadedCount(downloaded);
            setDownloadProgress(Math.round((downloaded / total) * 100));
            continue;
          } catch {
            // 文件不存在，继续下载
          }

          const downloadUrl = contractService.getDownloadUrl(contract.orderNo);
          const response = await fetch(downloadUrl);
          if (!response.ok) continue;

          const blob = await response.blob();
          const fileHandle = await dirHandle.getFileHandle(fileName, { create: true });
          const writable = await fileHandle.createWritable();
          await writable.write(blob);
          await writable.close();

          downloaded++;
          setDownloadedCount(downloaded);
          setDownloadProgress(Math.round((downloaded / total) * 100));
        } catch (err) {
          console.error(`下载合同失败: ${contract.orderNo}`, err);
        }
      }

      if (skipped > 0) {
        toast.success(`下载完成！新下载 ${downloaded - skipped} 份，跳过已存在 ${skipped} 份`);
      } else {
        toast.success(`下载完成！成功 ${downloaded}/${total} 份`);
      }

      // 更新最后下载时间
      const now = new Date().toISOString();
      localStorage.setItem('contract_last_download_time', now);
      setLastDownloadTime(now);
    } catch (error) {
      console.error('增量下载失败:', error);
      toast.error('增量下载失败，请重试');
    } finally {
      setBatchDownloading(false);
                  setDownloadProgress(0);
    }
  };

  // 重置筛选
  const handleResetFilters = () => {
    setFilters({
      page: 1,
      pageSize: 20,
      provinceCode: '',
      cityCode: '',
      status: '',
      signedAfter: '',
      signedBefore: '',
      orderBy: 'signedAt',
      orderDir: 'desc',
    });
  };

  // 翻页
  const handlePageChange = (newPage: number) => {
    setFilters(prev => ({ ...prev, page: newPage }));
  };

  return (
    <PageContainer title="合同管理">
      <div className={styles.contracts}>
        {/* 统计卡片 */}
        <div className={styles.contracts__statsGrid}>
          <div className={styles.contracts__statCard}>
            <span className={styles.contracts__statLabel}>合同总数</span>
            <span className={styles.contracts__statValue}>
              {statsLoading ? '...' : formatNumber(statistics?.totalContracts || 0)}
            </span>
          </div>
          <div className={styles.contracts__statCard}>
            <span className={styles.contracts__statLabel}>已签署</span>
            <span className={cn(styles.contracts__statValue, styles.contracts__statusGreen)}>
              {statsLoading ? '...' : formatNumber(statistics?.signedContracts || 0)}
            </span>
          </div>
          <div className={styles.contracts__statCard}>
            <span className={styles.contracts__statLabel}>待签署</span>
            <span className={cn(styles.contracts__statValue, styles.contracts__statusYellow)}>
              {statsLoading ? '...' : formatNumber(statistics?.pendingContracts || 0)}
            </span>
          </div>
          <div className={styles.contracts__statCard}>
            <span className={styles.contracts__statLabel}>已超时</span>
            <span className={cn(styles.contracts__statValue, styles.contracts__statusRed)}>
              {statsLoading ? '...' : formatNumber(statistics?.expiredContracts || 0)}
            </span>
          </div>
        </div>

        {/* 筛选栏 */}
        <div className={styles.contracts__filters}>
          <div className={styles.contracts__filterRow}>
            <div className={styles.contracts__filterItem}>
              <label>省份:</label>
              <select
                value={filters.provinceCode}
                onChange={(e) => setFilters(prev => ({ ...prev, provinceCode: e.target.value, cityCode: '', page: 1 }))}
              >
                <option value="">全部省份</option>
                {PROVINCE_OPTIONS.map(opt => (
                  <option key={opt.value} value={opt.value}>{opt.label}</option>
                ))}
              </select>
            </div>

            <div className={styles.contracts__filterItem}>
              <label>状态:</label>
              <select
                value={filters.status}
                onChange={(e) => setFilters(prev => ({ ...prev, status: e.target.value, page: 1 }))}
              >
                <option value="">全部状态</option>
                <option value="SIGNED">已签署</option>
                <option value="PENDING">待签署</option>
                <option value="EXPIRED">已超时</option>
              </select>
            </div>

            <div className={styles.contracts__filterItem}>
              <label>签署时间起:</label>
              <input
                type="date"
                value={filters.signedAfter?.split('T')[0] || ''}
                onChange={(e) => setFilters(prev => ({ ...prev, signedAfter: e.target.value ? `${e.target.value}T00:00:00.000Z` : '', page: 1 }))}
              />
            </div>

            <div className={styles.contracts__filterItem}>
              <label>签署时间止:</label>
              <input
                type="date"
                value={filters.signedBefore?.split('T')[0] || ''}
                onChange={(e) => setFilters(prev => ({ ...prev, signedBefore: e.target.value ? `${e.target.value}T23:59:59.999Z` : '', page: 1 }))}
              />
            </div>

            <Button variant="outline" size="sm" onClick={handleResetFilters}>
              重置筛选
            </Button>
          </div>

          <div className={styles.contracts__actions}>
            <Button variant="primary" onClick={handleBatchDownload} disabled={batchDownloading}>
              {batchDownloading
                ? `下载中 ${downloadedCount}/${totalCount}`
                : '批量下载'}
            </Button>
            {lastDownloadTime && (
              <Button variant="outline" onClick={handleIncrementalDownload} disabled={batchDownloading}>
                增量下载（上次: {new Date(lastDownloadTime).toLocaleString('zh-CN')}）
              </Button>
            )}
          </div>
        </div>

        {/* 下载进度条 */}
        {batchDownloading && (
          <div className={styles.contracts__progressContainer}>
            <div className={styles.contracts__progressLabel}>
              正在下载合同文件... ({downloadedCount}/{totalCount})
            </div>
            <div className={styles.contracts__progressBar}>
              <div
                className={styles.contracts__progressFill}
                style={{ width: `${downloadProgress}%` }}
              />
            </div>
            <div className={styles.contracts__progressText}>
              {downloadProgress}%
            </div>
          </div>
        )}

        {/* 合同列表 */}
        <div className={styles.contracts__table}>
          <div className={styles.contracts__tableHeader}>
            <div className={styles.contracts__tableCell}>合同编号</div>
            <div className={styles.contracts__tableCell}>订单号</div>
            <div className={styles.contracts__tableCell}>用户</div>
            <div className={styles.contracts__tableCell}>认种数量</div>
            <div className={styles.contracts__tableCell}>金额</div>
            <div className={styles.contracts__tableCell}>省市</div>
            <div className={styles.contracts__tableCell}>状态</div>
            <div className={styles.contracts__tableCell}>签署时间</div>
            <div className={styles.contracts__tableCell}>操作</div>
          </div>

          {loading ? (
            <div className={styles.contracts__loading}>加载中...</div>
          ) : data && data.items.length > 0 ? (
            data.items.map((contract) => (
              <div key={contract.orderNo} className={styles.contracts__tableRow}>
                <div className={styles.contracts__tableCell}>{contract.contractNo}</div>
                <div className={styles.contracts__tableCell}>{contract.orderNo}</div>
                <div className={styles.contracts__tableCell}>
                  <div>{contract.userRealName || '未实名'}</div>
                  <div className={styles.contracts__tableSubtext}>{contract.accountSequence}</div>
                </div>
                <div className={styles.contracts__tableCell}>{formatNumber(contract.treeCount)}</div>
                <div className={styles.contracts__tableCell}>{formatAmount(contract.totalAmount)}</div>
                <div className={styles.contracts__tableCell}>
                  {contract.provinceName} / {contract.cityName}
                </div>
                <div className={cn(styles.contracts__tableCell, getStatusStyleClass(contract.status))}>
                  {CONTRACT_STATUS_LABELS[contract.status] || contract.status}
                </div>
                <div className={styles.contracts__tableCell}>
                  {contract.signedAt ? formatDate(contract.signedAt) : '-'}
                </div>
                <div className={styles.contracts__tableCell}>
                  {contract.status === 'SIGNED' && contract.signedPdfUrl && (
                    <Button
                      variant="outline"
                      size="sm"
                      onClick={() => handleDownloadContract(contract.orderNo)}
                    >
                      下载
                    </Button>
                  )}
                </div>
              </div>
            ))
          ) : (
            <div className={styles.contracts__empty}>暂无合同数据</div>
          )}
        </div>

        {/* 分页 */}
        {data && data.totalPages > 1 && (
          <div className={styles.contracts__pagination}>
            <Button
              variant="outline"
              size="sm"
              disabled={filters.page === 1}
              onClick={() => handlePageChange(filters.page! - 1)}
            >
              上一页
            </Button>
            <span className={styles.contracts__pageInfo}>
              第 {filters.page} / {data.totalPages} 页，共 {data.total} 条
            </span>
            <Button
              variant="outline"
              size="sm"
              disabled={filters.page === data.totalPages}
              onClick={() => handlePageChange(filters.page! + 1)}
            >
              下一页
            </Button>
          </div>
        )}
      </div>
    </PageContainer>
  );
}
