import 'package:flutter/material.dart';
import 'package:qadirneyriz/services/report/report.dart';
import 'package:qadirneyriz/utils/enums/status.dart';

class ReportState extends ChangeNotifier {
  ReportApi reportApi = ReportApi();
  // set date for filters
  String fromDate = '';
  String toDate = '';

  void setFromDates(String? newFromDate) {
    fromDate = newFromDate ?? '';
    notifyListeners();
  }

  void setToDates(String? newToDate) {
    toDate = newToDate ?? '';
    notifyListeners();
  }

// clear filters
  void clearFilters() {
    selectedLocationId = null;
    selectedManagersId = null;
    selectedStatusId = null;
    selectedSubjectId = null;
    fromDate = '';
    toDate = '';
    notifyListeners();
  }

  // is filter Not empty
  bool hasActiveFilters() {
    return selectedLocationId != null ||
        selectedManagersId != null ||
        selectedStatusId != null ||
        selectedSubjectId != null ||
        fromDate.isNotEmpty ||
        toDate.isNotEmpty;
  }

// get filters location meetings

  int? selectedLocationId;
  void selectLocation(int? locationId) {
    selectedLocationId = locationId;
    notifyListeners();
  }

// get filters subjects meetings

  int? selectedSubjectId;
  void selectSubject(int? subjectId) {
    selectedSubjectId = subjectId;
    notifyListeners();
  }
// get filters meeting managers

  int? selectedManagersId;
  void selectManager(int? managerId) {
    selectedManagersId = managerId;
    notifyListeners();
  }

// all meeting status filters

  int? selectedStatusId;
  void selectStatusMeeting(int? statusId) {
    selectedStatusId = statusId;
    notifyListeners();
  }

// download report

  Status statusDownload = Status.empty;
  String? messageDownload;

  Future<Status> downloadReport(
      {String? fromDate,
      String? toDate,
      int? location,
      int? subject,
      int? meetingManager,
      int? status}) async {
    statusDownload = Status.loading;
    notifyListeners();
    try {
      final result = await reportApi.downloadReport(
          fromDate: fromDate,
          toDate: toDate,
          location: location,
          subject: subject,
          meetingManager: meetingManager,
          status: status);

      if (result == null) {
        statusDownload = Status.error;
      } else {
        if (result.isOk) {
          statusDownload = Status.ready;
          messageDownload = result.message ?? '';
        } else {
          statusDownload = Status.error;
        }
      }
    } catch (e) {
      statusDownload = Status.error;
    }

    notifyListeners();
    return statusDownload;
  }
}
