import 'dart:typed_data';
import 'dart:ui';

import 'package:cs_resources/generated/assets.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:plugin_platform/engine/toast/toast_engine.dart';
import 'package:shared/utils/date_time_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:cs_resources/constants/color_constants.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_field.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/shatter/form_require_text.dart';
import 'package:widgets/widget_export.dart';

/**
 * 批量操作修改时间的弹窗
 */
class DialogAppliedButchModify extends StatefulWidget {
  DateTime? selectedStartDate;
  DateTime? selectedEndDate;
  void Function(DateTime? startTime, DateTime? endTime, String? subtractHour, String? statusId)? confirmAction;

  DialogAppliedButchModify({this.selectedStartDate, this.selectedEndDate, this.confirmAction});

  @override
  State<DialogAppliedButchModify> createState() => _DialogAppliedButchModifyState();
}

class _DialogAppliedButchModifyState extends State<DialogAppliedButchModify> {
  DateTime? selectedStartDate;
  DateTime? selectedEndDate;
  String? chooseStatusId;
  String? chooseStatusTxt;
  late TextEditingController hourController;
  late FocusNode hourFocusNode;
  List<String> statusIdList = ["1", "6"];
  List<String> statusTxtList = ["Applied", "No Show"];

  @override
  void initState() {
    super.initState();
    selectedStartDate = widget.selectedStartDate;
    selectedEndDate = widget.selectedEndDate;
    hourController = TextEditingController();
    hourFocusNode = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        //Title （如果使用 Container 为最外层容器则默认为 match_parent 的效果，除非我们限制宽度和最大高度最小高度）
        Container(
          padding: EdgeInsets.symmetric(horizontal: 16.5),
          width: double.infinity,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: const BorderRadius.all(Radius.circular(15)),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Center(
                child: MyTextView(
                  "Batch Modify".tr,
                  fontSize: 19,
                  isFontMedium: true,
                  textColor: ColorConstants.black,
                  marginTop: 22,
                  marginBottom: 20,
                ),
              ),

              MyTextView(
                "Job Start Time".tr,
                fontSize: 14,
                textColor: ColorConstants.black33,
                isFontRegular: true,
              ),

              //选择时间
              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),
                      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(
                "Job End Time".tr,
                fontSize: 14,
                marginTop: 11,
                textColor: ColorConstants.black33,
                isFontRegular: true,
              ),

              //选择结束日期
              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),
                      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();
              }),

              // + / - 小时
              MyTextView(
                "Subtract Hours".tr,
                fontSize: 14,
                marginTop: 11,
                textColor: ColorConstants.black33,
                isFontRegular: true,
              ),

              // 加减小时的输入框
              IgnoreKeyboardDismiss(
                child: MyTextField(
                  "hour",
                  "",
                  hintText: "Enter...".tr,
                  hintStyle: TextStyle(
                    color: ColorConstants.gray88,
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                  controller: hourController,
                  focusNode: hourFocusNode,
                  margin: EdgeInsets.only(left: 0, right: 0, top: 8),
                  showDivider: false,
                  fillBackgroundColor: ColorConstants.grayECECEC,
                  fillCornerRadius: 5,
                  padding: EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 0),
                  height: 40,
                  style: TextStyle(
                    color: ColorConstants.black33,
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                  inputType: TextInputType.numberWithOptions(signed: false, decimal: true),
                  textInputAction: TextInputAction.next,
                  enabled: true,
                  onSubmit: (key, value) {},
                  cursorColor: ColorConstants.black33,
                  showLeftIcon: false,
                  showRightIcon: false,
                ),
              ),

              // 修改状态
              MyTextView(
                "Status".tr,
                fontSize: 14,
                marginTop: 11,
                textColor: ColorConstants.black33,
                isFontRegular: true,
              ),

              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(
                      chooseStatusTxt ?? "",
                      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();
              }),

              //分割线
              Container(
                margin: EdgeInsets.only(top: 25),
                color: Color(0XFFCECECE),
                height: 0.5,
              ),
              Row(
                children: [
                  Expanded(
                      flex: 1,
                      child: InkWell(
                        onTap: () {
                          onCancel();
                        },
                        child: MyTextView(
                          "Cancel".tr,
                          fontSize: 17.5,
                          isFontMedium: true,
                          textAlign: TextAlign.center,
                          textColor: Color(0XFF0085C4),
                          cornerRadius: 3,
                          borderWidth: 1,
                        ),
                      )),
                  Container(
                    color: Color(0xff09141F).withOpacity(0.13),
                    width: 0.5,
                  ),
                  Expanded(
                      flex: 1,
                      child: InkWell(
                        onTap: () async {
                          String hour = hourController.text;
                          widget.confirmAction?.call(selectedStartDate, selectedEndDate, hour, chooseStatusId);
                          onCancel();
                        },
                        child: MyTextView(
                          "Submit".tr,
                          marginLeft: 10,
                          fontSize: 17.5,
                          isFontMedium: true,
                          textAlign: TextAlign.center,
                          textColor: Color(0XFF0085C4),
                          cornerRadius: 3,
                        ),
                      )),
                ],
              ).constrained(height: 46),
            ],
          ),
        ),
      ],
    ).constrained(width: 285);
  }

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

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

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

  /// 筛选状态
  void pickerStatus() {
    int selectedIndex;
    if (chooseStatusId == null) {
      selectedIndex = 0;
    } else {
      selectedIndex = statusIdList.indexWhere((e) => e == chooseStatusId);
    }

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

    OptionPickerUtil.showCupertinoOptionPicker(
      items: statusTxtList,
      initialSelectIndex: selectedIndex,
      onPickerChanged: (_, index) {
        chooseStatusId = statusIdList[index];

        setState(() {
          chooseStatusTxt = statusTxtList[index];
        });
      },
    );
  }
}
