import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/controller/device/body_device_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';

class SleepDateWidget extends StatefulWidget {
  final String? mac;
  final String? time; // 必传：日期，例如 "07/15"
  final DateTime date; // 必传：日期，例如 "07/15"
  final String? score; // 可选：分数，默认为 "--"
  final String? comment; // 可选：评价，默认为 "暂无".tr
  final Color? textColor; // 可选：文字颜色，默认为灰色
  final bool? isSelected; // 是否选中

  const SleepDateWidget({
    super.key,
    this.mac,
    this.time,
    required this.date,
    this.score = '--',
    this.comment = '暂无',
    this.textColor,
    this.isSelected = false, // 新增参数，默认不选中
  });

  @override
  State<SleepDateWidget> createState() => _SleepDateWidgetState();
}

class _SleepDateWidgetState extends State<SleepDateWidget> {
  @override
  Widget build(BuildContext context) {
    ThemeController themeController = Get.find();
    BodyDeviceController bodyDeviceController = Get.find();
    String week = MyUtils.formatDateTimeWeek(widget.date);
    String day = MyUtils.formatDateTimeDay(widget.date);

    // 选中时背景色为黑色，否则为透明
    Color backgroundColor = widget.isSelected == true
        ? Colors.black.withOpacity(0.3)
        : Colors.transparent;

    return ClickableContainer(
      backgroundColor: backgroundColor,
      // highlightColor: themeController.currentColor.sc3.withOpacity(0.1),
      highlightColor: Colors.transparent,
      borderRadius: AppConstants().normal_container_radius,
      padding: EdgeInsets.zero,
      onTap: () {
        final mac = widget.mac;
        final time = widget.time;

        if (bodyDeviceController.sleepReportData.value.containsKey(mac)) {
          final list = bodyDeviceController.sleepReportData.value[mac];

          for (var item in list!) {
            item['selected'] = (item['time'] == time);
          }
          bodyDeviceController.sleepReportData.value = {
            ...bodyDeviceController.sleepReportData.value,
          };
          bodyDeviceController.updateAll();
        }
      },
      child: Container(
        width: MediaQuery.sizeOf(context).width * 0.19,
        constraints: BoxConstraints(
          minWidth: 143.rpx,
        ),
        child: Padding(
          padding:
              EdgeInsetsDirectional.fromSTEB(10.rpx, 25.rpx, 10.rpx, 22.rpx),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Padding(
                padding: EdgeInsetsDirectional.fromSTEB(0, 0, 0, 14.rpx),
                child: Text(
                  '${week}',
                  style: TextStyle(
                    fontFamily: 'Inter',
                    fontSize: AppConstants().title_text_fontSize,
                    letterSpacing: 0.0,
                    color: themeController.currentColor.sc3,
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsetsDirectional.fromSTEB(0, 0, 0, 33.rpx),
                child: Text(
                  '${day}',
                  style: TextStyle(
                    fontFamily: 'Inter',
                    fontSize: 20.rpx,
                    letterSpacing: 0.0,
                    color: themeController.currentColor.sc3,
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsetsDirectional.fromSTEB(0, 0, 0, 16.rpx),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      (widget.score?.isEmpty ?? true) ? '--' : widget.score!,
                      style: TextStyle(
                        fontFamily: 'Inter',
                        fontSize: 48.rpx,
                        letterSpacing: 0.0,
                        color: widget.textColor ??
                            themeController.currentColor.sc4,
                        fontWeight: FontWeight.bold, // 加粗
                      ),
                    ),
                    if ((widget.score?.trim().isNotEmpty ?? false))
                      Padding(
                        padding:
                            EdgeInsetsDirectional.fromSTEB(0, 16.rpx, 0, 0.rpx),
                        child: Text(
                          '分'.tr,
                          style: TextStyle(
                            fontFamily: 'Inter',
                            fontSize: AppConstants().small_text_fontSize,
                            letterSpacing: 0.0,
                            color: themeController.currentColor.sc3,
                          ),
                        ),
                      ),
                  ],
                ),
              ),
              Container(
                width: 0.2.rpx,
                height: 2.4.rpx,
                constraints: BoxConstraints(
                  minWidth: 123.rpx,
                  minHeight: 47.rpx,
                ),
                child: FFButtonWidget(
                  onPressed: () {
                    print('合格按钮点击');
                  },
                  text: (widget.comment?.trim().isEmpty ?? true)
                      ? '暂无'.tr
                      : widget.comment!,
                  options: FFButtonOptions(
                    height: 40.rpx,
                    padding:
                        EdgeInsetsDirectional.fromSTEB(16.rpx, 0, 16.rpx, 0),
                    iconPadding: EdgeInsetsDirectional.fromSTEB(0, 0, 0, 0),
                    color: widget.textColor ?? themeController.currentColor.sc4,
                    textStyle: TextStyle(
                      fontFamily: 'Inter Tight',
                      color: themeController.currentColor.sc3,
                      letterSpacing: 0.0,
                    ),
                    elevation: 0,
                    borderRadius: BorderRadius.circular(50.rpx),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
