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

import '../../../app.dart';
import '../controller.dart';
import 'order_widget.dart';

class OrdersListWidget extends StatelessWidget {
  const OrdersListWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetX<OrderController>(
      init: OrderController(),
      builder: (oc) {
        final bool isLastPage = oc.olState.lastPage.value;
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 10.h, top: 20.h),
              child: Text(
                'orders'.tr,
                style: AppTheme.subHeaderTxtStyle,
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 30.w, right: 30.w),
              child: AppTheme.appColorDivider,
            ),
            Expanded(
              child: oc.obx(
                (state) => state != null
                    ? RefreshIndicator(
                        onRefresh: oc.refreshList,
                        color: ThemeColor.mainColor,
                        child: ListView.builder(
                          padding: EdgeInsets.only(top: 16, bottom: 16, left: 30, right: 30),
                          shrinkWrap: true,
                          controller: oc.scroll,
                          itemCount: state.length + 1,
                          itemBuilder: (context, index) {
                            if (index < state.length) {
                              return OrderWidget(
                                model: state[index],
                                items: oc.getOrderItems(state[index]),
                              );
                            } else if (index == state.length && !isLastPage)
                              return Padding(
                                padding: const EdgeInsets.only(top: 10, bottom: 40),
                                child: Center(child: CustomLoader()),
                              );
                            else {
                              return SizedBox.shrink();
                            }
                          },
                        ),
                      )
                    : retryBuilder(oc.fetchProducts),
                onLoading: CustomLoader(),
                onEmpty: ItemNotFoundWidget(),
                onError: (error) => retryBuilder(oc.fetchProducts),
              ),
            ),
          ],
        );
      },
    );
  }
}

class CenterWidget extends StatelessWidget {
  final Widget child;
  CenterWidget({required this.child});
  @override
  Widget build(BuildContext context) {
    return Container(
      color: ThemeColor.white,
      height: 1.sh,
      child: Stack(
        children: [
          Positioned(
            top: 0.3.sh,
            left: 0,
            right: 0,
            child: child,
          ),
        ],
      ),
    );
  }
}
