import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';
import 'package:vbvs_app/component/tool/CustomCard.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/mh_controller/device_list_controller.dart';
import 'package:vbvs_app/pages/mh_page/bluetooth.dart';
import 'package:vbvs_app/pages/mh_page/test/WebviewTestModel.dart';

class EditBedPage extends StatefulWidget {
  final Map data;
  EditBedPage({Key? key, required this.data});
  @override
  _EditBedPageState createState() => _EditBedPageState();
}

BoxConstraints? bodysize;

class _EditBedPageState extends State<EditBedPage> {
  late Map<String, dynamic> editedData;
  late TextEditingController _nameController; //名称文本输入框
  DeviceListController deviceListController = Get.find();
  @override
  void initState() {
    super.initState();
    editedData = Map.from(widget.data); // 创建副本
    _nameController = TextEditingController(text: editedData['name']);
  }

  @override
  void dispose() {
    _nameController.dispose(); // 记得释放Controller
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, cc) {
      bodysize = cc;
      return GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/new_background.png'), // 本地图片
              fit: BoxFit.fill, // 填满整个 Container
            ),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              backgroundColor: Colors.transparent,
              iconTheme: const IconThemeData(color: Colors.white),
              automaticallyImplyLeading: false,
              titleSpacing: 0,
              title: SizedBox(
                width: double.infinity,
                height: 180.rpx,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    // 中间居中的标题
                    Text(
                      '智能床名称',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 30.rpx,
                      ),
                    ),
                    // 左侧图标
                    Positioned(
                      left: 0.rpx,
                      child: returnIconButtomNew,
                    ),
                  ],
                ),
              ),
              centerTitle: false,
            ),
            body: SafeArea(
                top: true,
                child: Padding(
                    padding: EdgeInsets.only(top: 40.rpx),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          width: bodysize!.maxHeight * 1,
                          height: bodysize!.maxHeight * 0.335,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Container(
                                child:
                                    Image.asset('assets/images/bed_name.png'),
                                width: bodysize!.maxWidth * 0.69,
                                height: 193.rpx,
                              ),
                              Padding(
                                  padding:
                                      EdgeInsets.symmetric(horizontal: 60.rpx),
                                  child: Container(
                                    alignment: Alignment.center,
                                    height: bodysize!.maxHeight * 0.076,
                                    decoration: const BoxDecoration(
                                        border: Border(
                                            bottom: BorderSide(
                                                color: Color(0xFF929699),
                                                width: 0))),
                                    child: TextFormField(
                                      // autofocus: true,
                                      controller: _nameController,
                                      obscureText: false,
                                      onChanged: (val) {
                                        editedData['name'] = val;
                                      },
                                      textAlign: TextAlign.center,

                                      decoration: InputDecoration(
                                        hintText: "请输入床的名称",
                                        contentPadding:
                                            const EdgeInsetsDirectional
                                                .fromSTEB(10, 0, 10, 0),
                                        labelStyle: TextStyle(
                                          fontFamily: 'Readex Pro',
                                          letterSpacing: 0,
                                          fontSize: 36.rpx, // 设置输入文字大小
                                          color: Colors.white, // 设置输入文字颜色
                                        ),
                                        hintStyle: TextStyle(
                                          fontFamily: 'Readex Pro',
                                          letterSpacing: 0,
                                          color: Color(0xFF929699),
                                          fontSize: 36.rpx,
                                        ),
                                        enabledBorder: UnderlineInputBorder(
                                          borderSide: const BorderSide(
                                            color: Color(0x00000000),
                                            width: 2,
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(8),
                                        ),
                                        focusedBorder: UnderlineInputBorder(
                                          borderSide: const BorderSide(
                                            color: Color(0x00000000),
                                            width: 2,
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(8),
                                        ),
                                        errorBorder: UnderlineInputBorder(
                                          borderSide: const BorderSide(
                                            color: Color(0x00000000),
                                            width: 2,
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(8),
                                        ),
                                        focusedErrorBorder:
                                            UnderlineInputBorder(
                                          borderSide: const BorderSide(
                                            color: Color(0x00000000),
                                            width: 2,
                                          ),
                                          borderRadius:
                                              BorderRadius.circular(8),
                                        ),
                                      ),
                                      style: TextStyle(
                                        fontFamily: 'Readex Pro',
                                        letterSpacing: 0,
                                        color: Colors.white,
                                        fontSize: 26.rpx,
                                      ),
                                    ),
                                  ))
                            ],
                          ),
                        ),
                        Padding(
                            padding: EdgeInsets.only(
                                left: 30.rpx, right: 30.rpx, bottom: 85.rpx),
                            child: CustomCard(
                              borderRadius: 16.rpx,
                              gradientDirection: GradientDirection.vertical,
                              onTap: () async {
                                String serviceAddress =
                                    ServiceConstant.service_address;
                                String serviceName =
                                    ServiceConstant.server_service;
                                String serviceApi = ServiceConstant.device_show;
                                String queryUrl =
                                    "$serviceAddress$serviceName$serviceApi";
                                await requestWithLog(
                                  logTitle: "更新设备信息",
                                  method: MyHttpMethod.put,
                                  queryUrl: queryUrl,
                                  data: {
                                    "id": editedData["_id"],
                                    "name": editedData["name"],
                                  },
                                  onSuccess: (res) {
                                    TopSlideNotification.show(context,
                                        text: "修改名称成功".tr);
                                    deviceListController.getDeviceList();
                                    try {
                                      WebviewTestController
                                          webviewTestController = Get.find();
                                      webviewTestController.web.jsbridge?.dart
                                          .alterDevice();
                                    } catch (e) {
                                      ef.log("[h5]通知列表更新错误：$e");
                                    }
                                    Get.back(result: editedData);
                                  },
                                );
                              },
                              colors: const [
                                Color(0xFFFCFCFC),
                                Color(0xFFF8FAF9),
                                Color(0XFFECF6F3),
                                Color(0XFFD9F0E9),
                                Color(0xFFCEECE3)
                              ],
                              child: Container(
                                width: double.infinity,
                                height: 90.rpx,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(6),
                                ),
                                child: Text("完成".tr,
                                    style: TextStyle(
                                        color: const Color(0xFF003058),
                                        fontSize: 26.rpx)),
                              ),
                            ))
                      ],
                    ))),
          ),
        ),
      );
    });
  }
}
