import 'dart:typed_data';
import 'dart:ui';
import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/s_g_labour_review_option_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 LabourReviewFilter extends StatefulWidget {
  void Function(DateTime? selectedStartDate, DateTime? selectedEndDate, String? outletId, String? sourceId)? onFilterAction;
  SGLabourReviewOptionEntity optionResult;
  DateTime? selectedStartDate;
  DateTime? selectedEndDate;
  String? outletId;
  String? sourceId;

  LabourReviewFilter({
    required this.optionResult,
    required this.selectedStartDate,
    required this.selectedEndDate,
    required this.outletId,
    required this.sourceId,
    this.onFilterAction,
  });

  @override
  State<LabourReviewFilter> createState() => _LabourReviewFilterState();
}

class _LabourReviewFilterState extends State<LabourReviewFilter> {
  DateTime? selectedStartDate;
  DateTime? selectedEndDate;
  String? selectedOutletId;
  String? selectedSourceId;

  @override
  void initState() {
    super.initState();
    this.selectedStartDate = widget.selectedStartDate;
    this.selectedEndDate = widget.selectedEndDate;
    this.selectedOutletId = widget.outletId;
    this.selectedSourceId = widget.sourceId;
  }

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

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

              //部门
              MyTextView(
                "Outlet".tr,
                fontSize: 14,
                marginTop: 11,
                isFontMedium: true,
                textColor: ColorConstants.black33,
              ),

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

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

              //选择时间
              Container(
                padding: const EdgeInsets.only(left: 16, right: 10),
                margin: const EdgeInsets.only(top: 10),
                height: 45,
                decoration: const BoxDecoration(
                  color: ColorConstants.grayECECEC,
                  borderRadius: 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(),
                    const 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: const EdgeInsets.only(left: 16, right: 10),
                margin: const EdgeInsets.only(top: 10),
                height: 45,
                decoration: const BoxDecoration(
                  color: ColorConstants.grayECECEC,
                  borderRadius: 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(),
                    const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                  ],
                ),
              ).onTap(() {
                pickerEndDate();
              }),

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

                      setState(() {
                        selectedStartDate = null;
                        selectedEndDate = null;
                        selectedOutletId = null;
                        selectedSourceId = null;
                      });
                    },
                    text: "Reset".tr,
                    textColor: ColorConstants.white,
                    backgroundColor: hexToColor("#FFBB1B"),
                    radius: 17.25,
                    minWidth: 60,
                    minHeight: 36,
                  ).expanded(),
                  const SizedBox(width: 15),
                  MyButton(
                    onPressed: () {
                      onCancel();
                      widget.onFilterAction?.call(selectedStartDate, selectedEndDate, selectedOutletId, selectedSourceId);
                    },
                    text: "Filter".tr,
                    textColor: ColorConstants.white,
                    backgroundColor: hexToColor("#0AC074"),
                    radius: 17.25,
                    minWidth: 60,
                    minHeight: 36,
                  ).expanded(),
                ],
              ).marginOnly(top: 20),
            ],
          ),
        ),
        Center(child: const 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 pickerOutlet() {
    int selectedDepartmentIndex;
    if (selectedOutletId == null) {
      selectedDepartmentIndex = 0;
    } else {
      selectedDepartmentIndex = widget.optionResult.outletList.indexWhere((department) => department.value.toString() == selectedOutletId);
    }

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

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

  /// 筛选Source
  void pickerSource() {
    int selectedIndex;
    if (selectedSourceId == null) {
      selectedIndex = 0;
    } else {
      selectedIndex = widget.optionResult.sourceList.indexWhere((department) => department.value.toString() == selectedSourceId);
    }

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

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