import 'package:cs_resources/constants/color_constants.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:domain/entity/response/labour_request_edit_index_entity.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/date_time_utils.dart';
import 'package:shared/utils/screen_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_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/no_shadow_scroll_behavior.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 'labour_request_add_controller.dart';
import 'labour_request_add_state.dart';

class UKLabourRequestAddPage extends BaseStatefulPage<LabourRequestAddController> {
  UKLabourRequestAddPage({Key? key}) : super(key: key);

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

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

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

class _LabourRequestAddState extends BaseState<UKLabourRequestAddPage, LabourRequestAddController> {
  late LabourRequestAddState state;

  @override
  void initState() {
    super.initState();
    state = controller.state;
    state.pageType = Get.arguments['pageType'];
    state.appliedId = Get.arguments['appliedId'];
  }

  @override
  Widget build(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: MyAppBar.appBar(
            context,
            state.pageType == 0
                ? "Add Labour Requisition".tr
                : state.pageType == 1
                    ? "Edit Labour Requisition".tr
                    : "Labour Requisition".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: "Job Title".tr,
                      ).marginOnly(top: 15),

                      //工作标题
                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10),
                        height: 45,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                          borderRadius: const BorderRadius.all(Radius.circular(5)),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            MyTextView(
                              state.selectedTemplateId == null || state.selectedTemplateId == "0"
                                  ? ""
                                  : state.labReqOption!.templateList
                                          .firstWhere((element) => element.value.toString() == state.selectedTemplateId,
                                              orElse: () => LabourRequestEditIndexTemplateList())
                                          .txt ??
                                      "",
                              fontSize: 14,
                              hint: "Choose Job Title".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),

                            //下拉选图标
                            Visibility(
                              visible: state.pageType != 2,
                              child: const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                            ),
                          ],
                        ),
                      ).onTap(() {
                        FocusScope.of(context).unfocus();
                        controller.pickJobTitle();
                      }),

                      //开始时间
                      FormRequireText(
                        text: "Start Time".tr,
                      ).marginOnly(top: 15),

                      //选择时间
                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10),
                        height: 45,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                          borderRadius: const BorderRadius.all(Radius.circular(5)),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            MyTextView(
                              state.selectedStartTime == null ? "" : DateTimeUtils.formatDate(state.selectedStartTime),
                              fontSize: 14,
                              hint: "Job Start Time".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),
                            //下拉选图标
                            Visibility(
                              visible: state.pageType != 2,
                              child: const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                            ),
                          ],
                        ),
                      ).onTap(() {
                        FocusScope.of(context).unfocus();
                        controller.pickStartTime();
                      }),

                      //结束时间
                      FormRequireText(
                        text: "End Time".tr,
                      ).marginOnly(top: 15),

                      //选择时间
                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10),
                        height: 45,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                          borderRadius: const BorderRadius.all(Radius.circular(5)),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            MyTextView(
                              state.selectedEndTime == null ? "" : DateTimeUtils.formatDate(state.selectedEndTime),
                              fontSize: 14,
                              hint: "Job End Time".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),
                            //下拉选图标
                            Visibility(
                              visible: state.pageType != 2,
                              child: const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                            ),
                          ],
                        ),
                      ).onTap(() {
                        FocusScope.of(context).unfocus();
                        controller.pickEndTime();
                      }),

                      //工作选择部门
                      FormRequireText(
                        text: "Outlet".tr,
                      ).marginOnly(top: 15),

                      //选择部门
                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10),
                        height: 45,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                          borderRadius: const BorderRadius.all(Radius.circular(5)),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            MyTextView(
                              state.selectedDepartmentId == null || state.selectedDepartmentId == "0"
                                  ? ""
                                  : state.labReqOption!.departmentList
                                          .firstWhere((element) => element.value.toString() == state.selectedDepartmentId,
                                              orElse: () => LabourRequestEditIndexDepartmentList())
                                          .txt ??
                                      "",
                              fontSize: 14,
                              hint: "Choose Outlet".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),
                            //下拉选图标
                            Visibility(
                              visible: state.pageType != 2,
                              child: const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                            ),
                          ],
                        ),
                      ).onTap(() {
                        FocusScope.of(context).unfocus();
                        controller.pickDepartment();
                      }),

                      //需要的人数
                      FormRequireText(
                        text: "No. of Staff".tr,
                      ).marginOnly(top: 15),

                      //输入框(只允许输入数字)
                      CustomTextField(
                        formKey: "no_of_staff",
                        marginLeft: 0,
                        marginRight: 0,
                        paddingTop: 0,
                        paddingBottom: 0,
                        height: 45,
                        fillBackgroundColor: const Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                        enabled: state.pageType != 2,
                        inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                        textInputType: TextInputType.number,
                        formData: state.formData,
                        textInputAction: TextInputAction.done,
                        onSubmit: (key, value) {
                          FocusScope.of(context).unfocus();
                        },
                        marginTop: 10,
                      ),

                      // FormRequireText(
                      //   text: "Salary By".tr,
                      // ).marginOnly(top: 15),
                      //
                      // //选择计费类型
                      // state.labReqOption != null
                      //     ? CustomRadioCheck(
                      //   options: state.labReqOption!.chargeList.map((e) => e.txt).whereType<String>().toList(), //后台返回的数据展示，并且根据后台的数据匹配索引
                      //   selectedPosition: state.labReqOption!.chargeList.indexWhere((element) => element.checked == "checked"),
                      //   onOptionSelected: (index, text) {
                      //     //修改内存的值
                      //     state.chargeOptionId = state.labReqOption!.chargeList[index].value;
                      //   },
                      // ).marginOnly(top: 15)
                      //     : CircularProgressIndicator(),

                      //选择是否需要输入金额
                      Visibility(
                        visible: state.labReqOption?.serviceType == 1,
                        child: FormRequireText(
                          text: "Amount".tr,
                        ).marginOnly(top: 15),
                      ),

                      Visibility(
                        visible: state.labReqOption?.serviceType == 1,
                        child: CustomTextField(
                          formKey: "amount",
                          marginLeft: 0,
                          marginRight: 0,
                          paddingTop: 0,
                          paddingBottom: 0,
                          height: 45,
                          fillBackgroundColor: const Color(0xFF4DCFF6).withOpacity(state.pageType == 2 ? 0.5 : 0.2),
                          enabled: state.pageType != 2,
                          textInputType: TextInputType.number,
                          formData: state.formData,
                          textInputAction: TextInputAction.done,
                          onSubmit: (key, value) {
                            FocusScope.of(context).unfocus();
                          },
                          marginTop: 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,
                        ).marginSymmetric(horizontal: 0, vertical: 30),
                      ),
                    ],
                  ).paddingOnly(left: 15, right: 15),
                ),
              ),
            ),
          ),
        ),
      );
    });
  }
}
