import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../app/core/themes/theme.dart';
import '../../../app/data/models/models.dart';
import '../../../app/global_widgets/global_widgets.dart';
import 'controller.dart';

class OrdersDetailsPage extends StatelessWidget {
  final List<OrderItem> items;

  const OrdersDetailsPage({
    Key? key,
    required this.items,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OrderController>(
      init: OrderController(),
      builder: (oc) => WillPopScope(
        onWillPop: () => oc.onWillPopScope(),
        child: Material(
          color: Colors.white,
          child: SafeArea(
            child: Scaffold(
              appBar: CustomAppbarWidget(leading: AppBarBackBtn()),
              body: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 10.h, top: 20.h),
                    child: Text(
                      'order_details'.tr,
                      style: AppTheme.subHeaderTxtStyle,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 30.w, right: 30.w),
                    child: AppTheme.appColorDivider,
                  ),
                  Expanded(
                    child: ListView.builder(
                      padding: EdgeInsets.only(left: 30.w, right: 30.w),
                      shrinkWrap: true,
                      itemCount: items.length,
                      itemBuilder: (_, index) => ItemCard(item: items[index]),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class ItemCard extends StatelessWidget {
  final OrderItem item;
  const ItemCard({
    Key? key,
    required this.item,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(height: 12.h),
        Text(
          item.name,
          style: new TextStyle(
            color: ThemeColor.mainColor,
            fontWeight: FontWeight.w500,
            fontSize: 14.sp,
          ),
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
        ),
        SizedBox(height: 12.h),
        ItemCardRowBuilder(
          title: 'quantity'.tr,
          content: item.qtyOrdered.toString(),
        ),
        SizedBox(height: 12.h),
        ItemCardRowBuilder(title: 'price'.tr, content: item.formattedGrantTotal),
        SizedBox(height: 8.h),
        AppTheme.appColorDivider,
        SizedBox(height: 8.h),
      ],
    );
  }
}
