import 'package:cs_resources/constants/color_constants.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 '../revise_list/revise_list_item.dart';
import 'revise_applied_controller.dart';
import 'revise_applied_state.dart';

/**
 * Revise的全部列表
 */
class ReviseAppliedPage extends BaseStatefulPage<ReviseAppliedController> {
  ReviseAppliedPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance(String? appliedId, String? jobTitle, String? jobDate) {
    return Get.start(RouterPath.reviseAppliedSG, arguments: {'appliedId': appliedId, 'jobTitle': jobTitle, 'jobDate': jobDate});
  }

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

  @override
  State<ReviseAppliedPage> createState() => _JobListState();
}

class _JobListState extends BaseState<ReviseAppliedPage, ReviseAppliedController> {
  late ReviseAppliedState state;

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

  @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,
                state.jobTitle ?? "Title".tr,
                subTitle: "(${state.jobDate ?? "-"})",
                subTitleColor: ColorConstants.textGrayAECAE5,
              ),

              // 添加按钮
              MyButton(
                type: ClickType.throttle,
                milliseconds: 500,
                onPressed: () {
                  FocusScope.of(context).unfocus();
                  controller.gotoReviewAddPage();
                },
                text: "Add Revise".tr,
                textColor: ColorConstants.white,
                fontSize: 16,
                radius: 20,
                backgroundColor: hexToColor("#FFBB1B"),
                fontWeight: FontWeight.w500,
              ).marginOnly(left: 15, right: 15, top: 15, bottom: 5),

              //底部的列表
              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 ReviseListItem(
                          index: index,
                          item: state.datas[index],
                          onDetailAction: () {
                            controller.gotoReviseDetailPage(index);
                          },
                          onLogsAction: () {
                            controller.gotoReviseLogsPage(index);
                          },
                          onDeleteAction: () {
                            controller.doDeleteAction(index);
                          },
                          onRecallAction: () {
                            controller.doRecallAction(index);
                          },
                          onEditAction: () {
                            controller.gotoReviseEditPage(index);
                          },
                        );
                      },
                      childCount: state.datas.length,
                    ))
                  ],
                ),
              ).marginOnly(top: 5, bottom: 5).expanded(),
            ],
          ),
        ),
      );
    });
  }
}
