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_view.dart';
import 'package:widgets/picker/date_picker_util.dart';
import 'package:widgets/shatter/form_require_text.dart';
import 'package:widgets/widget_export.dart';

/*
 * 门卫签到的编辑弹窗
 */
class SecurityEditDialog extends StatefulWidget {
  DateTime? startTime;
  DateTime? endTime;
  DateTime? securityInTime;
  DateTime? securityOutTime;
  String? staffName;
  void Function(DateTime? securityInTime, DateTime? securityOutTime)? confirmAction;

  SecurityEditDialog({this.startTime, this.endTime, this.securityInTime, this.securityOutTime, this.staffName, this.confirmAction});

  @override
  State<SecurityEditDialog> createState() => _AppliedButchModifyState();
}

class _AppliedButchModifyState extends State<SecurityEditDialog> {
  DateTime? startTime;
  DateTime? endTime;
  DateTime? securityInTime;
  DateTime? securityOutTime;
  String? staffName;

  @override
  void initState() {
    super.initState();
    startTime = widget.startTime;
    endTime = widget.endTime;
    securityInTime = widget.securityInTime;
    securityOutTime = widget.securityOutTime;
    staffName = widget.staffName;
  }

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

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

              //工作开始时间
              Container(
                padding: const EdgeInsets.only(left: 16, right: 10),
                margin: const EdgeInsets.only(top: 8),
                height: 45,
                decoration: BoxDecoration(
                  color: hexToColor("#DCDCDC"),
                  borderRadius: const BorderRadius.all(Radius.circular(2.5)),
                  border: Border.all(
                    color: hexToColor("#D8D8D8"),
                    width: 0.5,
                  ),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      startTime == null ? "-" : DateTimeUtils.formatDate(startTime),
                      fontSize: 14,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontRegular: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                  ],
                ),
              ),

              // 门卫签到
              MyTextView(
                "Security In".tr,
                marginTop: 8,
                textColor: ColorConstants.black33,
                fontSize: 14,
                isFontMedium: true,
              ),

              //选择门卫签到时间
              Container(
                padding: const EdgeInsets.only(left: 16, right: 10),
                margin: const EdgeInsets.only(top: 8),
                height: 45,
                decoration: BoxDecoration(
                  color: hexToColor("#F0F0F0"),
                  borderRadius: const BorderRadius.all(Radius.circular(2.5)),
                  border: Border.all(
                    color: hexToColor("#D8D8D8"),
                    width: 0.5,
                  ),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      securityInTime == null ? "-" : DateTimeUtils.formatDate(securityInTime),
                      fontSize: 14,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontRegular: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                    const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                  ],
                ),
              ).onTap(() {
                pickSecurityInDate();
              }),

              //结束时间
              MyTextView(
                "End Time".tr,
                marginTop: 8,
                textColor: ColorConstants.black33,
                fontSize: 14,
                isFontMedium: true,
              ),

              //工作结束时间
              Container(
                padding: const EdgeInsets.only(left: 16, right: 10),
                margin: const EdgeInsets.only(top: 8),
                height: 45,
                decoration: BoxDecoration(
                  color: hexToColor("#DCDCDC"),
                  borderRadius: const BorderRadius.all(Radius.circular(2.5)),
                  border: Border.all(
                    color: hexToColor("#D8D8D8"),
                    width: 0.5,
                  ),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      endTime == null ? "-" : DateTimeUtils.formatDate(endTime),
                      fontSize: 14,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontRegular: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                  ],
                ),
              ),

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

              //选择门卫签出时间
              Container(
                padding: const EdgeInsets.only(left: 16, right: 10),
                margin: const EdgeInsets.only(top: 8),
                height: 45,
                decoration: BoxDecoration(
                  color: hexToColor("#F0F0F0"),
                  borderRadius: const BorderRadius.all(Radius.circular(2.5)),
                  border: Border.all(
                    color: hexToColor("#D8D8D8"),
                    width: 0.5,
                  ),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    MyTextView(
                      securityOutTime == null ? "-" : DateTimeUtils.formatDate(securityOutTime),
                      fontSize: 14,
                      textHintColor: ColorConstants.textBlackHint,
                      isFontRegular: true,
                      textColor: ColorConstants.black33,
                    ).expanded(),
                    const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                  ],
                ),
              ).onTap(() {
                pickSecurityOutDate();
              }),

              // 按钮组
              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 {
                          widget.confirmAction?.call(securityInTime, securityOutTime);
                          onCancel();
                        },
                        child: MyTextView(
                          "Submit".tr,
                          marginLeft: 10,
                          fontSize: 17.5,
                          isFontMedium: true,
                          textAlign: TextAlign.center,
                          textColor: const Color(0XFF0085C4),
                          cornerRadius: 3,
                        ),
                      )),
                ],
              ).constrained(height: 46),
            ],
          ),
        ),
      ],
    ).constrained(width: 285);
  }

  /// 选择门卫签到日期
  void pickSecurityInDate() {
    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: securityInTime,
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          securityInTime = date;
        });
      },
      title: "Security In".tr,
    );
  }

  /// 选择门卫签出日期
  void pickSecurityOutDate() {
    DatePickerUtil.showCupertinoDatePicker(
      selectedDateTime: securityOutTime ?? securityInTime,
      mode: CupertinoDatePickerMode.dateAndTime,
      onDateTimeChanged: (date) {
        setState(() {
          securityOutTime = date;
        });
      },
      title: "Security Out".tr,
    );
  }

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