import 'package:cheminova/models/rejected_applicaton_response.dart';
import 'package:flutter/material.dart';
import 'package:cheminova/widgets/common_background.dart';
import 'package:cheminova/widgets/common_drawer.dart';
import 'package:cheminova/widgets/common_app_bar.dart';
import 'package:cheminova/widgets/common_elevated_button.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import '../provider/rejected_provider.dart';

// Main screen for displaying rejected applications
class RejectedApplicationScreen extends StatefulWidget {
  const RejectedApplicationScreen({super.key});

  @override
  State<RejectedApplicationScreen> createState() =>
      _RejectedApplicationScreenState();
}

class _RejectedApplicationScreenState extends State<RejectedApplicationScreen> {
  late RejectedProvider _rejectedProvider;

  @override
  void initState() {
    _rejectedProvider = RejectedProvider(); // Initialize the provider for rejected applications
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) => _rejectedProvider, // Provide the rejected provider to the widget tree
        child: CommonBackground(
          child: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: CommonAppBar(
              actions: [
                IconButton(
                  onPressed: () {
                    Navigator.pop(context); // Navigate back to the previous screen
                  },
                  icon: Image.asset('assets/Back_attendance.png'),
                  padding: const EdgeInsets.only(right: 20),
                ),
              ],
              title: const Text('Rejected Application',
                  style: TextStyle(
                      fontSize: 20,
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontFamily: 'Anek')), // Title of the app bar
              backgroundColor: Colors.transparent,
              elevation: 0,
            ),
            drawer: const CommonDrawer(), // Navigation drawer
            body: Consumer<RejectedProvider>(
              builder: (context, value, child) => value.isLoading
                  ? const Center(child: CircularProgressIndicator()) // Loading indicator while data is being fetched
                  : MyListView(value: value), // Display the list of rejected applications
            ),
          ),
        ));
  }
}

// Button to display product information
Widget buildProductButton(String productName) {
  return Padding(
    padding: const EdgeInsets.only(bottom: 15),
    child: CommonElevatedButton(
      borderRadius: 30,
      width: double.infinity,
      height: kToolbarHeight - 10,
      text: productName,
      backgroundColor: const Color(0xff004791),
      onPressed: () {
        // Handle product button press
        debugPrint('$productName pressed');
      },
    ),
  );
}

// List view for displaying rejected application details
class MyListView extends StatelessWidget {
  final RejectedProvider value;

  const MyListView({super.key, required this.value});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.only(top: 15),
      itemCount: value.rejectedApplicationList.length, // Number of rejected applications
      itemBuilder: (context, index) {
        RejectedApplicationResponse item = value.rejectedApplicationList[index];
        return Padding(
          padding: const EdgeInsets.only(bottom: 10, left: 10, right: 10),
          child: ExpansionTile(
            collapsedBackgroundColor: Colors.white,
            backgroundColor: Colors.white,
            title: Text(
              item.tradeName ?? '', // Trade name of the application
              style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
            ),
            subtitle: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text((DateFormat("dd/MMMM /yyyy")
                    .format(DateTime.parse(item.createdAt ?? '')))), // Formatted date of application
                Text(item.sId ?? ''), // Unique application ID
                for (var note in item.notes!) Text(note.message ?? ''), // Display application notes
              ],
            ),
            children: <Widget>[
              ListTile(
                title: Text('Address: ${item.address ?? ''}'), // Application address
              ),
              ListTile(
                title: Text('City: ${item.city ?? ''}'), // Application city
              ),
              ListTile(
                title: Text('State: ${item.state ?? ''}'), // Application state
              ),
              ListTile(
                title: Text('Pincode: ${item.pincode ?? ''}'), // Application pincode
              ),
              ListTile(
                title: Text('Mobile: ${item.mobileNumber ?? ''}'), // Mobile number associated with application
              ),
              ListTile(
                title: Text('Status: ${item.status ?? ''}'), // Current status of the application
              ),
              ListTile(
                title: Text(
                    'Principal Distributor: ${item.principalDistributer!.name ?? ''}'), // Principal distributor's name
              ),
              ListTile(
                title: Text('PAN Number: ${item.panNumber ?? ''}'), // PAN number associated with application
              ),
              Image.network(item.panImg!.url ?? '', height: 250, width: 250), // PAN image
              ListTile(
                title: Text('Aadhar Number: ${item.aadharNumber ?? ''}'), // Aadhar number associated with application
              ),
              Image.network(item.aadharImg?.url ?? '', height: 250, width: 250), // Aadhar image
              ListTile(
                title: Text('GST Number: ${item.gstNumber ?? ''}'), // GST number associated with application
              ),
              Image.network(item.gstImg!.url ?? '', height: 250, width: 250), // GST image
              const ListTile(
                title: Text('Pesticide License: '), // Pesticide license information
              ),

              if (item.pesticideLicenseImg != null)
                Image.network(item.pesticideLicenseImg!.url ?? '',
                    height: 250, width: 250), // Pesticide license image
              const ListTile(
                title: Text('selfieEntranceImg: '), // Selfie entrance image information
              ),
              Image.network(item.selfieEntranceImg!.url ?? '',
                  height: 250, width: 250), // Selfie entrance image
              const ListTile(
                title: Text('Notes:'), // Display notes for the application
              ),
              if (item.notes != null)
                for (var note in item.notes!) // Iterate over notes and display each
                  ListTile(
                    contentPadding: const EdgeInsets.only(left: 20),
                    title: Text(note.message ?? ''), // Note message
                  ),
            ],
          ),
        );
      },
    );
  }
}
