import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.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/device_share_list_controller.dart';
import 'package:vbvs_app/controller/message/message_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';

class DeviceShareInfoWidget extends StatefulWidget {
  final data;

  const DeviceShareInfoWidget({super.key, required this.data});

  @override
  State<DeviceShareInfoWidget> createState() => _DeviceShareInfoWidgetState();
}

class _DeviceShareInfoWidgetState extends State<DeviceShareInfoWidget> {
  ThemeController themeController = Get.find();
  MessageController messageController = Get.find();
  DeviceShareListController deviceShareListController = Get.find();

  @override
  Widget build(BuildContext context) {
    bool selected = false;
    return ClickableContainer(
      backgroundColor: themeController.currentColor.sc5,
      highlightColor: themeController.currentColor.sc3,
      borderRadius: 20.rpx,
      padding: EdgeInsetsDirectional.fromSTEB(0.rpx, 33.rpx, 0.rpx, 33.rpx),
      onTap: () {},
      child: Row(
        mainAxisSize: MainAxisSize.max,
        children: [
          Obx(() {
            return Theme(
              data: ThemeData(
                checkboxTheme: CheckboxThemeData(
                  visualDensity: VisualDensity.compact,
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(64),
                  ),
                ),
                unselectedWidgetColor: Color(0xFFD3D3D3),
              ),
              child: Checkbox(
                value: deviceShareListController.selectedShareInfo.value
                    .contains(widget.data[0]['k']),
                onChanged: (newValue) async {
                  if (newValue != null) {
                    if (newValue == true) {
                      deviceShareListController.selectedShareInfo.value
                          .add(widget.data[0]['k']);
                    } else {
                      deviceShareListController.selectedShareInfo.value
                          .remove(widget.data[0]['k']);
                    }
                  }
                  final selectedCount =
                      deviceShareListController.selectedShareInfo.length;
                  final totalCount =
                      deviceShareListController.shareInfoList.length;

                  deviceShareListController.model.all =
                      (selectedCount == totalCount) ? 1 : 0;
                  deviceShareListController.selectedShareInfo
                      .refresh(); // ✅ 关键代码
                  deviceShareListController.updateAll();
                },
                side: BorderSide(
                  width: 1.5,
                  color: FlutterFlowTheme.of(context).secondaryText,
                ),
                activeColor: stringToColor("#16C89F"),
                checkColor: FlutterFlowTheme.of(context).info,
              ),
            );
          }),
          Row(
            mainAxisSize: MainAxisSize.max,
            children: [
              Container(
                constraints: BoxConstraints(
                  minWidth: 30.rpx,
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: List.generate(widget.data.length, (index) {
                    final item = widget.data[index];
                    return Container(
                      constraints: BoxConstraints(
                        minHeight: 62.rpx,
                      ),
                      child: Align(
                        alignment: AlignmentDirectional(-1, 0),
                        child: Text(
                          "${item['k']}" + ":" + "${item['v']}",
                          style:
                              FlutterFlowTheme.of(context).bodyMedium.override(
                                    fontFamily: 'Inter',
                                    fontSize: 26.rpx,
                                    letterSpacing: 0.0,
                                    color: themeController.currentColor.sc3,
                                  ),
                        ),
                      ),
                    );
                  }),
                ),
              ),
            ].divide(SizedBox(width: 50.rpx)),
          ),
        ].divide(SizedBox(
          width: 10.rpx,
        )),
      ),
    );
  }
}
