import 'package:cs_resources/constants/color_constants.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:plugin_basic/base/base_state.dart';
import 'package:plugin_basic/base/base_stateful_page.dart';
import 'package:plugin_basic/utils/ext_get_nav.dart';
import 'package:router/path/router_path.dart';
import 'package:shared/utils/screen_util.dart';
import 'package:shared/utils/util.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/my_appbar.dart';
import 'package:widgets/my_button.dart';

import 'package:widgets/no_shadow_scroll_behavior.dart';
import 'package:widgets/shatter/form_require_text.dart';
import 'package:widgets/shatter/round_my_text_field.dart';
import 'package:widgets/widget_export.dart';

import 'revise_add_edit_controller.dart';
import 'revise_add_edit_item.dart';
import 'revise_add_edit_state.dart';

/**
 * Revise数据的添加，编辑，详情的页面
 */
class ReviseAddEditPage extends BaseStatefulPage<ReviseAddEditController> {
  ReviseAddEditPage({Key? key}) : super(key: key);

  //启动当前页面,pageType 0 是新增  1是编辑  2是详情
  static void startInstance(int pageType, String? id, void Function(dynamic value)? cb) {
    return Get.start(RouterPath.reviseEditSG, arguments: {'pageType': pageType, 'id': id,'cb': cb});
  }

  @override
  ReviseAddEditController createRawController() {
    return ReviseAddEditController();
  }

  @override
  State<ReviseAddEditPage> createState() => _LabourRequestAddState();
}

class _LabourRequestAddState extends BaseState<ReviseAddEditPage, ReviseAddEditController> {
  late ReviseAddEditState state;

  @override
  void initState() {
    super.initState();
    state = controller.state;
    state.pageType = Get.arguments['pageType'];
    state.id = Get.arguments['id'];
    state.cb = Get.arguments['cb'] as void Function(dynamic)?;
  }

  @override
  Widget build(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: MyAppBar.appBar(
            context,
            state.pageType == 0
                ? "Add Revise".tr
                : state.pageType == 1
                    ? "Edit Revise".tr
                    : "Detail".tr),
        body: SafeArea(
        bottom: MediaQuery.of(context).padding.bottom > 38,
          top: false,
          child: Container(
            width: double.infinity,
            height: double.infinity,
            padding: EdgeInsets.only(top: kToolbarHeight + ScreenUtil.getStatusBarH(context) + 1),
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Color(0xFF091D44),
                  Color(0xFF245A8A),
                  Color(0xFF7F7CEC),
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
            child: Scrollbar(
              child: ScrollConfiguration(
                behavior: NoShadowScrollBehavior(),
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  physics: const BouncingScrollPhysics(),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ReviseAddEditItem(title: "Staff Name".tr, content: state.addEditEntity?.labourerName ?? ""),

                      ReviseAddEditItem(title: "Job Date".tr, content: state.addEditEntity?.jobDate ?? ""),

                      ReviseAddEditItem(title: "Job Start Time".tr, content: state.addEditEntity?.startTime ?? ""),

                      ReviseAddEditItem(title: "Job End Time".tr, content: state.addEditEntity?.endTime ?? ""),

                      ReviseAddEditItem(title: "Security Clock In".tr, content: state.addEditEntity?.securityIn ?? ""),

                      ReviseAddEditItem(title: "Security Clock Out".tr, content: state.addEditEntity?.securityOut ?? ""),

                      ReviseAddEditItem(title: "Work Clock In".tr, content: state.addEditEntity?.workIn ?? ""),

                      ReviseAddEditItem(title: "Work Clock Out".tr, content: state.addEditEntity?.workOut ?? ""),

                      ReviseAddEditItem(title: "+/- Hours".tr, content: state.addEditEntity?.adjustHours ?? ""),

                      ReviseAddEditItem(title: "Total Hours".tr, content: state.addEditEntity?.totalHours ?? ""),

                      //修改的小时
                      FormRequireText(
                        fontSize: 15,
                        text: "Revise Hours".tr,
                      ).marginOnly(top: 15),

                      //输入框
                      CustomTextField(
                        formKey: "revise_hour",
                        marginLeft: 0,
                        marginRight: 0,
                        paddingTop: 0,
                        paddingBottom: 0,
                        height: 45,
                        fillBackgroundColor: Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                        enabled: state.pageType != 2,
                        textInputType: TextInputType.number,
                        formData: state.formData,
                        textInputAction: TextInputAction.next,
                        onSubmit: (key, value) {
                          state.formData[key]!['focusNode'].unfocus();
                          FocusScope.of(context).requestFocus(state.formData['reason']!['focusNode']);
                        },
                        marginTop: 10,
                      ),

                      //修改的原因
                      FormRequireText(
                        fontSize: 15,
                        text: "Reason".tr,
                      ).marginOnly(top: 15),

                      //输入框
                      IgnoreKeyboardDismiss(
                        child: Container(
                          height: 160,
                          padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
                          decoration: BoxDecoration(
                            color: Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                            borderRadius: const BorderRadius.all(Radius.circular(5)),
                          ),
                          child: TextField(
                            enabled: state.pageType != 2,
                            cursorColor: ColorConstants.white,
                            cursorWidth: 1.5,
                            autofocus: false,
                            controller: state.formData["reason"]!['controller'],
                            focusNode: state.formData["reason"]!['focusNode'],
                            obscureText: state.formData["reason"]!['obsecure'],
                            // 装饰
                            decoration: InputDecoration(
                              isDense: true,
                              isCollapsed: true,
                              border: InputBorder.none,
                              hintText: state.formData["reason"]!['hintText'],
                              hintStyle: TextStyle(
                                color: ColorConstants.textGrayAECAE5,
                                fontSize: 15.0,
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                            style: TextStyle(
                              color: ColorConstants.white,
                              fontSize: 15.0,
                              fontWeight: FontWeight.w400,
                            ),
                            // 键盘动作右下角图标
                            textInputAction: TextInputAction.done,
                            onSubmitted: (value) {
                              FocusScope.of(context).unfocus();
                              controller.doSubmit();
                            },
                          ),
                        ),
                      ).marginOnly(top: 10),

                      //提交按钮
                      Visibility(
                        visible: state.pageType != 2,
                        child: MyButton(
                          type: ClickType.throttle,
                          milliseconds: 500,
                          onPressed: () {
                            FocusScope.of(context).unfocus();
                            controller.doSubmit();
                          },
                          text: "Submit".tr,
                          textColor: ColorConstants.white,
                          fontSize: 16,
                          radius: 22.5,
                          backgroundColor: hexToColor("#FFBB1B"),
                          fontWeight: FontWeight.w500,
                        ).marginOnly(top: 20),
                      ),

                      SizedBox(height: 20),
                    ],
                  ).paddingOnly(left: 15, right: 15),
                ),
              ),
            ),
          ),
        ),
      );
    });
  }
}
