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/widget_export.dart';

import 'contract_rate_edit_item.dart';
import 'contract_rate_edit_list_controller.dart';
import 'contract_rate_edit_list_state.dart';

/*
 * 新加坡的指定合同的时薪管理列表的详情
 * 其实就是SGContractRateListPage的详情页面
 */
class SGContractRateEditListPage extends BaseStatefulPage<ContractRateEditListController> {
  SGContractRateEditListPage({Key? key}) : super(key: key);

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

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

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

class _LabourRequestListState extends BaseState<SGContractRateEditListPage, ContractRateEditListController> {
  late ContractRateEditListState state;

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

  @override
  Widget build(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return SafeArea(
        bottom: true,
        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, "Edit".tr),

              // 添加按钮
              Row(
                children: [
                  //设置
                  MyButton(
                    type: ClickType.throttle,
                    milliseconds: 500,
                    onPressed: () {
                      FocusScope.of(context).unfocus();
                      controller.gotoSettingPage();
                    },
                    text: "Setting".tr,
                    textColor: ColorConstants.white,
                    fontSize: 16,
                    radius: 20,
                    backgroundColor: hexToColor("#56AAFF"),
                    fontWeight: FontWeight.w500,
                  ).expanded(),

                  const SizedBox(width: 15),

                  //特殊日期
                  MyButton(
                    type: ClickType.throttle,
                    milliseconds: 500,
                    onPressed: () {
                      FocusScope.of(context).unfocus();
                      controller.gotoAddSpecificPage();
                    },
                    text: "Specific Day".tr,
                    textColor: ColorConstants.white,
                    fontSize: 16,
                    radius: 20,
                    backgroundColor: hexToColor("#0AC074"),
                    fontWeight: FontWeight.w500,
                  ).expanded(),
                ],
              ).marginSymmetric(horizontal: 15, vertical: 15),

              //底部的列表
              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 ContractRateEditItem(
                          index: index,
                          item: state.datas[index],
                          onEditAction: () {
                            controller.showEditDialog(state.datas[index]);
                          },
                          onDeleteAction: () {
                            controller.showDeleteDialog(state.datas[index], index);
                          },
                        );
                      },
                      childCount: state.datas.length,
                    ))
                  ],
                ),
              ).expanded(),
            ],
          ),
        ),
      );
    });
  }
}
