import 'package:cheminova/screens/view_pdf_screen.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:provider/provider.dart';

import '../models/product_manual_model.dart';
import '../provider/product_manual_provider.dart';

// Screen to display product manuals
class ProductsManualScreen extends StatefulWidget {
  const ProductsManualScreen({super.key});

  @override
  State<ProductsManualScreen> createState() => _ProductsManualScreenState();
}

class _ProductsManualScreenState extends State<ProductsManualScreen> {
  late ProductManualProvider _productManualProvider;

  @override
  void initState() {
    _productManualProvider = ProductManualProvider(); // Initialize the product manual provider
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ProductManualProvider>(
      create: (_) => _productManualProvider,
      builder: (context, 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(
              'Products Manual',
              style: TextStyle(
                  fontSize: 20,
                  color: Colors.black,
                  fontWeight: FontWeight.w400,
                  fontFamily: 'Anek'),
            ),
            backgroundColor: Colors.transparent,
            elevation: 0,
          ),
          drawer: const CommonDrawer(),
          body: Padding(
            padding: const EdgeInsets.all(16.0),
            child: SingleChildScrollView(
              physics: const BouncingScrollPhysics(),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  const SizedBox(height: 16),
                  Consumer<ProductManualProvider>(
                    builder: (context, provider, child) {
                      if (provider.isLoading) {
                        return const Center(
                          child: CircularProgressIndicator(), // Show loading indicator while data is being fetched
                        );
                      }

                      if (provider.productManualList.isEmpty) {
                        return const Center(
                            child: Text('No products available.')); // Message when no products are available
                      }

                      return Container(
                        padding: const EdgeInsets.all(20.0)
                            .copyWith(top: 30, bottom: 30),
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.white),
                          color: const Color(0xffB4D1E5).withOpacity(0.9),
                          borderRadius: BorderRadius.circular(26.0),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: provider.productManualList
                              .map(
                                (product) =>
                                _buildProductButton(context, product), // Build buttons for each product
                          )
                              .toList(),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  // Method to build a button for each product
  Widget _buildProductButton(BuildContext context, ProductManualModel product) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 15),
      child: CommonElevatedButton(
        borderRadius: 30,
        width: double.infinity,
        height: kToolbarHeight - 10,
        text: product.title, // Display product title on the button
        backgroundColor: const Color(0xff004791),
        onPressed: () => Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ViewPdfScreen(productManualModel: product), // Navigate to the PDF view screen
          ),
        ),
      ),
    );
  }
}
