import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../Models/hrmModels/tourExpensesListResponse.dart';
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';

class TourExpensesProvider extends ChangeNotifier {
  tourExpensesListResponse? _response;
  bool _isLoading = false;
  String? _errorMessage;

  tourExpensesListResponse? get response => _response;
  bool get isLoading => _isLoading;
  String? get errorMessage => _errorMessage;

  /// Fetch tour expenses list
  Future<void> fetchTourExpenses(BuildContext context, String pageNumber) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    try {
      final provider = Provider.of<HomescreenNotifier>(context, listen: false);

      final result = await ApiCalling.tourExpensesListAPI(
        provider.empId,
        provider.session,
        pageNumber,
      );

      debugPrint('empId: ${provider.empId}, session: ${provider.session}');

      if (result != null) {
        _response = result;
      } else {
        _errorMessage = "No data found!";
      }
    } catch (e) {
      _errorMessage = "Error: $e";
    }

    _isLoading = false;
    notifyListeners();
  }

  DateTime? _date;
  final TextEditingController dateController = TextEditingController();

  void setDate(DateTime newDate) {
    _date = newDate;
    dateController.text = "${newDate.day}-${newDate.month}-${newDate.year}";
    notifyListeners();
  }

  void showDatePickerDialog(BuildContext context) {
    if (_date == null) {
      setDate(DateTime.now());
    }

    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) => Container(
        height: 250,
        padding: const EdgeInsets.only(top: 6.0),
        margin: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        color: CupertinoColors.systemBackground.resolveFrom(context),
        child: SafeArea(
          top: false,
          child: Column(
            children: [
              SizedBox(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    CupertinoButton(
                      child: const Text("Cancel", style: TextStyle(color: Colors.blue)),
                      onPressed: () => Navigator.pop(context),
                    ),
                    CupertinoButton(
                      child: const Text("Done", style: TextStyle(color: Colors.blue)),
                      onPressed: () {
                        setDate(_date ?? DateTime.now());
                        Navigator.pop(context);
                      },
                    ),
                  ],
                ),
              ),
              Expanded(
                child: CupertinoDatePicker(
                  dateOrder: DatePickerDateOrder.dmy,
                  initialDateTime: _date ?? DateTime.now(),
                  mode: CupertinoDatePickerMode.date,
                  onDateTimeChanged: (DateTime newDate) {
                    _date = newDate;
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
