import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:farmer_client/models/app.dart';
import 'package:farmer_client/models/entities/CardListModel.dart';
import 'package:farmer_client/services/homeAPI.dart';
import 'package:farmer_client/widgets/CarsCard.dart';
import 'package:farmer_client/widgets/RefreshMoreList.dart';
import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:farmer_client/pages/home/widgets/searchBannerHeader.dart';
import 'package:farmer_client/pages/MoreCars/index.dart';

class Home extends BasicPage {
  Home({Key? key}) : super(key: key) {
    tabIndex = 0;
    naviTitle = '首页';
  }

  final machineryList = Rx<List<CardListModel>>([]);
  final params = Rx<Map<String, dynamic>>({
    'pageSize': 10,
    'location': AppController.t.locationStr ?? '112.087465,32.687507'
  });
  Function? Refresh;
  List<CardListModel> GetList(value) {
    List<CardListModel> list = [];
    if (value != null)
      value.forEach((item) => {list.add(CardListModel.fromJson(item))});
    return list;
  }

  @override
  Widget builder(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(color: Colors.white),
      padding: EdgeInsets.fromLTRB(15.w, 5.w, 15.w, 0),
      child: RefreshMoreList(
        pPrams: params,
        cardText: '暂无农机',
        fetch: getMachinery,
        isHome: true,
        useState: (state) {
          machineryList(GetList(state));
        },
        builder: ({required BuildContext context, required Function toggle}) {
          Refresh = toggle;
          return Column(
            children: [
              const searchBannerHeader(),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 15.h),
                    child: Row(
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(right: 8),
                          child: Image(
                            image: const AssetImage(
                                'images/icons/carsListImga.png'),
                            fit: BoxFit.cover,
                            width: 20.w,
                          ),
                        ),
                        Text(
                          '农机列表',
                          style: TextStyle(
                              color: const Color(0xff222222),
                              fontSize: 20.sp,
                              fontWeight: FontWeight.bold),
                        )
                      ],
                    ),
                  ),
                  GestureDetector(
                    child: const Text('更多 >>'),
                    onTap: () {
                      Get.to(const MoreCars());
                    },
                  )
                ],
              ),
              Obx(
                () => Column(
                  children: machineryList.value
                      .map(
                        (item) => CarsCard(item: item),
                      )
                      .toList(),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
