import 'package:easy_localization/easy_localization.dart' as lclize;
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/etqan_ovr/etqan_get_requests_model.dart';
import 'package:mohem_flutter_app/provider/etqan_ovr_provider.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:provider/provider.dart';
import 'package:intl/src/intl/text_direction.dart' as intl;
import 'package:sizer/sizer.dart';

class EtqanOvrHome extends StatefulWidget {
  const EtqanOvrHome({Key? key}) : super(key: key);

  @override
  _EtqanOvrHomeState createState() => _EtqanOvrHomeState();
}

class _EtqanOvrHomeState extends State<EtqanOvrHome> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      context.read<EtqanOvrProviderModel>().fetchEtqanEmployeeRequests(context);
    });
  }

  Color _getStatusColor(String status) {
    String statusLower = status.toLowerCase();
    if (statusLower.contains('open') || statusLower.contains('مفتوح')) {
      return const Color(0xff2196F3); // Blue for open
    } else if (statusLower.contains('progress') || statusLower.contains('قيد التنفيذ')) {
      return const Color(0xffFF9800); // Orange for in progress
    } else if (statusLower.contains('closed') || statusLower.contains('مغلق')) {
      return const Color(0xff4CAF50); // Green for closed
    }
    return MyColors.grey70Color; // Default gray
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(context, title: LocaleKeys.myIncidents.tr()),
      body: Consumer<EtqanOvrProviderModel>(
        builder: (BuildContext context, EtqanOvrProviderModel provider, Widget? child) {
          return Column(
            children: <Widget>[
              (provider.getEtqanEmployeeRequestsList == null || provider.getEtqanEmployeeRequestsList!.isEmpty
                      ? Utils.getNoDataWidget(context)
                      : ListView.separated(
                        physics: const BouncingScrollPhysics(),
                        padding: const EdgeInsets.all(20),
                        itemBuilder: (BuildContext context, int index) {
                          EtqanGetEmployeeOvrRequestsResponse data = provider.getEtqanEmployeeRequestsList![index];
                          return InkWell(
                            onTap: () {
                              context.read<EtqanOvrProviderModel>().setTicketId(data.ticketNumber ?? '', status: data.status, statusAr: data.statusAr, isAnonymous: data.isAnonymous);
                              Navigator.pushNamed(context, AppRoutes.etqanGetIncidientRequest);
                            },
                            child: Container(
                              padding: const EdgeInsets.all(12),
                              decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(15), border: Border.all(color: const Color(0xffefefef), width: 1)),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Expanded(
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Row(
                                          children: [
                                            Expanded(
                                              child: Wrap(
                                                spacing: 8,
                                                runSpacing: 8,
                                                children: [
                                                  if (data.status != null && data.status!.isNotEmpty)
                                                    Container(
                                                      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                                                      decoration: BoxDecoration(
                                                        color: _getStatusColor(data.status ?? "").withOpacity(0.1),
                                                        borderRadius: BorderRadius.circular(6),
                                                        border: Border.all(color: _getStatusColor(data.status ?? "").withOpacity(0.3), width: 1),
                                                      ),
                                                      child: (AppState().isArabic(context) ? (data.statusAr ?? "") : (data.status ?? "")).toText11(
                                                        color: _getStatusColor(data.status ?? ""),
                                                        isBold: true,
                                                      ),
                                                    ),
                                                  if (data.isAnonymous == true)
                                                    Container(
                                                      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                                                      decoration: BoxDecoration(
                                                        color: MyColors.grey70Color.withOpacity(0.15),
                                                        borderRadius: BorderRadius.circular(6),
                                                        border: Border.all(color: MyColors.grey70Color.withOpacity(0.3), width: 1),
                                                      ),
                                                      child: Row(
                                                        mainAxisSize: MainAxisSize.min,
                                                        children: [
                                                          const Icon(Icons.visibility_off, size: 12, color: MyColors.grey70Color),
                                                          const SizedBox(width: 4),
                                                          (AppState().isArabic(context) ? "مجهول" : "Anonymous").toText11(color: MyColors.grey70Color, isBold: true),
                                                        ],
                                                      ),
                                                    ),
                                                ],
                                              ),
                                            ),
                                            const SizedBox(width: 8),
                                          ],
                                        ),
                                        8.height,
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: <Widget>[
                                            (data.ticketNumber ?? "").toText14(isBold: true, color: MyColors.darkTextColor),
                                            Directionality(textDirection: TextDirection.ltr, child: (data.createdDate!.split(" ").first ?? "").toText12(color: MyColors.grey70Color)),
                                          ],
                                        ),
                                        8.height,
                                        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [(data.description ?? "").toText12(color: MyColors.grey57Color).expanded]),
                                        3.height,
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.center,
                                          mainAxisAlignment: MainAxisAlignment.end,
                                          children: [
                                            RotatedBox(
                                              quarterTurns: AppState().isArabic(context) ? 2 : 0,
                                              child: SvgPicture.asset("assets/images/arrow_next.svg", color: MyColors.grey70Color, width: 16, height: 16),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                        separatorBuilder: (BuildContext context, int index) => 12.height,
                        itemCount: provider.getEtqanEmployeeRequestsList!.length,
                      ))
                  .expanded,
              DefaultButton(LocaleKeys.reportIncident.tr(), () async {
                await Navigator.pushNamed(context, AppRoutes.etqanCreateRequest);
                // getOpenTickets();
              }).insideContainer,
            ],
          );
        },
      ),
    );
  }
}
