import 'package:cheminova/widgets/my_drawer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../controller/annaucement_controller.dart';
import '../../widgets/comman_background.dart';
import '../../widgets/common_appbar.dart';

class AnnouncementScreen extends StatefulWidget {
  AnnouncementScreen({super.key});

  @override
  State<AnnouncementScreen> createState() => _AnnouncementScreenState();
}

class _AnnouncementScreenState extends State<AnnouncementScreen> {
  // Initialize the controller
  final AnnouncementController _announcementController = Get.put(AnnouncementController());


  String formatDate(String apiDate) {
    // Parse the API date string into a DateTime object
    DateTime parsedDate = DateTime.parse(apiDate).toLocal(); // Convert to local time

    // Format the date and time according to your specified format
    String formattedDate = DateFormat('EEE MMM dd yyyy').format(parsedDate);

    return formattedDate; // Return the formatted date string
  }


  @override
  Widget build(BuildContext context) {
    // Fetch announcements when the screen is built
    _announcementController.fetchAnnouncements();

    return CommonBackground(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: CommonAppBar(
          actions: [
            IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: SvgPicture.asset('assets/svg/back_arrow.svg'),
              padding: const EdgeInsets.only(right: 20),
            ),
          ],
          title: const Text(
            'Announcement',
            style: TextStyle(
              fontSize: 20,
              color: Colors.black,
              fontWeight: FontWeight.w400,
              fontFamily: 'Anek',
            ),
          ),
          backgroundColor: Colors.transparent,
          elevation: 0,
        ),
        drawer: MyDrawer(),
        body: Obx(() {
          // Show loading indicator while fetching announcements
          if (_announcementController.isLoading.value) {
            return Center(child: CircularProgressIndicator());
          }
          // Show error message if there was an error
          if (_announcementController.errorMessage.isNotEmpty) {
            return Center(
              child: Text('Error: ${_announcementController.errorMessage}'),
            );
          }
          // Display the list of announcements
          return ListView.builder(
            itemCount: _announcementController.announcements.length,
            itemBuilder: (context, index) {
            final announcementList =   _announcementController.announcements[index];
            print("asdf,${announcementList}");
              return Column(
                children: [
                  Card(
                    child: ListTile(
                      //leading:Text(_announcementController.announcements[index].id),
                      title: Row(

                        children: [
                          Text("Message :",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 14),),
                          Text(announcementList.message.toString(),style: TextStyle(fontSize: 12),),

                        ],
                      ),
                      subtitle: Row(
                        children: [
                          Text("UniqueID :" , style: TextStyle(fontWeight: FontWeight.bold),),
                          Text(announcementList.uniqueId.toString()),
                        ],
                      ),
                      trailing: Text(formatDate(announcementList.createdAt.toString()),style: TextStyle(fontSize: 10),),
                    ),

                  ),

                ],
              );
            },
          );
        }),
      ),
    );
  }
}
