import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared/utils/date_time_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/widget_export.dart';

import 'casual_report_item.dart';
import 'casual_report_controller.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/my_appbar.dart';
import 'casual_report_state.dart';

/*
 * 最后一个Casual的月度报表
 */
class UKCasualReportPage extends BaseStatefulPage<CasualReportController> {
  UKCasualReportPage({Key? key}) : super(key: key);

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

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

  @override
  State<UKCasualReportPage> createState() => _LabourReviewState();
}

class _LabourReviewState extends BaseState<UKCasualReportPage, CasualReportController> {
  late CasualReportState 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, "Casual Report".tr),

              //选择时间
              Container(
                width: double.infinity,
                height: 42,
                padding: const EdgeInsets.symmetric(horizontal: 14),
                margin: const EdgeInsets.only(left: 15, right: 15, top: 10, bottom: 5),
                decoration: BoxDecoration(
                  color: const Color(0xFF4DCFF6).withOpacity(0.2), // 设置背景颜色和不透明度
                  borderRadius: BorderRadius.circular(20), // 设置圆角
                ),
                child:Align(
                  alignment: Alignment.centerLeft, // 左侧对齐
                  child: MyTextView(
                    DateTimeUtils.formatDate(state.selectedDate??DateTime.now(),format: "yyyy-MM"),
                    textColor: Colors.white,
                    fontSize: 15,
                    isFontRegular: true,
                  ),
                ),
              ).onTap((){
                controller.showDatePicker();
              }),

              //底部的列表
              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 CasualReportItem(
                          index: index,
                          item: state.datas[index],
                        );
                      },
                      childCount: state.datas.length,
                    ))
                  ],
                ),
              ).expanded(),
            ],
          ),
        ),
      );
    });
  }
}
