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

import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/job_applied_edit_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/util.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/shatter/custom_radio_check.dart';
import 'package:widgets/widget_export.dart';

/*
 * 修改员工的信息的弹窗
 */
class DialogAppliedModify extends StatefulWidget {
  JobAppliedEditIndexSGEntity? editIndexOption;

  void Function(JobAppliedEditIndexSGEntity? entity)? confirmAction;

  DialogAppliedModify({this.editIndexOption, this.confirmAction});

  @override
  State<DialogAppliedModify> createState() => _DialogAppliedModifyState();
}

class _DialogAppliedModifyState extends State<DialogAppliedModify> {
  JobAppliedEditIndexSGEntity? editIndexOption;
  late TextEditingController hourController;
  late FocusNode hourFocusNode;

  @override
  void initState() {
    super.initState();
    editIndexOption = widget.editIndexOption;
    hourController = TextEditingController();
    if (Utils.isNotEmpty(editIndexOption?.adjustHours) && editIndexOption?.adjustHours != "0.00") {
      hourController.text = editIndexOption!.adjustHours!;
    }
    hourFocusNode = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16.5),
      width: 285,
      height: 510,
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(15)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: [
          Center(
            // 标题
            child: MyTextView(
              "Modify".tr,
              fontSize: 19,
              isFontMedium: true,
              textColor: ColorConstants.black,
              marginTop: 22,
              marginBottom: 20,
            ),
          ),

          SingleChildScrollView(
            scrollDirection: Axis.vertical,
            physics: const BouncingScrollPhysics(),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                MyTextView(
                  "Job Start Time".tr,
                  fontSize: 14,
                  textColor: ColorConstants.black33,
                  isFontRegular: true,
                ),

                //选择时间
                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(
                        editIndexOption == null ? "" : editIndexOption?.startTime ?? "",
                        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(
                  "Security Clock In".tr,
                  fontSize: 14,
                  marginTop: 11,
                  textColor: ColorConstants.black33,
                  isFontRegular: true,
                ),

                //选择结束日期
                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(
                        editIndexOption == null ? "" : editIndexOption?.securityIn ?? "",
                        fontSize: 14,
                        hint: "Choose Date".tr,
                        textHintColor: ColorConstants.textBlackHint,
                        isFontMedium: true,
                        textColor: ColorConstants.black33,
                      ).expanded(),
                      const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                    ],
                  ),
                ).onTap(() {
                  pickerSecurityIn();
                }),

                // 工作地签到
                MyTextView(
                  "Work Clock In".tr,
                  fontSize: 14,
                  marginTop: 11,
                  textColor: ColorConstants.black33,
                  isFontRegular: true,
                ),

