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 expenseApplyScopeProvider = StateProvider<String>((ref) => 'mine');

final mockExpenseApplications = <ExpenseApplicationModel>[
  ExpenseApplicationModel(
    id: 'EA001',
    applicationNo: 'BXSQ-20240501-001',
    applicantId: 'U001',
    applicantName: '张三',
    deptId: 'D001',
    deptName: '销售部',
    expenseType: '差旅费',
    expenseTypes: ['差旅费'],
    estimatedAmount: 3500.00,
    purpose: '上海出差拜访客户',
    remark: '',
    status: 'pending',
    currentApproverId: 'U100',
    approvalChain: ['U100', 'U200'],
    urgency: 'normal',
    estimatedStartDate: DateTime(2024, 5, 5),
    estimatedEndDate: DateTime(2024, 5, 7),
    isTaxIncluded: true,
    projectName: '华东区客户拓展项目',
    budgetSubjectName: '差旅费预算',
    availableBalance: 15000.00,
    usageStatus: 'unused',
    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: '办公用品',
    expenseTypes: ['办公用品'],
    estimatedAmount: 1560.00,
    purpose: '采购开发板及配件',
    remark: '急用',
    status: 'approved',
    currentApproverId: '',
    approvalChain: ['U100', 'U200'],
    urgency: 'urgent',
    projectName: '智能网关研发项目',
    budgetSubjectName: '研发物料预算',
    availableBalance: 5000.00,
    usageStatus: 'partial',
    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: '业务招待费',
    expenseTypes: ['业务招待费'],
    estimatedAmount: 5000.00,
    purpose: '客户答谢晚宴',
    remark: '',
    status: 'rejected',
    currentApproverId: '',
    approvalChain: ['U100', 'U200'],
    urgency: 'normal',
    entertainTarget: '腾达科技',
    entertainLevel: '重要',
    guestCount: 5,
    hostCount: 3,
    entertainLocation: '香格里拉大酒店',
    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),
      ),
    ],
  ),
  ExpenseApplicationModel(
    id: 'EA004',
    applicationNo: 'BXSQ-20240601-004',
    applicantId: 'U001',
    applicantName: '张三',
    deptId: 'D001',
    deptName: '销售部',
    expenseType: '会议费',
    expenseTypes: ['会议费'],
    estimatedAmount: 12000.00,
    purpose: '年度销售总结大会',
    remark: '',
    status: 'draft',
    currentApproverId: '',
    approvalChain: ['U100', 'U200'],
    urgency: 'normal',
    meetingDate: DateTime(2024, 6, 15),
    meetingLocation: '公司3楼会议室',
    projectName: '销售体系优化项目',
    budgetSubjectName: '会议费预算',
    availableBalance: 20000.00,
    createTime: DateTime(2024, 6, 1, 9, 0),
    updateTime: DateTime(2024, 6, 1, 9, 0),
    details: [
      ExpenseAppDetailModel(
        id: 'det-ea004-1',
        itemName: '场地租赁',
        estimatedAmount: 5000.00,
        quantity: 1,
        unit: '天',
        unitPrice: 5000.00,
        lineAmount: 5000.00,
        category: '会议场地费',
        description: '公司3楼会议室全天租赁',
      ),
      ExpenseAppDetailModel(
        id: 'det-ea004-2',
        itemName: '茶歇',
        estimatedAmount: 3000.00,
        quantity: 50,
        unit: '人',
        unitPrice: 60.00,
        lineAmount: 3000.00,
        category: '会议茶歇费',
        description: '上下午茶歇及点心',
      ),
    ],
  ),
  ExpenseApplicationModel(
    id: 'EA005',
    applicationNo: 'BXSQ-20240602-005',
    applicantId: 'U001',
    applicantName: '张三',
    deptId: 'D001',
    deptName: '销售部',
    expenseType: '差旅费/招待费',
    expenseTypes: ['差旅费', '招待费'],
    estimatedAmount: 8000.00,
    purpose: '广州出差并拜访重要客户',
    remark: '',
    status: 'pending',
    currentApproverId: 'U100',
    approvalChain: ['U100', 'U200'],
    urgency: 'urgent',
    estimatedStartDate: DateTime(2024, 6, 10),
    estimatedEndDate: DateTime(2024, 6, 12),
    isOvernight: true,
    transportMode: '飞机',
    entertainTarget: '广汽集团',
    entertainLevel: 'VIP',
    guestCount: 3,
    hostCount: 2,
    entertainLocation: '广州花园酒店',
    projectName: '华南区市场开拓项目',
    budgetSubjectName: '差旅费预算',
    availableBalance: 25000.00,
    validUntil: DateTime(2024, 7, 31),
    createTime: DateTime(2024, 6, 2, 11, 0),
    updateTime: DateTime(2024, 6, 2, 11, 0),
    details: [
      ExpenseAppDetailModel(
        id: 'det-ea005-1',
        itemName: '机票',
        estimatedAmount: 2400.00,
        quantity: 2,
        unit: '张',
        unitPrice: 1200.00,
        lineAmount: 2400.00,
        category: '交通费',
        description: '广州往返机票',
      ),
      ExpenseAppDetailModel(
        id: 'det-ea005-2',
        itemName: '酒店住宿',
        estimatedAmount: 3000.00,
        quantity: 3,
        unit: '间夜',
        unitPrice: 1000.00,
        lineAmount: 3000.00,
        category: '住宿费',
        description: '广州花园酒店2间房3晚',
      ),
      ExpenseAppDetailModel(
        id: 'det-ea005-3',
        itemName: '餐饮接待',
        estimatedAmount: 2600.00,
        quantity: 1,
        unit: '次',
        unitPrice: 2600.00,
        lineAmount: 2600.00,
        category: '餐饮费',
        description: '客户招待晚宴',
      ),
    ],
    attachments: ['http://example.com/file1.pdf', 'http://example.com/file2.jpg'],
  ),
];

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();
    });
