import 'package:cs_resources/constants/color_constants.dart';
import 'package:cs_resources/generated/assets.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared/utils/date_time_utils.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_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/no_shadow_scroll_behavior.dart';
import 'package:widgets/shatter/form_require_text.dart';
import 'contract_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 'package:plugin_platform/platform_export.dart';

import 'contract_add_state.dart';

/*
 * 合同的添加与编辑
 */
class SGContractAddPage extends BaseStatelessPage<ContractAddController> {
  SGContractAddPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance(
    String? contractId,
    int type, //0 add  1 edit  2 renew
    void Function(dynamic value)? cb,
  ) {
    return Get.start(RouterPath.SGContractAdd, arguments: {'contractId': contractId, 'type': type, 'cb': cb});
  }

  late ContractAddState state;

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

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

  @override
  Widget buildWidget(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: MyAppBar.appBar(
            context,
            state.type == 0
                ? "Add Contract".tr
                : state.type == 1
                    ? "Edit Contract".tr
                    : "Renew Contract"),
        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),

                      //下拉选
                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10, left: 15, right: 15),
                        height: 48,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(state.type == 0 ? 0.2 : 0.5),
                          borderRadius: const BorderRadius.all(Radius.circular(5)),
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            MyTextView(
                              state.selectedAgencyTxt ?? "",
                              fontSize: 14,
                              hint: "Agency".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),

                            //下拉选图标
                            const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                          ],
                        ),
                      ).onTap(() {
                        if (state.type == 0) {
                          FocusScope.of(context).unfocus();
                          controller.pickAgency();
                        }
                      }),

                      //必填 - 合同开始时间
                      FormRequireText(text: "Contract Start Date".tr).marginOnly(left: 15, top: 15),

                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10, left: 15, right: 15),
                        height: 48,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(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, format: "yyyy-MM-dd"),
                              fontSize: 14,
                              hint: "Start Time".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),
                            //下拉选图标
                            const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                          ],
                        ),
                      ).onTap(() {
                        FocusScope.of(context).unfocus();
                        controller.pickStartTime();
                      }),

                      //必填 - 合同结束时间
                      FormRequireText(text: "Contract End Date".tr).marginOnly(left: 15, top: 15),

                      Container(
                        padding: const EdgeInsets.only(left: 16, right: 10),
                        margin: const EdgeInsets.only(top: 10, left: 15, right: 15),
                        height: 48,
                        decoration: BoxDecoration(
                          color: const Color(0xFF4DCFF6).withOpacity(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, format: "yyyy-MM-dd"),
                              fontSize: 14,
                              hint: "End Time".tr,
                              textHintColor: ColorConstants.textGrayAECAE5,
                              isFontMedium: true,
                              textColor: ColorConstants.white,
                            ).expanded(),
                            //下拉选图标
                            const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                          ],
                        ),
                      ).onTap(() {
                        FocusScope.of(context).unfocus();
                        controller.pickEndTime();
                      }),

                      //附件
                      MyTextView(
                        "Attachment".tr,
                        textColor: Colors.white,
                        fontSize: 14,
                        isFontRegular: true,
                        marginLeft: 15,
                        marginTop: 15,
                      ),

                      //九宫格图片选择
                      ImageNineGrid(
                        isSelectEnable: true,
                        maxImages: 1,
                        spacing: 15,
                        initialImages: state.selectedPaths,
                        onImagesChanged: (list) {
                          controller.setDocList(list);
                        },
                      ).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),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      );
    });
  }
}
