import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mitra_corporate/model/surat_jalan_model.dart';
import 'package:mitra_corporate/repository/surat_jalan_repository.dart';

import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

abstract class GetSuratJalanListState extends Equatable {
  final DateTime date;
  const GetSuratJalanListState(this.date);
  @override
  List<Object?> get props => [date];
}

class GetSuratJalanListStateInit extends GetSuratJalanListState {
  GetSuratJalanListStateInit() : super(DateTime.now());
}

class GetSuratJalanListStateLoading extends GetSuratJalanListState {
  GetSuratJalanListStateLoading() : super(DateTime.now());
}

class GetSuratJalanListStateError extends GetSuratJalanListState {
  final String message;
  GetSuratJalanListStateError({
    required this.message,
  }) : super(DateTime.now());
}

class GetSuratJalanListStateDone extends GetSuratJalanListState {
  final SearchSuratJalanModel model;
  GetSuratJalanListStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class GetSuratJalanListNotifier extends StateNotifier<GetSuratJalanListState> {
  final Ref ref;
  GetSuratJalanListNotifier({
    required this.ref,
  }) : super(GetSuratJalanListStateInit());

  void getData({
    required String token,
    required String company_id,
    required String rpp,
    required String keyword,
    required String page,
    required String sd,
    required String ed,
    required String dateType,
  }) async {
    try {
      state = GetSuratJalanListStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await SuratJalanRepository(dio: dio).getSuratJalan(
          token: token,
          company_id: company_id,
          endDate: ed,
          startDate: sd,
          rpp: rpp,
          keyword: keyword,
          dateType: dateType,
          page: page);
      state = GetSuratJalanListStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = GetSuratJalanListStateError(message: e.message.toString());
      } else {
        state = GetSuratJalanListStateError(message: e.toString());
      }
    }
  }
}

//provider
final GetSuratJalanListProvider =
    StateNotifierProvider<GetSuratJalanListNotifier, GetSuratJalanListState>(
        (ref) => GetSuratJalanListNotifier(ref: ref));
