import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../layout/main_layout.dart';

class BidanDrawer extends StatefulWidget {
  const BidanDrawer({super.key});

  @override
  State<BidanDrawer> createState() => _BidanDrawerState();
}

class _BidanDrawerState extends State<BidanDrawer> {
  String? fotoUser;
  String namaUser = "Bidan";

  @override
  void initState() {
    super.initState();
    _loadUserData();
  }

  Future<void> _loadUserData() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      fotoUser = prefs.getString('foto');
      namaUser = prefs.getString('nama') ?? "Bidan Posyandu";
    });
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20),
          bottomRight: Radius.circular(20),
        ),
      ),
      child: Column(
        children: [
          // ================= HEADER =================
          Material(
            color: MainLayout.mainColor,
            borderRadius: const BorderRadius.only(
              topRight: Radius.circular(20),
            ),
            child: InkWell(
              onTap: () {
                Navigator.pushReplacementNamed(context, '/profile-bidan');
              },
              child: Container(
                width: double.infinity,
                padding: const EdgeInsets.symmetric(vertical: 40),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CircleAvatar(
                      radius: 40,
                      backgroundColor: Colors.white,
                      child: CircleAvatar(
                        radius: 37,
                        backgroundColor: Colors.blue.shade100,
                        backgroundImage: (fotoUser != null &&
                                fotoUser!.isNotEmpty)
                            ? NetworkImage(
                                "http://ta.myhost.id/E31230549/mposyandu_api/uploads/$fotoUser")
                            : null,
                        child: (fotoUser == null || fotoUser!.isEmpty)
                            ? const Icon(
                                Icons.person,
                                size: 40,
                                color: MainLayout.mainColor,
                              )
                            : null,
                      ),
                    ),
                    const SizedBox(height: 12),
                    Text(
                      namaUser,
                      style: GoogleFonts.poppins(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    Text(
                      "Lihat Profil",
                      style: GoogleFonts.poppins(
                        color: Colors.white70,
                        fontSize: 12,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),

          // ================= MENU LIST =================
          Expanded(
            child: ListView(
              padding: EdgeInsets.zero,
              children: [
                _drawerItem(context, Icons.home, "Home", '/dashboard-bidan'),

                // ================= DROPDOWN JADWAL (TANPA GARIS) =================
                Theme(
                  data: Theme.of(context)
                      .copyWith(dividerColor: Colors.transparent),
                  child: ExpansionTile(
                    shape: const Border(),
                    collapsedShape: const Border(),
                    leading: const Icon(Icons.calendar_today,
                        color: MainLayout.mainColor),
                    title: Text(
                      "Jadwal",
                      style: GoogleFonts.poppins(
                        color: Colors.black,
                        fontWeight: FontWeight.w500,
                        fontSize: 14,
                      ),
                    ),
                    iconColor: MainLayout.mainColor,
                    collapsedIconColor: MainLayout.mainColor,
                    childrenPadding: const EdgeInsets.only(left: 10),
                    children: [
                      _drawerItem(context, Icons.event_note, "Jadwal Posyandu",
                          '/jadwal-posyandu'),
                      // Link ke halaman Jadwal ANC yang baru dibuat
                      _drawerItem(context, Icons.medical_information,
                          "Jadwal Periksa ANC", '/jadwal-anc'),
                    ],
                  ),
                ),

                _drawerItem(context, Icons.pregnant_woman,
                    "Pemeriksaan Kehamilan", '/periksa-kehamilan'),
                _drawerItem(context, Icons.child_care, "Data Gizi Balita",
                    '/data-gizi-balita'),
                _drawerItem(
                    context, Icons.medical_services, "Imunisasi", '/imunisasi'),
                _drawerItem(context, Icons.menu_book, "Edukasi", '/edukasi'),
                _drawerItem(context, Icons.description, "Laporan", '/laporan'),
              ],
            ),
          ),

          const Divider(),

          // ================= LOGOUT =================
          ListTile(
            leading: const Icon(Icons.logout, color: MainLayout.mainColor),
            title: Text(
              "Logout",
              style: GoogleFonts.poppins(
                color: Colors.black,
                fontWeight: FontWeight.w500,
                fontSize: 14,
              ),
            ),
            onTap: () => _logout(context),
          ),
          const SizedBox(height: 10),
        ],
      ),
    );
  }

  // ================= DRAWER ITEM WIDGET =================
  Widget _drawerItem(
      BuildContext context, IconData icon, String text, String route) {
    return ListTile(
      leading: Icon(icon, color: MainLayout.mainColor),
      title: Text(
        text,
        style: GoogleFonts.poppins(
          color: Colors.black,
          fontWeight: FontWeight.w500,
          fontSize: 14,
        ),
      ),
      onTap: () {
        Navigator.pushReplacementNamed(context, route);
      },
    );
  }

  // ================= LOGOUT FUNCTION =================
  Future<void> _logout(BuildContext context) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.clear();
    if (!context.mounted) return;
    Navigator.pushNamedAndRemoveUntil(context, '/login', (route) => false);
  }
}
