import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import '../constants/constant.dart';
import '../screens/data_submit_successfull.dart';
import '../services/api_client.dart';
import '../services/api_urls.dart';

class VisitPdRdProvider with ChangeNotifier {
  final _apiClient = ApiClient();
  bool isLoading = false;

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

  Future<void> submitVisitPdRd(String id,
      {String? type,

      required String retailerName,
      required String visitTime,
      required String visitDate,
      required String note, required String selectedPurpose, required String followUpActions, required String nextVisitDate
      }) async {
    setLoading(true);


    try {
      Response response = await _apiClient.post(ApiUrls.submitVisitUrl, data: {
        "addedFor": type, // Could also be 'PrincipalDistributor'
        "addedForId": id, // Example RetailDistributor/PrincipalDistributor ID
        "tradename": retailerName, // Example trade name
        "visitDate": visitDate,
        "visitTime": visitTime,
        "visitpurpose": selectedPurpose,
        "meetingsummary":note,
        "followup": followUpActions,
        "nextvisitdate":nextVisitDate

      });
      debugPrint('Response: $response');
      setLoading(false);
      if (response.statusCode == 201) {
        Response response =
            await _apiClient.put(ApiUrls.updateTaskInventoryUrl + id);
        debugPrint('Response: $response');
        setLoading(false);
        if (response.statusCode == 200) {
          Navigator.push(
              navigatorKey.currentContext!,
              MaterialPageRoute(
                  builder: (context) => const DataSubmitSuccessFullScreen()));
        }
      }
    } catch (e) {
      setLoading(false);
      debugPrint("Error: $e");
    }
  }
}
