// pages/business/loan-releases/loan-releases.js
const { apiService } = require('../../../services/apiService');

Page({
  data: {
    releases: [],
    loading: false,
    searchKeyword: '',
    filterStatus: 'all',
    statusMap: {
      'pending': '待处理',
      'processing': '处理中',
      'approved': '已通过',
      'rejected': '已拒绝',
      'completed': '已完成',
      'released': '已解押'
    }
  },

  onLoad() {
    this.loadReleases();
  },

  onShow() {
    this.loadReleases();
  },

  // 加载解押申请数据
  async loadReleases() {
    this.setData({ loading: true });
    
    try {
      const params = {
        page: 1,
        limit: 20,
        searchValue: this.data.searchKeyword,
        status: this.data.filterStatus === 'all' ? '' : this.data.filterStatus
      };
      
      const response = await apiService.loanReleases.getList(params);
      
      if (response.success) {
        const releases = response.data.releases.map(release => ({
          ...release,
          statusText: this.data.statusMap[release.status] || release.status,
          applicationTime: this.formatDate(release.applicationTime)
        }));
        
        this.setData({
          releases,
          loading: false
        });
      } else {
        throw new Error(response.message || '获取解押申请列表失败');
      }
    } catch (error) {
      console.error('加载解押申请失败:', error);
      
      // 使用模拟数据作为降级处理
      const mockReleases = [
        {
          id: 1,
          applicationNumber: '20240227145555918',
          customerName: '刘超',
          productName: '中国工商银行扎旗支行"畜禽活体抵押"',
          assetType: '牛',
          releaseQuantity: '10头',
          releaseAmount: 10000,
          status: 'released',
          statusText: '已解押',
          applicationTime: '2024-02-27 06:55',
          applicantPhone: '13800138000',
          processComment: '审核通过，同意解押'
        },
        {
          id: 2,
          applicationNumber: '20240227145555919',
          customerName: '张三',
          productName: '个人住房贷款',
          assetType: '房产',
          releaseQuantity: '1套',
          releaseAmount: 50000,
          status: 'pending',
          statusText: '待处理',
          applicationTime: '2024-02-27 10:30',
          applicantPhone: '13800138001',
          processComment: ''
        }
      ];
      
      this.setData({
        releases: mockReleases,
        loading: false
      });
      
      wx.showToast({
        title: '使用模拟数据',
        icon: 'none'
      });
    }
  },

  // 格式化日期
  formatDate(dateString) {
    if (!dateString) return '';
    const date = new Date(dateString);
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    });
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    this.loadReleases();
  },

  // 筛选状态
  onFilterChange(e) {
    const status = e.detail.value;
    this.setData({
      filterStatus: status
    });
    this.loadReleases();
  },

  // 查看解押申请详情
  viewReleaseDetail(e) {
    const releaseId = e.currentTarget.dataset.releaseId;
    wx.navigateTo({
      url: `/pages/business/loan-releases/detail?releaseId=${releaseId}`
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadReleases().finally(() => {
      wx.stopPullDownRefresh();
    });
  }
});
