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: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:widgets/ext/ex_widget.dart';
import 'package:widgets/load_state_layout.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/widget_export.dart';

import 'agency_item.dart';
import 'agency_list_controller.dart';
import 'agency_list_state.dart';

/*
 * 新加坡的中介公司列表
 */
class SGAgencyListPage extends BaseStatefulPage<AgencyListController> {
  SGAgencyListPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance() {
    return Get.start(RouterPath.SGAgencyList);
  }

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

  @override
  State<SGAgencyListPage> createState() => _LabourRequestListState();
}

class _LabourRequestListState extends BaseState<SGAgencyListPage, AgencyListController> {
  late AgencyListState state;

  @override
  void initState() {
    super.initState();
    state = controller.state;
  }

  @override
  Widget build(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return SafeArea(
        bottom: MediaQuery.of(context).padding.bottom > 38,
        top: false,
        child: Container(
          width: double.infinity,
          height: double.infinity,
          padding: EdgeInsets.only(top: ScreenUtil.getStatusBarH(context)),
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: [
                Color(0xFF091D44),
                Color(0xFF245A8A),
                Color(0xFF7F7CEC),
              ],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
          ),
          child: Column(
            children: [
              MyAppBar.titleBar(context, "Agency List".tr),

              //搜索的条件
              Row(
                children: [
                  //下拉选
                  Container(
                    padding: const EdgeInsets.only(left: 12, right: 10),
                    margin: const EdgeInsets.only(right: 10),
                    height: 40,
                    decoration: BoxDecoration(
                      color: const Color(0xFF4DCFF6).withOpacity(0.2),
                      borderRadius: const BorderRadius.all(Radius.circular(20)),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        MyTextView(
                          state.selectedStatusTxt??"",
                          fontSize: 14,
                          hint: "Status".tr,
                          textHintColor: ColorConstants.textGrayAECAE5,
                          textColor: ColorConstants.white,
                        ).expanded(),

                        //下拉选图标
                        const MyAssetImage(Assets.baseServiceTriangleDropDownIcon, width: 11.5, height: 6.28),
                      ],
                    ),
                  ).onTap(() {
                    FocusScope.of(context).unfocus();
                    controller.pickAgencyStatus();
                  }).expanded(flex: 1),

                  //搜索框
                  Container(
                    height: 40,
                    margin: const EdgeInsets.only(right: 10),
                    padding: const EdgeInsets.only(left: 12, right: 10),
                    decoration: BoxDecoration(
                      color: const Color(0xFF4DCFF6).withOpacity(0.2),
                      borderRadius: const BorderRadius.all(Radius.circular(20)),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        IgnoreKeyboardDismiss(
                          child: TextField(
                            cursorColor: Colors.white,
                            cursorWidth: 1.5,
                            autofocus: false,
                            maxLines: 1,
                            minLines: 1,
                            focusNode: state.focusNode,
                            controller: state.searchController,
                            decoration: InputDecoration(
                              isDense: true,
                              //清除垂直方向的填充
                              isCollapsed: true,
                              //让文字垂直居中
                              border: InputBorder.none,
                              hintText: 'Agency Name'.tr,
                              hintStyle: const TextStyle(
                                color: ColorConstants.textGrayAECAE5,
                                fontSize: 14.0,
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                            style: const TextStyle(
                              color: ColorConstants.white,
                              fontSize: 14.0,
                              fontWeight: FontWeight.w400,
                            ),
                            onSubmitted: (value){
                              controller.doSearch(value);
                            },
                            // 键盘动作右下角图标
                            textInputAction: TextInputAction.search,
                            textAlignVertical: TextAlignVertical.center,
                          ),
                        ).expanded(),
                      ],
                    ),
                  ).expanded(flex: 1),

                  //筛选按钮
                  MyButton(
                    onPressed: () {
                      FocusScope.of(context).unfocus();
                      controller.resetFiltering();
                    },
                    text: "Reset".tr,
                    textColor: ColorConstants.white,
                    backgroundColor: hexToColor("#2BA9F9", opacity: 0.5),
                    radius: 20,
                    minWidth: 60,
                    minHeight: 35,
                  ),
                ],
              ).marginOnly(top: 10, left: 15, right: 15),

              // 添加按钮
              MyButton(
                type: ClickType.throttle,
                milliseconds: 500,
                onPressed: () {
                  FocusScope.of(context).unfocus();
                  controller.gotoAddAgencyPage();
                },
                text: "Add Agency".tr,
                textColor: ColorConstants.white,
                fontSize: 16,
                radius: 20,
                backgroundColor: hexToColor("#FFBB1B"),
                fontWeight: FontWeight.w500,
              ).marginSymmetric(horizontal: 15, vertical: 10),

              //底部的列表
              EasyRefresh(
                controller: controller.refreshController,
                onRefresh: controller.onRefresh,
                onLoad: controller.loadMore,
                child: LoadStateLayout(
                  state: controller.loadingState,
                  errorMessage: controller.errorMessage,
                  errorRetry: () {
                    controller.retryRequest();
                  },
                  successSliverWidget: [
                    SliverList(
                        delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        return AgencyItem(
                          index: index,
                          item: state.datas[index],
                          onDetailAction: () {
                            controller.gotoDetailPage(state.datas[index]);
                          },
                          onDeleteAction: () {
                            controller.doDelete(state.datas[index].pivotId,index);
                          },
                          onEditAction: () {
                            controller.gotoEditPage(state.datas[index]);
                          },

                        );
                      },
                      childCount: state.datas.length,
                    ))
                  ],
                ),
              ).expanded(),
            ],
          ),
        ),
      );
    });
  }
}
