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

import 'job_list_er_controller.dart';
import 'job_list_er_item.dart';
import 'job_list_er_state.dart';

/*
 * 工作列表
 */
class JobListERPage extends BaseStatefulPage<JobListController> {
  JobListERPage({Key? key}) : super(key: key);

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

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

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

class _JobListState extends BaseState<JobListERPage, JobListController> {
  late JobListErState state;

  @override
  void initState() {
    super.initState();
    state = controller.state;
    String? dateFormat = Get.arguments['selectedDate'];
    if (Utils.isNotEmpty(dateFormat)) {
      state.selectedStartDate =  DateTimeUtils.getDateTime(dateFormat!);
      state.selectedEndDate =  DateTimeUtils.getDateTime(dateFormat);
    }
    state.cb = Get.arguments['cb'] as void Function(dynamic)?;
  }

  @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.searchTitleBar(
                context,
                value: state.keyword,
                hintText: 'Title'.tr,
                controller: state.searchController,
                onSearch: (keyword) {
                  controller.doSearch(keyword);
                },
                actions: [
                  //重置按钮
                  MyButton(
                    onPressed: () {
                      FocusScope.of(context).unfocus();
                      controller.resetFiltering();
                    },
                    text: "Reset".tr,
                    textColor: ColorConstants.white,
                    backgroundColor: hexToColor("#2BA9F9", opacity: 0.5),
                    radius: 17.25,
                    minWidth: 60,
                    minHeight: 35,
                  ).marginOnly(right: 15),

                  //筛选图标
                  const MyAssetImage(
                    Assets.baseServiceTitleBarFilterIcon,
                    width: 24,
                    height: 16.5,
                  ).onTap(() {
                    FocusScope.of(context).unfocus();
                    controller.showFilterDialog();
                  }).marginOnly(right: 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 JobListErItem(
                          index: index,
                          item: state.datas[index],
                          onEditAction: () {
                            controller.gotoJobEditPage(state.datas[index]);
                          },
                          onAppliedAction: () {
                            controller.gotoJobAppliedPage(state.datas[index]);
                          },
                          onDeleteAction: (){
                            controller.doDeleteAction(state.datas[index]);
                          },
                          onPreSelectedAction: (){
                            controller.doPreSelectedAction(state.datas[index]);
                          },
                        );
                      },
                      childCount: state.datas.length,
                    ))
                  ],
                ),
              ).marginOnly(top: 5, bottom: 5).expanded(),
            ],
          ),
        ),
      );
    });
  }
}
