import 'package:cheminova/models/pd_rd_response_model.dart';
import 'package:cheminova/models/task_model.dart';
import 'package:cheminova/screens/data_submit_successfull.dart';
import 'package:cheminova/services/api_client.dart';
import 'package:cheminova/services/api_urls.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class TaskProvider extends ChangeNotifier {
  bool _isLoading = false;
  PdRdResponseModel? _selectedSalesCoordinator;
  String? _selectedTask;
  String? _selectedPriority;
  String _selectedDate = DateFormat('dd/MM/yyyy').format(DateTime.now());
  List<PdRdResponseModel> _salesCoordinators = [];
  List<PdRdResponseModel> _pdList = [];
  List<PdRdResponseModel> _rdList = [];
  List<TaskModel> _taskModelList = [];
  PdRdResponseModel? _selectedDistributor;
  final TextEditingController _noteController = TextEditingController();
  final _apiClient = ApiClient();

  bool get isLoading => _isLoading;
  PdRdResponseModel? get selectedSalesCoordinator => _selectedSalesCoordinator;
  String? get selectedTask => _selectedTask;
  String? get selectedPriority => _selectedPriority;
  String get selectedDate => _selectedDate;
  List<PdRdResponseModel> get salesCoordinators => _salesCoordinators;
  TextEditingController get noteController => _noteController;
  List<PdRdResponseModel> get pdList => _pdList;
  List<PdRdResponseModel> get rdList => _rdList;
  List<TaskModel> get taskModelList => _taskModelList;
  PdRdResponseModel? get selectedDistributor => _selectedDistributor;

  void setLoading(bool loading) {
    _isLoading = loading;
    notifyListeners();
  }


  Future<void> getAllTaskByDate(DateTime date) async {
    setLoading(true);
    try {
      final String formatedDate = DateFormat('dd/MM/yyyy').format(date);
      Response response =
      await _apiClient.get("${ApiUrls.allTaskByDate}?Date=$formatedDate");
      if (response.statusCode == 200) {
        List<TaskModel> data = (response.data['tasks'] as List)
            .map((json) => TaskModel.fromJson(json))
            .toList();
        _taskModelList = data;
      }
    } catch (e) {
      print("Error occurred: $e");
    } finally {
      setLoading(false);
    }
  }
}
