import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/product_local_datasource.dart';
import 'package:enaklo_pos/presentation/home/models/order_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'day_sales_event.dart';
part 'day_sales_state.dart';
part 'day_sales_bloc.freezed.dart';

class DaySalesBloc extends Bloc<DaySalesEvent, DaySalesState> {
  final ProductLocalDatasource datasource;
  DaySalesBloc(this.datasource) : super(const _Initial()) {
    on<_GetDaySales>((event, emit) async {
      emit(const _Loading());
      final result = await datasource.getAllOrder(event.date);
      emit(_Loaded(result));
    });
    on<_GetRangeDateSales>((event, emit) async {
      emit(const _Loading());
      final result =
          await datasource.getAllOrderByRange(event.startDate, event.endDate);
      emit(_Loaded(result));
    });
  }
}
