import 'package:carousel_slider/carousel_slider.dart';
import 'package:farmer_client/pages/home/widgets/home/widgets/headers.dart';
import 'package:farmer_client/widgets/NullCard.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../../models/app.dart';
import '../../../../models/entities/CardInfo.dart';
import '../../../../models/entities/CardListModel.dart';
import '../../../../services/homeAPI.dart';
import '../../../../widgets/CarsCard.dart';
import 'package:farmer_client/models/entities/Banners.dart';
import '../../../MoreCars/index.dart';

class HomePage extends StatefulWidget {
  const HomePage({
    Key? key,
  }) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final CarouselController _controller = CarouselController();
  List<Banners> bannerList = [];
  String? location = AppController.t.locationStr;

  List<CardListModel> machineryLists = [];

  @override
  void initState() {
    super.initState();
   // final long=   location.value!['longitude'].toString() + "," + location.value!['latitude'].toString();
   //  print('location+$location,lolongglongng+$long');

    getHomeBanner('banner').then((value) {
      setState(() {
        value.forEach((item) {
          bannerList.add(Banners.fromJson(item));
        });
      });
    });

    getMachinery(location??'112.087465,32.687507').then((value) {
      setState(() {
        value['records'].forEach((item) {
          machineryLists.add(CardListModel.fromJson(item));
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white
      ),
      alignment: Alignment.center,
      padding: EdgeInsets.fromLTRB(15.w, 0, 15.w, 0),
      child:  Column(
        children: [
          TypeHeader(
            type: true,
          ),
          Container(
            margin: EdgeInsets.fromLTRB(0, 15, 0, 0),
            child: CarouselSlider(
              items: bannerList
                  .map((item) => Container(
                        child: Center(
                            child: Image.network(item.thumb.toString(),
                                fit: BoxFit.cover, width: 350.w)),
                      ))
                  .toList(),
              options: CarouselOptions(
                autoPlay: true,
                enlargeCenterPage: false, //图片中心放大
                viewportFraction: 1, //每个页面应占据的视口部分。默认为 0.8，这意味着每个页面填充 80% 的轮播。
                // aspectRatio: 1.6,//纵横比
                height: 214.w,
                initialPage: 1, //初始页
              ),
            ),
          ),
          Container(
              margin: EdgeInsets.fromLTRB(0, 20, 0, 20),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child: Row(
                          children: [
                            Padding(
                              padding: EdgeInsets.fromLTRB(0, 0, 8, 0),
                              child: Image(
                                image:
                                    AssetImage('images/icons/carsListImga.png'),
                                fit: BoxFit.cover,
                                width: 20.w,
                              ),
                            ),
                            Text(
                              '农机列表',
                              style: TextStyle(
                                  color: Color(0xff222222),
                                  fontSize: 20.sp,
                                  fontWeight: FontWeight.bold),
                            )
                          ],
                        ),
                      ),
                      Container(
                        child: GestureDetector(
                          child: Text('更多 >>'),
                          onTap: () {
                            print('点我进入农机搜索/更多页');
                            Get.to(MoreCars());
                          },
                        ),
                      )
                    ],
                  ),
                  machineryLists.length>= 1?
                  Column(
                    children: machineryLists
                        .map((item) => CarsCard(item: item))
                        .toList(),
                  ):NullCard(text: '暂无农机信息！')
                ],
              )),
        ],
      ),
    );
  }
}
