// lib/app/modules/home/controllers/home_controller.dart
import 'package:get/get.dart';
import 'package:flutter/material.dart';

class InfusMonitoring {
  final String id;
  final String patientName;
  final String deviceId;
  final String room;
  final String dropsPerMinute;
  final DateTime lastUpdate;
  final DateTime createdAt; // Tambahkan properti createdAt

  InfusMonitoring({
    required this.id,
    required this.patientName,
    required this.deviceId,
    required this.room,
    required this.dropsPerMinute,
    required this.lastUpdate,
    required this.createdAt, // Pastikan ini diisi saat pembuatan objek
  });
}

class HomeController extends GetxController {
  final searchController = TextEditingController();

  // Banner carousel
  final currentCarouselIndex = 0.obs;
  final bannerImages = [
    'assets/images/banner-slider1.png',
    'assets/images/banner-slider2.png',
  ];

  final monitoringList = <InfusMonitoring>[
    InfusMonitoring(
      id: '1',
      patientName: 'Mrs. Shinta',
      deviceId: 'SI001',
      room: 'MAWAR 002',
      dropsPerMinute: '30 tetes per menit',
      lastUpdate: DateTime.now().subtract(const Duration(minutes: 5)),
      createdAt: DateTime.now(), // Menambahkan createdAt
    ),
    InfusMonitoring(
      id: '2',
      patientName: 'Mr. Suparjo',
      deviceId: 'SI001',
      room: 'MELATI 001',
      dropsPerMinute: '30 tetes per menit',
      lastUpdate: DateTime.now().subtract(const Duration(minutes: 5)),
      createdAt: DateTime.now(), // Menambahkan createdAt
    ),
  ].obs;

  @override
  void onClose() {
    searchController.dispose();
    super.onClose();
  }

  void updateCarouselIndex(int index) {
    currentCarouselIndex.value = index;
  }

  void showLogoutDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15),
          ),
          title: const Text(
            'Konfirmasi Logout',
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          content: const Text('Apakah Anda yakin ingin keluar?'),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: Text('Batal', style: TextStyle(color: Colors.grey[600])),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop();
                // Navigasi ke halaman login
                Get.offAllNamed('/login');
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red,
                foregroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              child: const Text('Logout'),
            ),
          ],
        );
      },
    );
  }

  String getLastUpdateText(DateTime lastUpdate) {
    final difference = DateTime.now().difference(lastUpdate);
    if (difference.inMinutes < 60) {
      return '${difference.inMinutes}min ago';
    } else if (difference.inHours < 24) {
      return '${difference.inHours}h ago';
    } else {
      return '${difference.inDays}d ago';
    }
  }
}
