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

import '../../Models/hrmModels/attendanceRequestListResponse.dart';
import '../../Utils/SharedpreferencesService.dart';
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';


class Attendancelistprovider extends ChangeNotifier {
  attendanceRequestListResponse? _response;
  bool _isLoading = false;
  String? _errorMessage;

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

  /// fetch aattendance request list
  Future<void> fetchAttendanceRequests(context,String type, String from, String to) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

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

      final result = await ApiCalling.attendanceRequestListAPI(provider.empId, provider.session, type, from, to);
      debugPrint('empId: ${provider.empId}, session: ${provider.requestId}');


      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; // temp update
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
