import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'expense_model.dart';

final expenseStatusFilterProvider = StateProvider<String>((ref) => '');
final expensePageProvider = StateProvider<int>((ref) => 1);
final expenseDateStartProvider = StateProvider<DateTime?>((ref) => null);
final expenseDateEndProvider = StateProvider<DateTime?>((ref) => null);
final expenseKeywordProvider = StateProvider<String>((ref) => '');
final expenseRefreshProvider = StateProvider<int>((ref) => 0);
final expenseSortDirProvider = StateProvider<String>((ref) => 'DESC');

final now = DateTime.now();
final mockExpenses = <ExpenseModel>[
  ExpenseModel(
    id: 'EXP001', expenseNo: 'BX202606001', expenseDate: DateTime(2026, 6, 29),
    applicantId: 'U001', applicantName: '张三', deptId: 'D001', deptName: '销售部',
    currencyCode: 'CNY', totalAmount: 2380.0, approvedAmount: 2380.0,
    purpose: '上海出差拜访客户', paymentMethod: 'bankTransfer',
    paymentStatus: 'unpaid', status: 'pending', version: 1,
    isInvoiceVerified: false, isTaxIdMatched: false, isCategoryCompliant: false,
    createTime: DateTime(2026, 6, 29, 10, 0), updateTime: DateTime(2026, 6, 29, 10, 0),
    details: [
      ExpenseDetailModel(id: 'ED001', expenseId: 'EXP001', expenseApplyNo: 'BXSQ-20240501-001',
          expenseCategory: 'transport', categoryName: '交通费', priority: '2', purpose: '往返机票',
          projectId: 'P001', projectName: '华东销售项目', costDeptId: 'D001', costDeptName: '销售部',
          acctSubjectId: '550201', acctSubjectName: '差旅费-交通',
          amount: 1200, taxRate: 0.09, taxAmount: 108, totalAmount: 1308, currencyCode: 'CNY',
          exchangeRate: 1.0, baseAmount: 1308, approvedAmount: 1308,
          bankName: '招商银行', bankAccountName: '张三', bankAccount: '6222 0212 3456 7890',
          remark: '经济舱', createTime: now, updateTime: now),
      ExpenseDetailModel(id: 'ED002', expenseId: 'EXP001',
          expenseCategory: 'hotel', categoryName: '住宿费', priority: '2', purpose: '酒店住宿',
          projectId: 'P001', projectName: '华东销售项目', costDeptId: 'D001', costDeptName: '销售部',
          acctSubjectId: '550202', acctSubjectName: '差旅费-住宿',
          amount: 984, taxRate: 0.09, taxAmount: 88, totalAmount: 1072, currencyCode: 'CNY',
          exchangeRate: 1.0, baseAmount: 1072, approvedAmount: 1072,
          bankName: '招商银行', bankAccountName: '张三', bankAccount: '6222 0212 3456 7890',
          remark: '3晚', createTime: now, updateTime: now),
    ],
    attachments: ['发票_机票.pdf', '发票_酒店.pdf'],
    currentApproverId: 'M001',
  ),
  ExpenseModel(
    id: 'EXP002', expenseNo: 'BX202606002', expenseDate: DateTime(2026, 6, 25),
    applicantId: 'U001', applicantName: '张三', deptId: 'D001', deptName: '销售部',
    currencyCode: 'CNY', totalAmount: 156.0, approvedAmount: 156.0,
    purpose: '办公用品报销', paymentMethod: 'alipay',
    paymentStatus: 'paid', status: 'approved', version: 1,
    isInvoiceVerified: true, isTaxIdMatched: true, isCategoryCompliant: true,
    voucherNo: 'JZ-202606-025', bankTransferNo: '20260628001',
    createTime: DateTime(2026, 6, 25, 14, 30), updateTime: DateTime(2026, 6, 28, 9, 0),
    details: [
      ExpenseDetailModel(id: 'ED003', expenseId: 'EXP002',
          expenseCategory: 'office', categoryName: '办公费', priority: '1', purpose: 'A4打印纸+墨盒',
          costDeptId: 'D001', costDeptName: '销售部',
          acctSubjectId: '550601', acctSubjectName: '办公费',
          amount: 156, taxRate: 0.13, taxAmount: 20, totalAmount: 176, currencyCode: 'CNY',
          exchangeRate: 1.0, baseAmount: 176, approvedAmount: 156,
          bankName: '支付宝', bankAccountName: '张三', bankAccount: 'zhangsan@alipay',
          createTime: now, updateTime: now),
    ],
    attachments: ['发票_办公用品.pdf'],
  ),
  ExpenseModel(
    id: 'EXP003', expenseNo: 'BX202606003', expenseDate: DateTime(2026, 6, 20),
    applicantId: 'U002', applicantName: '李四', deptId: 'D002', deptName: '技术部',
    currencyCode: 'CNY', totalAmount: 890.0, approvedAmount: 0.0,
    purpose: '服务器配件采购', paymentMethod: 'bankTransfer',
    paymentStatus: 'unpaid', status: 'rejected', version: 1,
    isInvoiceVerified: false, isTaxIdMatched: false, isCategoryCompliant: false,
    createTime: DateTime(2026, 6, 20, 9, 0), updateTime: DateTime(2026, 6, 21, 16, 0),
    details: [
      ExpenseDetailModel(id: 'ED004', expenseId: 'EXP003',
          expenseCategory: 'equipment', categoryName: '设备费', priority: '3', purpose: 'SSD硬盘×2',
          projectId: 'P002', projectName: '智能硬件研发', costDeptId: 'D002', costDeptName: '技术部',
          acctSubjectId: '550301', acctSubjectName: '研发费用-设备',
          amount: 890, taxRate: 0.13, taxAmount: 116, totalAmount: 1006, currencyCode: 'CNY',
          exchangeRate: 1.0, baseAmount: 1006, approvedAmount: 0,
          customerVendorName: 'XX电子科技有限公司',
          bankName: '工商银行', bankAccountName: '李四', bankAccount: '6212 3456 7890 1234',
          remark: '已收到货物但审批未通过', createTime: now, updateTime: now),
    ],
    attachments: ['报价单.pdf', '入库单.pdf'],
  ),
  ExpenseModel(
    id: 'EXP004', expenseNo: 'BX202606004', expenseDate: DateTime(2026, 6, 28),
    applicantId: 'U003', applicantName: '王五', deptId: 'D003', deptName: '市场部',
    currencyCode: 'USD', totalAmount: 820.0, approvedAmount: 800.0,
    purpose: '海外广告投放素材采购', paymentMethod: 'bankTransfer',
    paymentStatus: 'unpaid', status: 'approved', version: 1,
    isInvoiceVerified: true, isTaxIdMatched: true, isCategoryCompliant: false,
    createTime: DateTime(2026, 6, 28, 11, 0), updateTime: DateTime(2026, 6, 28, 18, 0),
    details: [
      ExpenseDetailModel(id: 'ED005', expenseId: 'EXP004',
          expenseCategory: 'service', categoryName: '服务费', priority: '2', purpose: '设计外包',
          projectId: 'P003', projectName: '品牌推广', costDeptId: 'D003', costDeptName: '市场部',
          acctSubjectId: '550701', acctSubjectName: '广告费',
          amount: 800, taxRate: 0, taxAmount: 0, totalAmount: 800,
          currencyCode: 'USD', exchangeRate: 7.25, baseAmount: 5800, approvedAmount: 800,
          customerVendorName: 'Global Media Ltd.',
          bankName: '中国银行', bankAccountName: '王五', bankAccount: '6217 8901 2345 6789',
          createTime: now, updateTime: now),
      ExpenseDetailModel(id: 'ED006', expenseId: 'EXP004',
          expenseCategory: 'office', categoryName: '办公费', priority: '1', purpose: '印刷物料',
          projectId: 'P003', projectName: '品牌推广', costDeptId: 'D003', costDeptName: '市场部',
          acctSubjectId: '550701', acctSubjectName: '广告费',
          amount: 20, taxRate: 0, taxAmount: 0, totalAmount: 20,
          currencyCode: 'USD', exchangeRate: 7.25, baseAmount: 145, approvedAmount: 0,
          bankName: '中国银行', bankAccountName: '王五', bankAccount: '6217 8901 2345 6789',
          createTime: now, updateTime: now),
    ],
    attachments: ['合同_设计外包.pdf', 'invoice.pdf'],
  ),
];

final expenseListProvider = FutureProvider.autoDispose
    .family<List<ExpenseModel>, String>((ref, status) async {
  ref.watch(expensePageProvider);
  ref.watch(expenseRefreshProvider);
  ref.watch(expenseDateStartProvider);
  ref.watch(expenseDateEndProvider);
  await Future.delayed(const Duration(milliseconds: 500));
  if (status.isEmpty) return mockExpenses;
  return mockExpenses.where((e) => e.status == status).toList();
});
