import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/screens/hrm/TourExpensesDetailsScreen.dart';
import 'package:provider/provider.dart';
import '../../Utils/app_colors.dart';
import '../../Models/hrmModels/tourExpensesListResponse.dart';
import '../../Notifiers/hrmProvider/tourExpensesProvider.dart';
import 'AddTourExpBillScreen.dart';

class TourExpensesListScreen extends StatefulWidget {
  const TourExpensesListScreen({super.key});

  @override
  State<TourExpensesListScreen> createState() => _TourExpensesListScreenState();
}

class _TourExpensesListScreenState extends State<TourExpensesListScreen> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: ChangeNotifierProvider(
        create: (_) => TourExpensesProvider()..fetchTourExpenses(context, "1"),
        child: Consumer<TourExpensesProvider>(
          builder: (context, provider, child) {
            return Scaffold(
              appBar: AppBar(
                automaticallyImplyLeading: false,
                backgroundColor: Colors.white,
                title: Row(
                  children: [
                    InkResponse(
                      onTap: () => Navigator.pop(context, true),
                      child: SvgPicture.asset(
                        "assets/svg/appbar_back_button.svg",
                        height: 25,
                      ),
                    ),
                    const SizedBox(width: 10),
                    const Text(
                      "Tour Expenses",
                      style: TextStyle(
                        fontSize: 18,
                        fontFamily: "Plus Jakarta Sans",
                        fontWeight: FontWeight.w600,
                        color: Colors.black87,
                      ),
                    ),
                  ],
                ),
              ),

              backgroundColor: const Color(0xFFF6F6F8),

              body: Column(
                children: [
                  Expanded(
                    child: Builder(
                      builder: (context) {
                        if (provider.isLoading) {
                          return const Center(
                              child: CircularProgressIndicator(color: Colors.blue));
                        }
                        if (provider.errorMessage != null) {
                          return Center(child: Text(provider.errorMessage!));
                        }
                        if (provider.response?.tourList == null ||
                            provider.response!.tourList!.isEmpty) {
                          return const Center(child: Text("No Tour Expenses Found"));
                        }

                        final list = provider.response!.tourList!;
                        return ListView.builder(
                          padding: const EdgeInsets.all(12),
                          itemCount: list.length,
                          itemBuilder: (context, index) {
                            final TourList item = list[index];

                            return InkWell(
                              onTap: () {
                                ///  navigation flow
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) => TourExpensesDetailsScreen(
                                      tourBillId: item.id.toString(),
                                    ),
                                  ),
                                );
                              },
                              child: Container(
                                margin: const EdgeInsets.symmetric(vertical: 6),
                                padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(16),
                                ),
                                child: Row(
                                  children: [
                                    /// Left Avatar Circle
                                    Container(
                                      height: 46,
                                      width: 46,
                                      decoration: BoxDecoration(
                                        color: _getAvatarColor(item.approvalStatus),
                                        shape: BoxShape.circle,
                                      ),
                                      child: Center(
                                        child: Text(
                                          getText(item.approvalStatus),
                                          style: TextStyle(
                                            fontSize: 15,
                                            fontFamily: "Plus Jakarta Sans",
                                            fontWeight: FontWeight.w500,
                                            color: _getAvatarTxtColor(item.approvalStatus),
                                          ),
                                        ),
                                      ),
                                    ),
                                    const SizedBox(width: 12),

                                    /// Middle Section
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            item.placeOfVisit ?? "-",
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                              fontFamily: "JakartaRegular",
                                              fontSize: 14,
                                              color: AppColors.semi_black,
                                            ),
                                          ),
                                          Text(
                                            item.appliedDate ?? "-",
                                            style: TextStyle(
                                              fontFamily: "JakartaRegular",
                                              fontSize: 14,
                                              color: AppColors.grey_semi,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),

                                    /// Right Section (Applied Amount)
                                    Text(
                                      "₹${item.appliedAmount ?? '0'}",
                                      style: const TextStyle(
                                        fontFamily: "JakartaMedium",
                                        fontSize: 14,
                                        color: Color(0xff1487c9),
                                      )
                                    ),
                                  ],
                                ),
                              ),
                            );
                          },
                        );
                      },
                    ),
                  ),
                ],
              ),

              bottomNavigationBar: Container(
                padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 10),
                color: Colors.white,
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xff1487c9), // App blue
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15),
                    ),
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    elevation: 0, // Optional: remove shadow
                  ),
                  onPressed: () {
                    HapticFeedback.selectionClick();
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const AddBillScreen(pageTitleName: "Add Bill"),
                        settings: const RouteSettings(name: 'AddTourExpBillScreen'),
                      ),
                    ).then((_) {
                      provider.fetchTourExpenses(context, "1");
                    });
                  },
                  child: const Text(
                    "Add Bill",
                    style: TextStyle(
                      fontSize: 16,
                      fontFamily: "JakartaMedium",
                      fontWeight: FontWeight.w500,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),

              // /// Bottom Add Bill Button
              // bottomNavigationBar: Container(
              //   padding: const EdgeInsets.all(18),
              //   color: Colors.white,
              //   child: ElevatedButton(
              //     style: ElevatedButton.styleFrom(
              //       backgroundColor: Color(0xff1487c9),
              //       shape: RoundedRectangleBorder(
              //         borderRadius: BorderRadius.circular(15),
              //       ),
              //       padding: const EdgeInsets.symmetric(vertical: 14),
              //     ),
              //     onPressed: () {
              //       // to work
              //     },
              //     child: const Text(
              //       "Add Bill",
              //       style: TextStyle(
              //         fontSize: 16,
              //         fontFamily: "Plus Jakarta Sans",
              //         fontWeight: FontWeight.w500,
              //         color: Colors.white,
              //       ),
              //     ),
              //   ),
              // ),
            );
          },
        ),
      ),
    );
  }

  /// Avatar color generator
  Color _getAvatarColor(value) {
    var color = AppColors.approved_bg_color;
    switch (value) {
      case 'HR Approved':
        return AppColors.approved_bg_color;
      case 'Expired at HR':
        return AppColors.rejected_bg_color;
      case 'Expired at TL':
        return AppColors.rejected_bg_color;
    }
    return color;
  }

  Color _getAvatarTxtColor(value) {
    var color = AppColors.approved_text_color;
    switch (value) {
      case 'HR Approved':
        return AppColors.approved_text_color;
      case 'Expired at HR':
        return AppColors.rejected_text_color;
      case 'Expired at TL':
        return AppColors.rejected_text_color;

    }
    return color;
  }
  getText(value) {
    switch (value) {
      case 'HR Approved':
        return "A";
      case 'Expired at HR':
        return "E";
      case 'Expired at TL':
        return "E";
      case 'Updated':
        return "U";
      default:
        return "R";
    }
  }

}
