import 'dart:math';

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:worker_client/models/entities/Banner.dart';
import 'package:worker_client/models/entities/Job.dart';
import 'package:worker_client/pages/home/widgets/headers.dart';
import 'package:worker_client/services/banner.dart';
import 'package:worker_client/services/job.dart';
import 'package:worker_client/widgets/LoadMore.dart';
import 'package:worker_client/widgets/MyCard/index.dart';
import 'package:worker_client/widgets/Refresher.dart';
import 'package:worker_client/widgets/layout/BasicPage.dart';

import '../../models/entities/PagedResult.dart';

class Home extends BasicPage {
  Home({Key? key}) : super(key: key) {
    tabIndex = 0;
    naviTitle = '首页';
  }
  final bannerList = Rx<List<BannerModel>>([]);
  final jobList = Rx<List<Job>>([]);
  final job = Rx<Job>(Job());

  void beforeShow() {
    super.beforeShow();
    getBannerList('bannerWorker').then((res) {
      List<BannerModel> list = [];
      for (var item in res) {
        list.add(BannerModel.fromJson(item));
      }
      bannerList.value = list;
    });
  }

  List<Job> GetList(value) {
    List<Job> list = [];
    if (value != null) value.forEach((item) => {list.add(Job.fromJson(item))});
    return list;
  }

  Map<String, dynamic> map = {'pageNum': 1};

  void changeJob(Job item) {}
  void goDetail(Job item) {
    Get.toNamed('/jobDetail', arguments: {'id': item.jobId});
  }

  Function? Refresh;
  @override
  Widget builder(BuildContext context) {
    return Column(
      children: [
        Container(
          decoration: const BoxDecoration(color: Colors.white),
          padding: EdgeInsets.all(15.w),
          child: Column(
            children: [
              // const headers(),
              // //banner
              // Container(
              //   margin: EdgeInsets.fromLTRB(0, 15.h, 0, 15.h),
              //   child: Obx(() => CarouselSlider(
              //         items: bannerList.value
              //             .map((item) => Container(
              //                   child: Center(
              //                       child: Image.network(item.thumb.toString(),
              //                           fit: BoxFit.cover, width: 345.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, 15.h, 0, 20.h),
              //   child: Row(
              //     children: [
              //       Padding(
              //         padding: EdgeInsets.fromLTRB(0, 0, 10.w, 0),
              //         child: Image.asset(
              //           'images/index/orderList.png',
              //           width: 20.w,
              //         ),
              //       ),
              //       Text(
              //         '作业列表',
              //         style: TextStyle(
              //             color: Color(0xff222222),
              //             fontSize: 20.sp,
              //             fontWeight: FontWeight.w800),
              //       )
              //     ],
              //   ),
              // ),

              Container(
                // height: 185.h,
                height: 585.h,
                child: LoadMore(
                    fetch: (params) => getJobList(params),
                    builder: (
                        {required BuildContext context,
                        required ScrollController scrollController,
                        required PagedResult data,
                        required bool haveMore}) {
                      return RefreshBuilder(
                        fetch: (params) => getJobList(params),
                        useState: (value) {
                          jobList(GetList(value.records));
                        },
                        builder: (
                            {required BuildContext context,
                            required Function toggle}) {
                          Refresh = toggle;
                          return ListView.builder(
                              controller: scrollController,
                              itemCount: 1,
                              itemBuilder:
                                  (BuildContext context, int position) {
                                if (jobList.value.isNotEmpty &&
                                    jobList.value[0].jobId !=
                                        GetList(data.records)[0].jobId) {
                                  jobList([
                                    ...jobList.value,
                                    ...GetList(data.records)
                                  ]);
                                }

                                return Obx(() {
                                  if (jobList.value != []) {
                                    return Column(
                                      children: jobList.value
                                          .map(
                                            (item) => MyCard(
                                              item: item,
                                              job: true,
                                              detail: false,
                                              onClick: () {
                                                changeJob(item);
                                              },
                                              goDetail: () {
                                                goDetail(item);
                                              },
                                              buttomText: item.status == 0
                                                  ? '开始作业'
                                                  : '完成作业',
                                            ),
                                          )
                                          .toList(),
                                    );
                                  } else {
                                    return const Text('暂无数据');
                                  }
                                });
                              });
                        },
                        onError: () {},
                      );
                    }),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
