import 'package:cs_resources/constants/color_constants.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared/utils/log_utils.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/my_text_view.dart';
import 'package:widgets/no_shadow_scroll_behavior.dart';
import 'package:widgets/shatter/custom_check_box.dart';
import 'package:widgets/shatter/custom_radio_check.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 'agency_add_controller.dart';

import 'package:plugin_basic/base/base_stateless_page.dart';
import 'package:plugin_basic/utils/ext_get_nav.dart';
import 'package:router/path/router_path.dart';

import 'agency_add_state.dart';

/*
 * 中介的添加与编辑
 */
class SGAgencyAddPage extends BaseStatelessPage<AgencyAddController> {
  SGAgencyAddPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance(
    String agentId,
    void Function(dynamic value)? cb,
  ) {
    return Get.start(RouterPath.SGAgencyAdd, arguments: {'agentId': agentId, 'cb': cb});
  }

  late AgencyAddState state;

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

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

  @override
  Widget buildWidget(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: MyAppBar.appBar(context, Utils.isEmpty(state.agentId) ? "Add Agency".tr : "Edit Agency".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: [
                      //必填 - 中介名称
                      FormRequireText(text: "Agency Name".tr).marginOnly(left: 15, top: 19),

                      CustomTextField(
                        formKey: "agency_name",
                        formData: state.formData,
                        height: 46,
                        fontSize: 14,
                        onSubmit: (key, value) {
                          state.formData[key]!['focusNode'].unfocus();
                          FocusScope.of(context).requestFocus(state.formData['acra']!['focusNode']);
                        },
                        marginTop: 10,
                      ),

                      //必填 - 中介商家编码
                      FormRequireText(text: "ACRA NO".tr).marginOnly(left: 15, top: 15),

                      CustomTextField(
                        formKey: "acra",
                        formData: state.formData,
                        height: 46,
                        fontSize: 14,
                        onSubmit: (key, value) {
                          state.formData[key]!['focusNode'].unfocus();
                          FocusScope.of(context).requestFocus(state.formData['person_in_charge']!['focusNode']);
                        },
                        marginTop: 10,
                      ),

                      // 负责人
                      MyTextView(
                        "Person in Charge".tr,
                        textColor: Colors.white,
                        fontSize: 14,
                        isFontRegular: true,
                        marginLeft: 15,
                        marginTop: 15,
                      ),

                      // 输入框
                      CustomTextField(
                        formKey: "person_in_charge",
                        formData: state.formData,
                        height: 46,
                        fontSize: 14,
                        onSubmit: (key, value) {
                          state.formData[key]!['focusNode'].unfocus();
                          FocusScope.of(context).requestFocus(state.formData['address']!['focusNode']);
                        },
                        marginTop: 10,
                      ),

                      // 公司地址
                      MyTextView(
                        "Company Address".tr,
                        textColor: Colors.white,
                        fontSize: 14,
                        isFontRegular: true,
                        marginLeft: 15,
                        marginTop: 15,
                      ),

                      // 输入框
                      CustomTextField(
                        formKey: "address",
                        formData: state.formData,
                        height: 46,
                        fontSize: 14,
                        onSubmit: (key, value) {
                          state.formData[key]!['focusNode'].unfocus();
                          FocusScope.of(context).requestFocus(state.formData['email']!['focusNode']);
                        },
                        marginTop: 10,
                      ),

                      // 公司邮箱
                      MyTextView(
                        "Contact Email".tr,
                        textColor: Colors.white,
                        fontSize: 14,
                        isFontRegular: true,
                        marginLeft: 15,
                        marginTop: 15,
                      ),

                      // 输入框
                      CustomTextField(
                        formKey: "email",
                        formData: state.formData,
                        height: 46,
                        fontSize: 14,
                        textInputType: TextInputType.emailAddress,
                        onSubmit: (key, value) {
                          state.formData[key]!['focusNode'].unfocus();
                          FocusScope.of(context).requestFocus(state.formData['phone']!['focusNode']);
                        },
                        marginTop: 10,
                      ),

                      // 公司电话
                      MyTextView(
                        "Contact NO".tr,
                        textColor: Colors.white,
                        fontSize: 14,
                        isFontRegular: true,
                        marginLeft: 15,
                        marginTop: 15,
                      ),

                      // 输入框
                      CustomTextField(
                        formKey: "phone",
                        formData: state.formData,
                        height: 46,
                        fontSize: 14,
                        textInputType: TextInputType.phone,
                        textInputAction: TextInputAction.done,
                        onSubmit: (key, value) {
                          FocusScope.of(context).unfocus();
                          controller.doSubmit();
                        },
                        marginTop: 10,
                      ),

                      //修改状态
                      Visibility(
                        visible: Utils.isNotEmpty(state.agentId),
                        child: MyTextView(
                          "Status".tr,
                          textColor: Colors.white,
                          fontSize: 14,
                          isFontRegular: true,
                          marginLeft: 15,
                          marginTop: 15,
                        ),
                      ),

                      //单选
                      Visibility(
                        visible: state.statusOption!= null && state.statusOption!.isNotEmpty,
                        child: CustomRadioCheck(
                          options: state.statusOption?.map((e) => e.txt!).toList() ?? [],
                          onOptionSelected: (index, text) {
                            state.statusId = state.statusOption![index].value;
                          },
                          selectedPosition: state.statusOption?.indexWhere((e) => e.checked == "checked") ?? -1,
                        ).marginOnly(left: 15, right: 15, top: 10),
                      ),

                      //提交按钮
                      MyButton(
                        type: ClickType.throttle,
                        milliseconds: 500,
                        onPressed: () {
                          FocusScope.of(context).unfocus();
                          controller.doSubmit();
                        },
                        text: "Submit".tr,
                        textColor: ColorConstants.white,
                        fontSize: 16,
                        radius: 20,
                        backgroundColor: ColorConstants.textYellowFFBB1B,
                        fontWeight: FontWeight.w500,
                      ).marginSymmetric(vertical: 25, horizontal: 15),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      );
    });
  }
}
