import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:taxglide/consts/comman_button.dart';
import 'package:taxglide/controller/api_contoller.dart';

class StaffListScreen extends ConsumerStatefulWidget {
  const StaffListScreen({super.key});

  @override
  ConsumerState<StaffListScreen> createState() => _StaffListScreenState();
}

class _StaffListScreenState extends ConsumerState<StaffListScreen> {
  @override
  Widget build(BuildContext context) {
    final staffAsync = ref.watch(staffListProvider);

    const commonTextStyle = TextStyle(
      fontFamily: "Gilroy-SemiBold",
      fontWeight: FontWeight.w600,
      fontSize: 14.7,
      height: 1.30, // 130%
      letterSpacing: 0.04,
      color: Color(0xFF111827),
    );
    const commonTextStylevalue = TextStyle(
      fontFamily: "Gilroy-SemiBold",
      fontWeight: FontWeight.w400,
      fontSize: 14.7,
      height: 1.30, // 130%
      letterSpacing: 0.04,
      color: Color(0xFF111827),
    );
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 230, 229, 229),
      body: SafeArea(
        child: staffAsync.when(
          loading: () => const Center(child: CircularProgressIndicator()),

          error: (err, _) => Center(
            child: Text(
              'Error: $err',
              style: const TextStyle(color: Colors.red),
            ),
          ),

          data: (staffList) {
            if (staffList.isEmpty) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Text(
                      "No Staff Found",
                      style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.w600,
                        color: Colors.black87,
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(16),
                      child: CommanButton(
                        text: 'Go Back',
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  ],
                ),
              );
            }

            return SingleChildScrollView(
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 24),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      GestureDetector(
                        onTap: () => Navigator.pop(context),
                        child: const Icon(
                          Icons.arrow_back_ios_new,
                          color: Colors.black,
                          size: 22,
                        ),
                      ),
                      const Expanded(
                        child: Center(
                          child: Text(
                            'Sub - Staff List',
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.w700,
                              color: Colors.black,
                            ),
                          ),
                        ),
                      ),
                      const Opacity(
                        opacity: 0,
                        child: Icon(Icons.arrow_back_ios_new, size: 22),
                      ),
                    ],
                  ),

                  const SizedBox(height: 50),

                  ...staffList.map((staff) {
                    String statusText = staff.status == 1
                        ? "Active"
                        : "Inactive";
                    String generalChatText = staff.generalChat == 1
                        ? "Yes"
                        : "No";
                    String createServiceText = staff.createService == 1
                        ? "Yes"
                        : "No";

                    Color statusColor = staff.status == 1
                        ? Colors.green
                        : Colors.red;

                    return Container(
                      width: double.infinity,
                      padding: const EdgeInsets.all(16),
                      margin: const EdgeInsets.only(bottom: 16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: const [
                          BoxShadow(
                            color: Colors.black12,
                            blurRadius: 4,
                            offset: Offset(0, 2),
                          ),
                        ],
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          /// ✅ Name
                          Row(
                            children: [
                              Text("Name :  ", style: commonTextStyle),
                              Expanded(
                                child: Text(
                                  staff.name,
                                  style: commonTextStylevalue,
                                ),
                              ),
                            ],
                          ),

                          const SizedBox(height: 6),

                          /// ✅ Mobile
                          Row(
                            children: [
                              Text("Mobile :  ", style: commonTextStyle),
                              Expanded(
                                child: Text(
                                  staff.mobile,
                                  style: commonTextStylevalue,
                                ),
                              ),
                            ],
                          ),

                          const SizedBox(height: 6),

                          /// ✅ Email
                          Row(
                            children: [
                              Text("Email :  ", style: commonTextStyle),
                              Expanded(
                                child: Text(
                                  staff.email,
                                  style: commonTextStylevalue,
                                ),
                              ),
                            ],
                          ),

                          const SizedBox(height: 10),

                          /// ✅ Status
                          Row(
                            children: [
                              Text("Status :  ", style: commonTextStyle),
                              Text(
                                statusText,
                                style: commonTextStylevalue.copyWith(
                                  color: statusColor,
                                ),
                              ),
                            ],
                          ),

                          const SizedBox(height: 6),

                          /// ✅ General Chat
                          Row(
                            children: [
                              Text("General Chat :  ", style: commonTextStyle),
                              Text(
                                generalChatText,
                                style: commonTextStylevalue,
                              ),
                            ],
                          ),

                          const SizedBox(height: 6),

                          /// ✅ Create Service
                          Row(
                            children: [
                              Text(
                                "Create Service :  ",
                                style: commonTextStyle,
                              ),
                              Text(
                                createServiceText,
                                style: commonTextStylevalue,
                              ),
                            ],
                          ),
                        ],
                      ),
                    );
                  }).toList(),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
