import 'package:farmer_client/pages/order/widgets/index.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:farmer_client/models/entities/OrderInfoModel.dart';
import 'package:farmer_client/services/orderAPI.dart';
import 'package:farmer_client/widgets/OrderListCard.dart';
import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:farmer_client/widgets/RefreshMoreList.dart';

class OrderPage extends BasicPage {
  OrderPage({Key? key}) : super(key: key) {
    tabIndex = 1;
    naviTitle = '订单';
  }

  final orderList = Rx<List<OrderInfoModel>>([]);
  final params = Rx<Map<String, dynamic>>({'pageSize': 10, 'mine': true});
  Function? Refresh;
  List<OrderInfoModel> GetList(value) {
    List<OrderInfoModel> list = [];
    if (value != null)
      value.forEach((item) => {list.add(OrderInfoModel.fromJson(item))});
    return list;
  }

  @override
  Widget builder(BuildContext context) {
    return Column(
      children: [
        const OrderHead(),
        Expanded(
          child: RefreshMoreList(
            pPrams: params,
            cardText: '您还没有新的订单',
            fetch: getOrderList,
            useState: (state) {
              orderList(GetList(state));
            },
            builder: (
                {required BuildContext context, required Function toggle}) {
              Refresh = toggle;
              return Obx(
                () => Column(
                  children: orderList.value
                      .map(
                        (item) => OrderListCard(item: item),
                      )
                      .toList(),
                ),
              );
            },
          ),
        ),
      ],
    );
  }
}
