import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../shared/models/approval_status.dart';
import 'expense_application_model.dart';

final expenseApplyStatusFilterProvider = StateProvider<String>((ref) => '');

final expenseAppPageProvider = StateProvider<int>((ref) => 1);

final mockExpenseApplications = <ExpenseApplicationModel>[
  ExpenseApplicationModel(
    id: 'EA001',
    applicationNo: 'BXSQ-20240501-001',
    applicantId: 'U001',
    applicantName: '张三',
    deptId: 'D001',
    deptName: '销售部',
    expenseType: '差旅费',
    estimatedAmount: 3500.00,
    purpose: '上海出差拜访客户',
    remark: '',
    status: 'pending',
    currentApproverId: 'U100',
    approvalChain: ['U100', 'U200'],
    urgency: 'normal',
    estimatedStartDate: DateTime(2024, 5, 5),
    estimatedEndDate: DateTime(2024, 5, 7),
    createTime: DateTime(2024, 5, 1, 14, 30),
    updateTime: DateTime(2024, 5, 1, 14, 30),
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-ea-001',
        bizId: 'EA001',
        bizType: 'expense_application',
        approverId: 'U100',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'pending',
        opinion: '',
        approvalTime: DateTime(2024, 5, 1),
      ),
    ],
  ),
  ExpenseApplicationModel(
    id: 'EA002',
    applicationNo: 'BXSQ-20240428-002',
    applicantId: 'U002',
    applicantName: '李四',
    deptId: 'D002',
    deptName: '技术部',
    expenseType: '办公用品',
    estimatedAmount: 1560.00,
    purpose: '采购开发板及配件',
    remark: '急用',
    status: 'approved',
    currentApproverId: '',
    approvalChain: ['U100', 'U200'],
    urgency: 'urgent',
    createTime: DateTime(2024, 4, 28, 9, 15),
    updateTime: DateTime(2024, 4, 29, 10, 0),
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-ea-002a',
        bizId: 'EA002',
        bizType: 'expense_application',
        approverId: 'U100',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'approve',
        opinion: '同意',
        approvalTime: DateTime(2024, 4, 28),
      ),
      ApprovalRecord(
        id: 'ar-ea-002b',
        bizId: 'EA002',
        bizType: 'expense_application',
        approverId: 'U200',
        approverName: '王总监',
        approvalLevel: 2,
        action: 'approve',
        opinion: '批准',
        approvalTime: DateTime(2024, 4, 29),
      ),
    ],
  ),
  ExpenseApplicationModel(
    id: 'EA003',
    applicationNo: 'BXSQ-20240425-003',
    applicantId: 'U003',
    applicantName: '王五',
    deptId: 'D003',
    deptName: '市场部',
    expenseType: '业务招待费',
    estimatedAmount: 5000.00,
    purpose: '客户答谢晚宴',
    remark: '',
    status: 'rejected',
    currentApproverId: '',
    approvalChain: ['U100', 'U200'],
    urgency: 'normal',
    estimatedStartDate: DateTime(2024, 4, 30),
    estimatedEndDate: DateTime(2024, 4, 30),
    createTime: DateTime(2024, 4, 25, 16, 0),
    updateTime: DateTime(2024, 4, 26, 11, 0),
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-ea-003',
        bizId: 'EA003',
        bizType: 'expense_application',
        approverId: 'U100',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'reject',
        opinion: '预算不足，请重新申请',
        approvalTime: DateTime(2024, 4, 26),
      ),
    ],
  ),
];

final expenseApplicationListProvider =
    FutureProvider.autoDispose<List<ExpenseApplicationModel>>((ref) async {
      ref.watch(expenseApplyStatusFilterProvider);
      ref.watch(expenseAppPageProvider);
      final status = ref.read(expenseApplyStatusFilterProvider);
      if (status.isEmpty) return mockExpenseApplications;
      return mockExpenseApplications.where((e) => e.status == status).toList();
    });
