import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/utils/app_utils.dart';
import 'package:cargocalculaterapp/features/home/presentation/pages/widgets/status_widget.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:cargocalculaterapp/router/name_routes.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../../../data/models/orders_list_response.dart';
import '../arguments/order_info_argument.dart';
import 'order_text_widget.dart';

class OrdersListWidget extends StatelessWidget {
  const OrdersListWidget({super.key, required this.data});

  final List<Data>? data;

  @override
  Widget build(BuildContext context) {
    return SliverPadding(
      padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
      sliver: SliverList.separated(
        itemBuilder: (context, index) {
          return InkWell(
            onTap: () {
              Navigator.pushNamed(
                context,
                Routes.orderInfo,
                arguments: OrderInfoArgument(
                  orderId: data?[index].id ?? "",
                  orderNumber: data?[index].orderNumber ?? "",
                ),
              );
            },
            borderRadius: AppUtils.kBorderRadius16,
            child: Ink(
              padding: AppUtils.kPaddingAll16,
              width: double.infinity,
              decoration: BoxDecoration(
                borderRadius: AppUtils.kBorderRadius16,
                color: context.color.statusBackground,
                border: Border.all(color: context.color.lightBorder),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    children: [
                      OrderTextWidget(
                        title: AppLocalization.current.order_number,
                        name: data?[index].orderNumber ?? "",
                      ),
                      StatusWidget(status: data?[index].status?.code ?? ""),
                    ],
                  ),
                  AppUtils.kBoxHeight16,
                  Row(
                    children: [
                      OrderTextWidget(
                        title: AppLocalization.current.warehouse,
                        name:
                            Localizations.localeOf(context).languageCode == "uz"
                            ? (data?[index].wharehouseUz ?? "")
                            : Localizations.localeOf(context).languageCode ==
                                  "ru"
                            ? (data?[index].wharehouseRu ?? "")
                            : (data?[index].wharehouseZh ?? ""),
                      ),
                      OrderTextWidget(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        title: AppLocalization.current.order_name,
                        name:
                            Localizations.localeOf(context).languageCode == "uz"
                            ? (data?[index].nameUz ?? "")
                            : Localizations.localeOf(context).languageCode ==
                                  "ru"
                            ? (data?[index].nameRu ?? "")
                            : (data?[index].nameZh ?? ""),
                      ),
                    ],
                  ),
                  AppUtils.kBoxHeight16,
                  Row(
                    children: [
                      OrderTextWidget(
                        title: AppLocalization.current.delivery_price,
                        name: (data?[index].deliveryPrice ?? ""),
                      ),
                      OrderTextWidget(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        title: AppLocalization.current.delivery_time,
                        name: DateFormat('dd-MM-yyyy').format(
                          DateTime.tryParse(data?[index].arrivalDate ?? "") ??
                              DateTime.now(),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          );
        },
        separatorBuilder: (_, __) => AppUtils.kBoxHeight12,
        itemCount: data?.length ?? 0,
      ),
    );
  }
}