                //选择结束日期
                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(
                        editIndexOption == null ? "" : editIndexOption?.workIn ?? "",
                        fontSize: 14,
                        hint: "Choose Date".tr,
                        textHintColor: ColorConstants.textBlackHint,
                        isFontMedium: true,
                        textColor: ColorConstants.black33,
                      ).expanded(),
                      const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                    ],
                  ),
                ).onTap(() {
                  pickerWorkIn();
                }),

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

                //选择结束日期
                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(
                        editIndexOption == null ? "" : editIndexOption?.endTime ?? "",
                        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();
                }),

                // 工作地签出
                MyTextView(
                  "Work Clock Out".tr,
                  fontSize: 14,
                  marginTop: 11,
                  textColor: ColorConstants.black33,
                  isFontRegular: true,
                ),

                //选择结束日期
                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(
                        editIndexOption == null ? "" : editIndexOption?.workOut ?? "",
                        fontSize: 14,
                        hint: "Choose Date".tr,
                        textHintColor: ColorConstants.textBlackHint,
                        isFontMedium: true,
                        textColor: ColorConstants.black33,
                      ).expanded(),
                      const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                    ],
                  ),
                ).onTap(() {
                  pickerWorkOut();
                }),

                // 门卫签出
                MyTextView(
                  "Security Clock Out".tr,
                  fontSize: 14,
                  marginTop: 11,
                  textColor: ColorConstants.black33,
                  isFontRegular: true,
                ),

                //选择结束日期
                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(
                        editIndexOption == null ? "" : editIndexOption?.securityOut ?? "",
                        fontSize: 14,
                        hint: "Choose Date".tr,
                        textHintColor: ColorConstants.textBlackHint,
                        isFontMedium: true,
                        textColor: ColorConstants.black33,
                      ).expanded(),
                      const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                    ],
                  ),
                ).onTap(() {
                  pickerSecurityOut();
                }),

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

                // 加减小时的输入框
                IgnoreKeyboardDismiss(
                  child: MyTextField(
                    "hour",
                    Utils.isNotEmpty(editIndexOption?.adjustHours) && editIndexOption?.adjustHours != "0.00" ? editIndexOption!.adjustHours! : "",
                    hintText: "Enter...".tr,
                    hintStyle: const TextStyle(
                      color: ColorConstants.gray88,
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                    ),
                    controller: hourController,
                    focusNode: hourFocusNode,
                    margin: const EdgeInsets.only(left: 0, right: 0, top: 8),
                    showDivider: false,
                    fillBackgroundColor: ColorConstants.grayECECEC,
                    fillCornerRadius: 5,
                    padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 0),
                    height: 40,
                    style: const TextStyle(
                      color: ColorConstants.black33,
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                    ),
                    inputType: const TextInputType.numberWithOptions(signed: true, 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,
                ),

                //状态单选
                CustomRadioCheck(
                  options: editIndexOption?.statusList?.map((e) => e.txt!).toList() ?? [],
                  onOptionSelected: (index, text) {
                    editIndexOption?.status = editIndexOption?.statusList?[index].value ?? 0;
                  },
                  textColor: ColorConstants.black33,
                  selectedPosition: editIndexOption?.statusList?.indexWhere((e) => e.checked == "checked") ?? -1,
                ).marginOnly(left: 15, right: 15, top: 10),
              ],
            ),
          ).expanded(),

          //分割线
          Container(
            margin: const EdgeInsets.only(top: 25),
            color: const 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: const Color(0XFF0085C4),
                      cornerRadius: 3,
                      borderWidth: 1,
                    ),
                  )),
              Container(
                color: const Color(0xff09141F).withOpacity(0.13),
                width: 0.5,
              ),
              Expanded(
                  flex: 1,
                  child: InkWell(
                    onTap: () async {
                      //输入框需要手动赋值
                      String hour = hourController.text;
                      editIndexOption?.adjustHours = hour;

                      widget.confirmAction?.call(editIndexOption);
                      onCancel();
                    },
                    child: MyTextView(
                      "Submit".tr,
                      marginLeft: 10,
                      fontSize: 17.5,
                      isFontMedium: true,
                      textAlign: TextAlign.center,
                      textColor: const Color(0XFF0085C4),
                      cornerRadius: 3,
                    ),
                  )),
            ],
          ).constrained(height: 46),
        ],
      ),
    );
  }

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

  /// 筛选开始日期
  void pickerStartDate() {
    if (editIndexOption == null) return;

    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: DateTimeUtils.getDateTime(editIndexOption!.startTime ?? ""),
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          editIndexOption?.startTime = DateTimeUtils.formatDate(date);
        });
      },
      title: "Start Date".tr,
    );
  }

  /// 筛选结束日期
  void pickerEndDate() {
    if (editIndexOption == null) return;

    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: DateTimeUtils.getDateTime(editIndexOption!.endTime ?? ""),
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          editIndexOption?.endTime = DateTimeUtils.formatDate(date);
        });
      },
      title: "End Date".tr,
    );
  }

  /// 选择工作地签到
  void pickerWorkIn() {
    if (editIndexOption == null) return;

    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: Utils.isNotEmpty(editIndexOption!.workIn)
          ? DateTimeUtils.getDateTime(editIndexOption!.workIn ?? "")
          : DateTimeUtils.getDateTime(editIndexOption!.startTime ?? ""),
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          editIndexOption?.workIn = DateTimeUtils.formatDate(date);
        });
      },
      title: "Work Clock In".tr,
    );
  }

  /// 选择工作地签出
  void pickerWorkOut() {
    if (editIndexOption == null) return;

    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: Utils.isNotEmpty(editIndexOption!.workOut)
          ? DateTimeUtils.getDateTime(editIndexOption!.workOut ?? "")
          : DateTimeUtils.getDateTime(editIndexOption!.endTime ?? ""),
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          editIndexOption?.workOut = DateTimeUtils.formatDate(date);
        });
      },
      title: "Work Clock Out".tr,
    );
  }

  /// 选择门卫签到
  void pickerSecurityIn() {
    if (editIndexOption == null) return;

    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: Utils.isNotEmpty(editIndexOption!.securityIn)
          ? DateTimeUtils.getDateTime(editIndexOption!.securityIn ?? "")
          : DateTimeUtils.getDateTime(editIndexOption!.startTime ?? ""),
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          editIndexOption?.securityIn = DateTimeUtils.formatDate(date);
        });
      },
      title: "Security Clock In".tr,
    );
  }

  /// 选择门卫签出
  void pickerSecurityOut() {
    if (editIndexOption == null) return;
    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: Utils.isNotEmpty(editIndexOption!.securityOut)
          ? DateTimeUtils.getDateTime(editIndexOption!.securityOut ?? "")
          : DateTimeUtils.getDateTime(editIndexOption!.endTime ?? ""),
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          editIndexOption?.securityOut = DateTimeUtils.formatDate(date);
        });
      },
      title: "Security Clock Out".tr,
    );
  }
}
