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:shared/utils/date_time_utils.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_text_view.dart';

import 'report_finance_controller.dart';

import 'package:plugin_basic/base/base_stateless_page.dart';
import 'package:plugin_basic/utils/ext_get_nav.dart';
import 'package:router/path/router_path.dart';

import 'report_finance_item.dart';
import 'report_finance_state.dart';

class ReportFinancePage extends BaseStatelessPage<ReportFinanceController> {
  ReportFinancePage({Key? key}) : super(key: key);

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

  late ReportFinanceState state;

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

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

  @override
  Widget buildWidget(BuildContext context) {
    return autoCtlGetBuilder(builder: (controller) {
      return Scaffold(
          extendBodyBehindAppBar: true,
          appBar: MyAppBar.appBar(context, "Finance Report".tr),
          body: SafeArea(
              bottom: MediaQuery.of(context).padding.bottom > 38,
              top: false,
              child: Container(
              width: double.infinity,
          height: double.infinity,
              padding: EdgeInsets.only(top: kToolbarHeight + ScreenUtil.getStatusBarH(context) + 1),
      decoration: const BoxDecoration(
      gradient: LinearGradient(
      colors: [
      Color(0xFF091D44),
      Color(0xFF245A8A),
      Color(0xFF7F7CEC),
      ],
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      ),
      ),
      child: Column(
      children: [
      // 时间筛选
      Container(
      width: double.infinity,
      height: 36,
      margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      decoration: BoxDecoration(
      color: const Color(0xFF4DCFF6).withOpacity(0.2), // 设置背景颜色和不透明度
      borderRadius: BorderRadius.circular(20.0), // 设置圆角
      ),
      child: Row(
      children: [
      MyTextView(
      DateTimeUtils.formatDate(state.startDateTime, format: "yyyy-MM-dd"),
      hint: "Start Date".tr,
      textHintColor: ColorConstants.textGrayAECAE5,
      fontSize: 15,
      textAlign: TextAlign.center,
      isFontRegular: true,
      onClick: controller.pickerStartDate,
      textColor: ColorConstants.textGrayAECAE5,
      ).expanded(),
      Container(color: ColorConstants.dividerBar, height: 21.5, width: 0.5),
      MyTextView(
      DateTimeUtils.formatDate(state.endDateTime, format: "yyyy-MM-dd"),
      hint: "End Date".tr,
      textHintColor: ColorConstants.textGrayAECAE5,
      fontSize: 15,
      textAlign: TextAlign.center,
      isFontRegular: true,
      onClick: controller.pickerEndDate,
      textColor: ColorConstants.textGrayAECAE5,
      ).expanded(),
      ],
      )),

      //总数据
      Container(
      margin: const EdgeInsets.only(left: 15, right: 15),
      width: double.infinity,
      height: 45,
      decoration: BoxDecoration(
      color: const Color(0xFF4DCFF6).withOpacity(0.2), // 设置背景颜色和不透明度
      borderRadius: BorderRadius.circular(5), // 设置圆角
      ),
      child: Row(
      children: [
      Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
      MyTextView(
      "Total RQST:",
      fontSize: 14,
      isFontRegular: true,
      onClick: controller.pickerStartDate,
      textColor: ColorConstants.textGrayAECAE5,
      ),
      MyTextView(
      state.entity?.requestTotal.toString() ?? "",
      fontSize: 14,
      marginLeft: 5,
      isFontRegular: true,
      textColor: ColorConstants.white,
      ),
      ],
      ).expanded(),
      Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
      MyTextView(
      "Total Actual:",
      fontSize: 14,
      isFontRegular: true,
      onClick: controller.pickerStartDate,
      textColor: ColorConstants.textGrayAECAE5,
      ),
      MyTextView(
      state.entity?.actualTotal.toString() ?? "",
      fontSize: 14,
      marginLeft: 5,
      isFontRegular: true,
      textColor: ColorConstants.white,
      ),
      ],
      ).expanded(),
      ],
      )),

      //动态列表
      LoadStateLayout(
      state: controller.loadingState,
      errorMessage: controller.errorMessage,
      errorRetry: () {
      controller.retryRequest();
      },
      successSliverWidget: [
      SliverGrid(
      delegate: SliverChildBuilderDelegate(
      (context, index) {
      return ReportFinanceItem(item: state.datas[index]);
      },
      childCount: state.datas.length,
      ),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2, // 每行2个项目
      mainAxisSpacing: 8.5, // 主轴方向的间距
      crossAxisSpacing: 9.5, // 交叉轴方向的间距
      childAspectRatio: 168.16 / 107.22, // 子项目的宽高比
      ),
      )
      ],
      ).marginOnly(left: 15, right: 15, top: 10).expanded(),
      ],
      ),
      ),
      ),
      );
    });
  }
}
