import 'package:flutter/material.dart';
import 'package:glowwheels/helpers/shopid_helper.dart';
import 'package:provider/provider.dart';
import '../provider/serviceboy_provider.dart';
import 'add_serviceboy_screen.dart';
import 'edit_serviceboy_screen.dart';
import 'package:google_fonts/google_fonts.dart';

class ServiceBoyScreen extends StatefulWidget {
  @override
  State<ServiceBoyScreen> createState() => _ServiceBoyScreenState();
}
late String shopId='';
class _ServiceBoyScreenState extends State<ServiceBoyScreen> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
     shopId = getShopId(context)!;
      if (shopId != null) {
        Provider.of<ServiceBoyProvider>(context, listen: false)
            .fetchServiceBoys(shopId);
      } else {
        print("Shop ID is null");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<ServiceBoyProvider>(context);

    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [Color.fromRGBO(208, 235, 255, 1), Colors.white],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: SafeArea(
          child: Column(
            children: [
              SizedBox(height: 40),
              Text(
                'GLOWWHEELS',
                style: GoogleFonts.inter(
                  fontSize: 32,
                  color: Color.fromRGBO(25, 25, 112, 0.87),
                  fontWeight: FontWeight.w700,
                ),
              ),
              Text(
                'Service Center',
                style: GoogleFonts.inter(
                  fontSize: 24,
                  color: Color.fromRGBO(25, 25, 112, 0.87),
                  fontWeight: FontWeight.w400,
                ),
              ),
              SizedBox(height: 20),
              Text(
                'Service Boy List',
                style: GoogleFonts.inter(
                  fontSize: 16,
                  color: Color.fromRGBO(33, 33, 33, 1),
                  fontWeight: FontWeight.w500,
                ),
              ),
              SizedBox(height: 10),
              Expanded(
                child: provider.isLoading
                    ? Center(child: CircularProgressIndicator())
                    : provider.serviceBoys.isEmpty
                    ? Center(child: Text("No service boys found"))
                    : ListView.builder(
                  padding: EdgeInsets.symmetric(
                      horizontal: 16, vertical: 8),
                  itemCount: provider.serviceBoys.length,
                  itemBuilder: (context, index) {
                    final boy = provider.serviceBoys[index];
                    return Card(
                      color: Colors.white,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12)),
                      elevation: 2,
                      child: ListTile(
                        contentPadding: EdgeInsets.symmetric(
                            horizontal: 16, vertical: 8),
                        title: Text(
                          boy.name,
                          style: GoogleFonts.inter(
                            fontSize: 18,
                            color: Color.fromRGBO(33, 33, 33, 0.78),
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        subtitle: Text(
                          boy.phone,
                          style: GoogleFonts.inter(
                            fontSize: 16,
                            color: Color.fromRGBO(33, 33, 33, 0.78),
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        trailing: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            _buildIconButton(
                              icon: Icons.edit,
                              onPressed: () {
                                Navigator.of(context).push(
                                  MaterialPageRoute(
                                    builder: (_) =>
                                        EditServiceBoyScreen(
                                            serviceBoy: boy),
                                  ),
                                );
                              },
                            ),
                            SizedBox(width: 8),
                            _buildIconButton(
                              icon: Icons.delete,
                              onPressed: () {
                                _showDeleteDialog(context, boy.id);
                              },
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          shape: CircleBorder(),
          backgroundColor: Colors.white,
          elevation: 4,
          child: Icon(
            Icons.add,
            color: Color(0xFF1F1762),
            size: 25,
          ),
          onPressed: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (_) => AddServiceBoyScreen()),
            );
          },
        ),
      ),
    );
  }

  Widget _buildIconButton(
      {required IconData icon, required VoidCallback onPressed}) {
    return Container(
      height: 40,
      width: 40,
      child: Center(
        child: Ink(
          decoration: ShapeDecoration(
            color: Color(0xFF1F1762),
            shape: CircleBorder(),
          ),
          child: IconButton(
            icon: Icon(icon, color: Colors.white, size: 23),
            onPressed: onPressed,
            splashRadius: 15,
          ),
        ),
      ),
    );
  }

  void _showDeleteDialog(BuildContext context, String id) {
    final shopId = getShopId(context);

    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder: (BuildContext bottomSheetContext) {
        return Container(
          padding: EdgeInsets.all(24),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Image.asset('assets/images/delete_serviceboy.png'),
              SizedBox(height: 16),
              Text(
                'Delete Service boy?',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
              SizedBox(height: 8),
              Text(
                'Are you sure you want to delete this service boy? You won’t be able to undo this.',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.black54,
                ),
              ),
              SizedBox(height: 24),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red[700],
                    padding: EdgeInsets.symmetric(vertical: 14),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onPressed: () async {
                    if (shopId != null) {
                      await Provider.of<ServiceBoyProvider>(
                        bottomSheetContext,
                        listen: false,
                      ).deleteServiceBoy(id, shopId);
                    }
                    Navigator.pop(bottomSheetContext); // Correct context
                  },
                  child: Text(
                    'Confirm',
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  ),
                ),
              ),
              SizedBox(height: 12),
            ],
          ),
        );
      },
    );
  }

}
