import 'package:cheminova/provider/collect_kyc_provider.dart';
import 'package:cheminova/screens/data_submit_successfull.dart';
import 'package:cheminova/widgets/common_background.dart';
import 'package:cheminova/widgets/common_drawer.dart';
import 'package:cheminova/widgets/common_elevated_button.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class VerifyDocumentsScreen extends StatelessWidget {
  const VerifyDocumentsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: const CommonDrawer(), // Drawer for navigation
      body: CommonBackground( // Background widget for styling
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: SingleChildScrollView(
            physics: const BouncingScrollPhysics(), // Enable bouncing effect on scroll
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Section for retailer details
                Consumer<CollectKycProvider>( // Listen to changes in the KYC provider
                  builder: (BuildContext context, CollectKycProvider value,
                      Widget? child) =>
                      _buildSection(
                        'Retailer Details', // Title for the section
                        {
                          'Trade Name': value.tradeNameController.text,
                          'Name': value.nameController.text,
                          'Address': value.addressController.text,
                          'Town/City': value.city.text,
                          'District': value.districtController.text,
                          'State': value.state.text,
                          'Pincode': value.pinCodeController.text,
                          'Mobile Number': value.mobileNumberController.text,
                          'Aadhar Number': value.aadharNumberController.text,
                          'PAN Number': value.panNumberController.text,
                          'GST Number': value.gstNumberController.text,
                          'Mapped Principal Distributor':
                          value.selectedDistributor ?? '',
                        },
                        onEdit: () {
                          value.tabController.animateTo(0); // Navigate to the first tab for editing
                          debugPrint('Edit retailer details'); // Debug message for tracking
                        },
                      ),
                ),
                const SizedBox(height: 20),
                // Section for uploaded documents
                Consumer<CollectKycProvider>( // Listen to changes in the KYC provider
                  builder: (BuildContext context, CollectKycProvider value,
                      Widget? child) =>
                      _buildSection(
                        'Uploaded Documents', // Title for the section
                        {
                          'PAN Card':
                          value.panCard?.path.split('/').last ?? 'Not uploaded',
                          'Aadhar Card': value.aadharCard?.path.split('/').last ??
                              'Not uploaded',
                          'GST Registration':
                          value.gstRegistration?.path.split('/').last ??
                              'Not uploaded',
                          'Pesticide License':
                          value.pesticideLicense?.path.split('/').last ??
                              'Not uploaded',
                          'Fertilizer License':
                          value.fertilizerLicense?.path.split('/').last ??
                              'Not uploaded',
                          'Selfie of Entrance Board':
                          value.selfieEntranceBoard?.path.split('/').last ??
                              'Not uploaded',
                        },
                        onEdit: () {
                          value.tabController.animateTo(1); // Navigate to the second tab for editing
                          debugPrint('Edit uploaded documents'); // Debug message for tracking
                        },
                      ),
                ),
                const SizedBox(height: 30),
                // Button to save and confirm the details
                Align(
                  alignment: Alignment.center,
                  child: Consumer<CollectKycProvider>( // Listen to changes in the KYC provider
                    builder: (context, value, child) => CommonElevatedButton(
                      borderRadius: 30,
                      width: double.infinity,
                      height: kToolbarHeight - 10,
                      text: 'SAVE AND CONFIRM', // Button text
                      backgroundColor: const Color(0xff004791),
                      onPressed: () {
                        // Handle final submission of KYC details
                        value.validateFields(context); // Validate fields before submission
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // Method to build each section of the screen
  Widget _buildSection(String title, Map<String, String> details,
      {required VoidCallback onEdit}) {
    return Container(
      padding: const EdgeInsets.all(20.0),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.white),
        color: const Color(0xffB4D1E5).withOpacity(0.9), // Background color for sections
        borderRadius: BorderRadius.circular(24.0),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                title, // Title of the section
                style:
                const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              ElevatedButton(
                onPressed: onEdit, // Trigger edit function
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xffFFFFFF), // White background for the edit button
                  elevation: 0,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20),
                  ),
                ),
                child: const Text('Edit'), // Edit button text
              ),
            ],
          ),
          const SizedBox(height: 10),
          // Build detail rows from the provided details map
          ...details.entries
              .map((entry) => _buildDetailRow(entry.key, entry.value)),
        ],
      ),
    );
  }

  // Method to build a single detail row
  Widget _buildDetailRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            flex: 2,
            child: Text(
              label, // Label for the detail
              style: const TextStyle(fontWeight: FontWeight.bold),
            ),
          ),
          Expanded(
              flex: 3,
              child: Text(
                value, // Value for the detail
                style: TextStyle(
                    color: value == 'Not uploaded' ? Colors.red : Colors.black), // Change color if not uploaded
              )),
        ],
      ),
    );
  }
}
