import 'dart:ui';
import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/revise_index_s_g_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:shared/utils/date_time_utils.dart';
import 'package:shared/utils/screen_util.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:cs_resources/constants/color_constants.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/picker/date_picker_util.dart';
import 'package:widgets/picker/option_pick_util.dart';
import 'package:widgets/widget_export.dart';

/**
 * 用工请求列表的筛选
 */
class ReviseListFilter extends StatefulWidget {
  void Function(DateTime? selectedStartDate, DateTime? selectedEndDate, String? selectedStatusId)? onFilterAction;
  ReviseIndexSGEntity optionResult;
  DateTime? selectedStartDate;
  DateTime? selectedEndDate;
  String? selectedStatusId;

  ReviseListFilter({
    required this.optionResult,
    required this.selectedStartDate,
    required this.selectedEndDate,
    required this.selectedStatusId,
    this.onFilterAction,
  });

  @override
  State<ReviseListFilter> createState() => _ReviseListFilterState();
}

class _ReviseListFilterState extends State<ReviseListFilter> {
  DateTime? selectedStartDate;
  DateTime? selectedEndDate;
  String? selectedStatusId;

  @override
  void initState() {
    super.initState();
    this.selectedStartDate = widget.selectedStartDate;
    this.selectedEndDate = widget.selectedEndDate;
    this.selectedStatusId = widget.selectedStatusId;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        SizedBox(
          height: kToolbarHeight + ScreenUtil.getStatusBarH(context) + 1,
        ),
        Container(
          padding: EdgeInsets.only(left: 15, right: 15, top: 17.5, bottom: 20),
          width: double.infinity,
          decoration: BoxDecoration(
            color: Colors.white,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              //状态
              MyTextView(
                "Status".tr,
                fontSize: 14,
                isFontMedium: true,
                textColor: ColorConstants.black33,
              ),

              //选择状态
              Container(
                padding: EdgeInsets.only(left: 16, right: 10),
                margin: EdgeInsets.only(top: 10),
                height: 45,
                decoration: BoxDecoration(
                  color: ColorConstants.grayECECEC,
                  borderRadius: const BorderRadius.all(Radius.circular(5)),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      selectedStatusId == null || selectedStatusId == "0"
                          ? ""
                          : widget.optionResult.statusList.firstWhere((element) => element.value == selectedStatusId).txt!,
                      fontSize: 14,
                      hint: "Choose Status".tr,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontMedium: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                    MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                  ],
                ),
              ).onTap(() {
                pickerStatus();
              }),

              //开始时间
              MyTextView(
                "Start Date".tr,
                fontSize: 14,
                isFontMedium: true,
                marginTop: 11,
                textColor: ColorConstants.black33,
              ),

              //选择时间
              Container(
                padding: EdgeInsets.only(left: 16, right: 10),
                margin: EdgeInsets.only(top: 10),
                height: 45,
                decoration: BoxDecoration(
                  color: ColorConstants.grayECECEC,
                  borderRadius: const BorderRadius.all(Radius.circular(5)),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      selectedStartDate == null ? "" : DateTimeUtils.formatDate(selectedStartDate, format: "yyyy-MM-dd"),
                      fontSize: 14,
                      hint: "Choose Start Date".tr,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontMedium: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                    MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                  ],
                ),
              ).onTap(() {
                pickerStartDate();
              }),

              //结束日期
              MyTextView(
                "End Date".tr,
                fontSize: 14,
                marginTop: 11,
                isFontMedium: true,
                textColor: ColorConstants.black33,
              ),

              //选择结束日期
              Container(
                padding: EdgeInsets.only(left: 16, right: 10),
                margin: EdgeInsets.only(top: 10),
                height: 45,
                decoration: BoxDecoration(
                  color: ColorConstants.grayECECEC,
                  borderRadius: const BorderRadius.all(Radius.circular(5)),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      selectedEndDate == null ? "" : DateTimeUtils.formatDate(selectedEndDate, format: "yyyy-MM-dd"),
                      fontSize: 14,
                      hint: "Choose End Date".tr,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontMedium: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                    MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                  ],
                ),
              ).onTap(() {
                pickerEndDate();
              }),

              //按钮组
              Row(
                children: [
                  MyButton(
                    onPressed: () {
                      //只是Reset当前的弹窗筛选选项
                      widget.selectedStartDate = null;
                      widget.selectedEndDate = null;
                      widget.selectedStatusId = null;

                      setState(() {
                        selectedStartDate = null;
                        selectedEndDate = null;
                        selectedStatusId = null;
                      });
                    },
                    text: "Reset".tr,
                    textColor: ColorConstants.white,
                    backgroundColor: hexToColor("#FFBB1B"),
                    radius: 17.25,
                    minWidth: 60,
                    minHeight: 36,
                  ).expanded(),
                  SizedBox(width: 15),
                  MyButton(
                    onPressed: () {
                      onCancel();
                      widget.onFilterAction?.call(selectedStartDate, selectedEndDate, selectedStatusId);
                    },
                    text: "Filter".tr,
                    textColor: ColorConstants.white,
                    backgroundColor: hexToColor("#0AC074"),
                    radius: 17.25,
                    minWidth: 60,
                    minHeight: 36,
                  ).expanded(),
                ],
              ).marginOnly(top: 20),
            ],
          ),
        ),
        Center(child: MyAssetImage(Assets.baseServiceDialogDeleteIcon, width: 26.5, height: 26.5).marginOnly(top: 35)).onTap(() {
          onCancel();
        }),
      ],
    );
  }

  //取消弹框
  void onCancel() async {
    SmartDialog.dismiss();
  }

  /// 筛选开始日期
  void pickerStartDate() {
    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: selectedStartDate,
      onDateTimeChanged: (date) {
        setState(() {
          selectedStartDate = date;
        });
      },
      title: "Start Date".tr,
    );
  }

  /// 筛选结束日期
  void pickerEndDate() {
    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: selectedEndDate ?? selectedStartDate,
      onDateTimeChanged: (date) {
        setState(() {
          selectedEndDate = date;
        });
      },
      title: "End Date".tr,
    );
  }

  /// 筛选状态
  void pickerStatus() {
    int selectedStatusIndex;
    if (selectedStatusId == null) {
      selectedStatusIndex = 0;
    } else {
      selectedStatusIndex = widget.optionResult.statusList!.indexWhere((department) => department.value == selectedStatusId);
    }

    if (selectedStatusIndex < 0) {
      selectedStatusIndex = 0;
    }

    OptionPickerUtil.showCupertinoOptionPicker(
      items: widget.optionResult.statusList.map((e) => e.txt!).toList(growable: false),
      initialSelectIndex: selectedStatusIndex,
      onPickerChanged: (_, index) {
        setState(() {
          selectedStatusId = widget.optionResult.statusList[index].value;
        });
      },
    );
  }
}
