import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:qr_flutter/qr_flutter.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/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/device/device_type_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';

class DeviceDetailPage extends StatefulWidget {
  var device;
  DeviceDetailPage({super.key, required this.device});

  @override
  State<DeviceDetailPage> createState() => _DeviceDetailPageState();
}

class _DeviceDetailPageState extends State<DeviceDetailPage> {
  GlobalController globalController = Get.find();
  UserInfoController userInfoController = Get.find();
  BlueteethBindController blueteethBindController = Get.find();
  ThemeController themeController = Get.find();
  DeviceTypeController deviceTypeController = Get.find();

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

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, bodySize) => GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/img/bgNoImg.png'), // 本地图片
              fit: BoxFit.fill, // 填满整个 Container
            ),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent, // 加上这一行
            appBar: AppBar(
              backgroundColor: themeController.currentColor.sc17,
              // backgroundColor: Colors.transparent,
              automaticallyImplyLeading: false,
              iconTheme: IconThemeData(color: themeController.currentColor.sc3),
              titleSpacing: 0.rpx,
              // leading: returnIconButtom,
              title: Container(
                width: double.infinity,
                height: 180.rpx,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    /// 居中标题
                    Text(
                      '设备详情.标题'.tr,
                      style: TextStyle(
                        fontFamily: 'Readex Pro',
                        color: themeController.currentColor.sc3,
                        letterSpacing: 0.rpx,
                        fontSize: 30.rpx,
                      ),
                    ),

                    /// 左边返回按钮
                    Positioned(
                      left: 0.rpx,
                      child: returnIconButtom,
                    ),
                  ],
                ),
              ),

              actions: [],
              centerTitle: false,
            ),

            body: SafeArea(
              top: true,
              child: Padding(
                padding: EdgeInsetsDirectional.fromSTEB(
                    30.rpx, 26.rpx, 30.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(
                        0.rpx, 92.rpx, 0.rpx, 0.rpx),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          width: MediaQuery.sizeOf(context).width * 0.267,
                          height: MediaQuery.sizeOf(context).width * 0.267,
                          constraints: BoxConstraints(
                            minWidth: 200.rpx,
                            minHeight: 200.rpx,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.white,
                          ),
                          child:
                              (widget.device['code']?.toString().isNotEmpty ??
                                      false)
                                  ? QrImageView(
                                      data: widget.device['code'].toString(),
                                      version: QrVersions.auto,
                                      size: 200.0.rpx,
                                    )
                                  : Image.asset(
                                      "assets/img/errorImg.png",
                                      fit: BoxFit.cover,
                                    ),
                        ),
                        SizedBox(height: 20.rpx),
                        Container(
                          height: 50.rpx,
                          decoration: BoxDecoration(),
                          child: Text(
                            (widget.device['code'] ?? '').toString().isNotEmpty
                                ? widget.device['code'].toString()
                                : '未知数据'.tr,
                            style: TextStyle(
                              fontFamily: 'Inter',
                              fontSize: 26.rpx,
                              letterSpacing: 0.rpx,
                              color: themeController.currentColor.sc3,
                            ),
                          ),
                        ),
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: [
                                // Container(
                                //   width:
                                //       MediaQuery.sizeOf(context).width * 0.267,
                                //   height:
                                //       MediaQuery.sizeOf(context).width * 0.267,
                                //   constraints: BoxConstraints(
                                //     minWidth: 200.rpx,
                                //     minHeight: 200.rpx,
                                //   ),
                                //   decoration: BoxDecoration(),
                                // ),
                                // Container(
                                //   height: 50.rpx,
                                //   decoration: BoxDecoration(),
                                // ),

                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.设备名称'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.MAC'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.型号'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.版本'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.网络状态'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.故障状态'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.更新状态'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '设备详情.更新时间'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc4,
                                      ),
                                    ),
                                  ),
                                ),
                              ]
                                  .divide(SizedBox(height: 34.rpx))
                                  .addToStart(SizedBox(height: 92.rpx))
                                  .addToEnd(SizedBox(height: 97.rpx)),
                            ),
                            Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                // Container(
                                //   width:
                                //       MediaQuery.sizeOf(context).width * 0.267,
                                //   height:
                                //       MediaQuery.sizeOf(context).width * 0.267,
                                //   constraints: BoxConstraints(
                                //     minWidth: 200.rpx,
                                //     minHeight: 200.rpx,
                                //   ),
                                //   decoration: BoxDecoration(
                                //     color: Colors.transparent,
                                //   ),
                                //   child: (widget.device['code']
                                //               ?.toString()
                                //               .isNotEmpty ??
                                //           false)
                                //       ? QrImageView(
                                //           data:
                                //               widget.device['code'].toString(),
                                //           version: QrVersions.auto,
                                //           size: 200.0.rpx,
                                //         )
                                //       : Image.asset(
                                //           "assets/img/errorImg.jpeg",
                                //           fit: BoxFit.cover,
                                //         ),
                                // ),
                                // Container(
                                //   height: 50.rpx,
                                //   decoration: BoxDecoration(),
                                //   child: Align(
                                //     alignment:
                                //         AlignmentDirectional(-1.rpx, 0.rpx),
                                //     child: Text(
                                //       (widget.device['code'] ?? '')
                                //               .toString()
                                //               .isNotEmpty
                                //           ? widget.device['code'].toString()
                                //           : '未知数据'.tr,
                                //       style: FlutterFlowTheme.of(context)
                                //           .bodyMedium
                                //           .override(
                                //             fontFamily: 'Inter',
                                //             fontSize: 26.rpx,
                                //             letterSpacing: 0.rpx,
                                //             color: themeController
                                //                 .currentColor.sc3,
                                //           ),
                                //     ),
                                //   ),
                                // ),

                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '${widget.device['person']?['name'] ?? '未命名'.tr}',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '${widget.device['mac'] ?? '-'.tr}',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '-',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '-',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '${widget.device['status']['status'] == 1 ? '在线'.tr : '离线'.tr}',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '${widget.device['status']['failure'] == 1 ? '有故障'.tr : '无故障'.tr}',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '${widget.device['status']['upgrade'] == 1 ? '有更新'.tr : '无更新'.tr}',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  height: 50.rpx,
                                  decoration: BoxDecoration(),
                                  child: Align(
                                    alignment:
                                        AlignmentDirectional(-1.rpx, 0.rpx),
                                    child: Text(
                                      '${widget.device['status']?['updateTime'] == null ? '-'.tr : MyUtils.timestampToDateString(widget.device['status']?['updateTime'])}',
                                      style: TextStyle(
                                        fontFamily: 'Inter',
                                        fontSize: 26.rpx,
                                        letterSpacing: 0.rpx,
                                        color: themeController.currentColor.sc3,
                                      ),
                                    ),
                                  ),
                                ),
                              ]
                                  .divide(SizedBox(height: 34.rpx))
                                  .addToStart(SizedBox(height: 92.rpx))
                                  .addToEnd(SizedBox(height: 97.rpx)),
                            ),
                          ].divide(SizedBox(width: 34.rpx)),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
