import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/home_page/children/student_work_detail/widget/personal_detail_topbar.dart';
import 'package:syncfusion_flutter_datepicker/datepicker.dart';

class JobConditionFilter extends StatefulWidget {
  final TabController controller;
  final int jobType;
  final PickerDateRange? customTime;
  final Function? refreshTime;
  final String customTimeStr;
  final bool? hasAll;
  final Function(String? startTime, String? endTime) onTimeFilter;
  const JobConditionFilter({
    Key? key,
    required this.controller,
    required this.jobType,
    this.customTime,
    this.refreshTime,
    required this.customTimeStr,
    required this.onTimeFilter,
    this.hasAll = false,
  }) : super(key: key);

  @override
  State<JobConditionFilter> createState() => _JobConditionFilterState();
}

class _JobConditionFilterState extends State<JobConditionFilter> {
  var customTimeState = const PickerDateRange(null, null);

  @override
  void initState() {
    super.initState();
    if (widget.customTime != null) {
      customTimeState = PickerDateRange(widget.customTime!.startDate, widget.customTime!.endDate);
    }
  }

  DateTime getMonthStartDate() {
    // DateTime now = DateTime.now();
    // return DateTime(now.year, now.month, 1); // 获取当前月份的第一天
    DateTime now = DateTime.now();
    return now.subtract(const Duration(days: 29));
  }

  DateTime getMonthEndDate() {
    DateTime now = DateTime.now();
    // int nextMonth = now.month + 1;
    // if (nextMonth > 12) {
    //   nextMonth = 1;
    //   now = now.add(Duration(days: 31 - now.day)); // 跨年了，所以加到当前月的最后一天
    // } else {
    //   now = now.add(Duration(days: DateTime(now.year, nextMonth, 0).day - now.day)); // 加到下个月的第一天的前一天，即本月最后一天
    // }
    return now;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // height: 39.h,
      // padding: EdgeInsets.only(left: 4.w, right: 12.w),
      decoration: const BoxDecoration(
        color: Color.fromRGBO(244, 244, 244, 1),
        // border: Border(bottom: BorderSide(color: Color.fromRGBO(204, 204, 204, 1), width: 1)),
      ),
      child: PersonalDetailTopBar(
        controller: widget.controller,
        customTimeStr: widget.customTimeStr,
        hasAll: widget.hasAll,
        onTap: (int val) async {
          if (val == 3 && widget.hasAll == true) {
            widget.onTimeFilter(null, null);
          } else if ((val == 0 && widget.hasAll == true) || (val == 0 && widget.hasAll == false)) {
            widget.onTimeFilter(
              Utils.getWeekStartDate().toString().substring(0, 10),
              Utils.getWeekEndDate().toString().substring(0, 10),
            );
          } else if ((val == 1 && widget.hasAll == true) || (val == 1 && widget.hasAll == false)) {
            widget.onTimeFilter(
              getMonthStartDate().toString().substring(0, 10),
              getMonthEndDate().toString().substring(0, 10),
            );
          } else {
            var dialogData = await showDialog<PickerDateRange?>(
                context: context,
                builder: (BuildContext context1) {
                  return Center(
                    child: Container(
                      color: Colors.white,
                      width: Utils.isPad() ? ScreenUtil().screenWidth / 2 : ScreenUtil().screenWidth / 1.3,
                      height: ScreenUtil().screenHeight / 2,
                      child: SfDateRangePicker(
                        showActionButtons: true,
                        confirmText: '确定',
                        cancelText: '取消',
                        onSubmit: (p0) {
                          print(p0);
                          Get.back(result: p0);
                          // Navigator.of(context1).pop(p0);
                          widget.refreshTime!(p0);
                        },
                        onCancel: () {
                          Get.back();
                          // Navigator.of(context1).pop();
                        },
                        selectionMode: DateRangePickerSelectionMode.range,
                        initialSelectedRange: customTimeState,
                      ),
                    ),
                  );
                });
            widget.onTimeFilter(
              dialogData?.startDate?.toString().substring(0, 10),
              dialogData?.endDate?.toString().substring(0, 10),
            );
            customTimeState = dialogData!;
          }
          /* switch (val) {
            case 0: // 全部
              widget.onTimeFilter(null, null);
              break;
            case 1: // 近一周
              widget.onTimeFilter(
                Utils.getWeekStartDate().toString().substring(0, 10),
                Utils.getWeekEndDate().toString().substring(0, 10),
              );
              break;
            case 2: // 近一个月
              widget.onTimeFilter(
                getMonthStartDate().toString().substring(0, 10),
                getMonthEndDate().toString().substring(0, 10),
              );
              break;
            default: // 自定义
              var dialogData = await showDialog<PickerDateRange?>(
                  context: context,
                  builder: (BuildContext context1) {
                    return Center(
                      child: Container(
                        color: Colors.white,
                        width: Utils.isPad() ? ScreenUtil().screenWidth / 2 : ScreenUtil().screenWidth / 1.3,
                        height: ScreenUtil().screenHeight / 2,
                        child: SfDateRangePicker(
                          showActionButtons: true,
                          confirmText: '确定',
                          cancelText: '取消',
                          onSubmit: (p0) {
                            print(p0);
                            Get.back(result: p0);
                            // Navigator.of(context1).pop(p0);
                            widget.refreshTime!(p0);
                          },
                          onCancel: () {
                            Get.back();
                            // Navigator.of(context1).pop();
                          },
                          selectionMode: DateRangePickerSelectionMode.range,
                          initialSelectedRange: customTimeState,
                        ),
                      ),
                    );
                  });
              widget.onTimeFilter(
                dialogData?.startDate?.toString().substring(0, 10),
                dialogData?.endDate?.toString().substring(0, 10),
              );
              customTimeState = dialogData!;
          }*/
        },
      ),
    );
  }
}
