import 'dart:ui';

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/util.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:cs_resources/constants/color_constants.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/widget_export.dart';

/*
 * 拒绝的弹窗
 */
class AttendaceReviewRejectDialog extends StatefulWidget {

  void Function(String reason)? confirmAction;

  AttendaceReviewRejectDialog({this.confirmAction});

  @override
  State<AttendaceReviewRejectDialog> createState() => _AttendaceReviewRejectDialogState();
}

class _AttendaceReviewRejectDialogState extends State<AttendaceReviewRejectDialog> {

  late TextEditingController _controller;
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _focusNode = FocusNode();
  }

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

              IgnoreKeyboardDismiss(
                child: Container(
                  height: 130,
                  margin: const EdgeInsets.symmetric(vertical: 19, horizontal: 22),
                  padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                  decoration: BoxDecoration(
                    color: const Color(0xFFF0F0F0),
                    border: Border.all(
                      color: const Color(0xFFD8D8D8),
                      width: 0.5,
                    ),
                  ),
                  child: TextField(
                    cursorColor: ColorConstants.black66,
                    cursorWidth: 1.5,
                    autofocus: false,
                    enabled: true,
                    focusNode: _focusNode,
                    controller: _controller,
                    // 装饰
                    decoration: InputDecoration(
                      isDense: true,
                      isCollapsed: true,
                      border: InputBorder.none,
                      hintText: "Enter...".tr,
                      hintStyle: const TextStyle(
                        color: ColorConstants.black66,
                        fontSize: 15.0,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                    style: const TextStyle(
                      color: ColorConstants.black,
                      fontSize: 15.0,
                      fontWeight: FontWeight.w400,
                    ),
                    // 键盘动作右下角图标
                    textInputAction: TextInputAction.done,
                    onSubmitted: (value) {
                      doCallbackAction();
                    },
                  ),
                ),
              ),

              // 分割线
              Container(
                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: () {
                          doCallbackAction();
                        },
                        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 onCancel() async {
    SmartDialog.dismiss();
  }

  //执行回调
  void doCallbackAction() {
    _focusNode.unfocus();

    final content = _controller.text.toString();

    if (Utils.isEmpty(content)) {
      ToastEngine.show("Please Enter Reason".tr);
      return;
    }

    onCancel();

    widget.confirmAction?.call(content);
  }
}
