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/component/tool/CustomCard.dart';

class RepairHistoryInfoWidget extends StatefulWidget {
  var data;
  RepairHistoryInfoWidget({super.key, required this.data});

  @override
  State<RepairHistoryInfoWidget> createState() =>
      _RepairHistoryInfoWidgetState();
}

class _RepairHistoryInfoWidgetState extends State<RepairHistoryInfoWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        ClickableContainer(
          borderRadius: AppConstants().normal_container_radius,
          onTap: () {
            //跳转详情
          },
          // colors: [
          //   themeController.currentColor.sc5,
          // ],
          backgroundColor: Colors.transparent,
          highlightColor: themeController.currentColor.sc21,
          padding: EdgeInsetsDirectional.fromSTEB(0.rpx, 0.rpx, 0.rpx, 0.rpx),
          child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: themeController.currentColor.sc5,
              borderRadius:
                  BorderRadius.circular(AppConstants().normal_container_radius),
            ),
            child: Padding(
              padding:
                  EdgeInsetsDirectional.fromSTEB(30.rpx, 36.rpx, 0.rpx, 36.rpx),
              child: Container(
                width: double.infinity,
                decoration: BoxDecoration(),
                child: Column(
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    Container(
                      width: double.infinity,
                      decoration: BoxDecoration(),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Container(
                            width: MediaQuery.sizeOf(context).width * 0.17,
                            constraints: BoxConstraints(
                              maxWidth: 140.rpx,
                            ),
                            decoration: BoxDecoration(),
                            child: Text(
                              '设备类型'.tr,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 26.rpx,
                                letterSpacing: 0.0,
                                color: themeController.currentColor.sc4,
                              ),
                            ),
                          ),
                          Text(
                            '${widget.data['type']}',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 26.rpx,
                              letterSpacing: 0.0,
                              color: themeController.currentColor.sc3,
                            ),
                          ),
                        ].divide(SizedBox(width: 33.rpx)),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      decoration: BoxDecoration(),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Container(
                            width: MediaQuery.sizeOf(context).width * 0.17,
                            constraints: BoxConstraints(
                              maxWidth: 140.rpx,
                            ),
                            decoration: BoxDecoration(),
                            child: Text(
                              '设备ID'.tr,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 26.rpx,
                                letterSpacing: 0.0,
                                color: themeController.currentColor.sc4,
                              ),
                            ),
                          ),
                          Expanded(
                            child: Text(
                              '${getDeviceInfo(widget.data)}',
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 26.rpx,
                                letterSpacing: 0.0,
                                color: themeController.currentColor.sc3,
                              ),
                            ),
                          ),
                        ].divide(SizedBox(width: 33.rpx)),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      decoration: BoxDecoration(),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Container(
                            width: MediaQuery.sizeOf(context).width * 0.17,
                            constraints: BoxConstraints(
                              maxWidth: 140.rpx,
                            ),
                            decoration: BoxDecoration(),
                            child: Text(
                              '联系人'.tr,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 26.rpx,
                                letterSpacing: 0.0,
                                color: themeController.currentColor.sc4,
                              ),
                            ),
                          ),
                          Text(
                            '${widget.data['contacts']?['name']}',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 26.rpx,
                              letterSpacing: 0.0,

                              color: themeController.currentColor.sc3,
                            ),
                          ),
                        ].divide(SizedBox(width: 33.rpx)),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      decoration: BoxDecoration(),
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Container(
                            width: MediaQuery.sizeOf(context).width * 0.17,
                            constraints: BoxConstraints(
                              maxWidth: 140.rpx,
                            ),
                            decoration: BoxDecoration(),
                            child: Text(
                              '发生时间'.tr,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 26.rpx,
                                letterSpacing: 0.0,
                                color: themeController.currentColor.sc4,
                              ),
                            ),
                          ),
                          Text(
                            '${MyUtils.timestampToDateString(widget.data['create_time'])}',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 26.rpx,
                              letterSpacing: 0.0,

                              color: themeController.currentColor.sc3,
                            ),
                          ),
                        ].divide(SizedBox(width: 33.rpx)),
                      ),
                    ),
                  ].divide(SizedBox(height: 34.rpx)),
                ),
              ),
            ),
          ),
        ),
        Positioned(
          bottom: 40.rpx,
          right: 20.rpx,
          child: Container(
            width: 123.rpx,
            height: 47.rpx,
            child: CustomCard(
              borderRadius: AppConstants().button_container_radius, // 直角
              colors: [
                themeController.currentColor.sc4.withOpacity(0.3),
              ],
              enableAnimation: true, // 有点击缩放动画
              enableGradient: false, // 不用渐变
              onTap: () {
                Get.toNamed("/repairDetailPage", arguments: widget.data);
              },
              child: Center(
                child: Text(
                  "查看".tr,
                  style: TextStyle(
                    fontFamily: 'Inter',
                    fontSize: 26.rpx,
                    letterSpacing: 0.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  String getDeviceInfo(Map data) {
    if (data['device'] == null || data['device'] is! List) {
      return '';
    }

    List deviceList = data['device'];
    List<String> result = deviceList.map((device) {
      final name = device['name'] ?? '';
      final code = device['code'];
      final mac = device['mac'] ?? '';
      final suffix = (code != null && code.toString().isNotEmpty) ? code : mac;
      return '$name($suffix)';
    }).toList();

    return result.join(', ');
  }
}
