import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/network/api_client.dart';
import '../../app.dart';
import '../../shared/models/pagination_model.dart';
import 'expense_apply_model.dart';

final expenseApplyApiProvider = Provider<ExpenseApplyApi>(
  (ref) => ExpenseApplyApi(ref.read(apiClientProvider)),
);

class ExpenseApplyApi {
  final ApiClient _client;
  ExpenseApplyApi(this._client);

  /// 费用申请列表（分页）
  Future<PaginatedData<ExpenseApplyModel>> fetchList({
    String status = '',
    String keyword = '',
    String startDate = '',
    String endDate = '',
    String usr = '',
    int page = 1,
    int size = 20,
  }) async {
    final response = await _client.get<Map<String, dynamic>>(
      '/OA/GetExpenseApplies',
      queryParameters: {
        'status': status,
        'keyword': keyword,
        'startDate': startDate,
        'endDate': endDate,
        'usr': usr,
        'page': page,
        'size': size,
      },
    );
    return PaginatedData.fromJson(response.data!, ExpenseApplyModel.fromJson);
  }

  /// 费用申请详情（主表+明细）
  Future<ExpenseApplyModel> fetchDetail(String billNo) async {
    final response = await _client.get<Map<String, dynamic>>(
      '/OA/GetExpenseApplyDetail',
      queryParameters: {'billNo': billNo},
    );
    return ExpenseApplyModel.fromJson(response.data!);
  }

  /// 提交审批
  Future<void> submit(Map<String, dynamic> data) async {
    await _client.post('/OA/BillSave', data: {
      'erpCategory': 'MasterService',
      'billId': 'AE',
      'procId': '',
      'data': data,
    });
  }
}
