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/NullDataComponentWidget.dart';
import 'package:vbvs_app/pages/mh_page/device/device.dart';
import 'package:vbvs_app/pages/mh_page/searchWidget.dart';

import '../../controller/mh_controller/device_list_controller.dart';

class DeviceListPage extends StatefulWidget {
  const DeviceListPage({super.key});

  @override
  State<DeviceListPage> createState() => _DeviceListPageState();
}

class _DeviceListPageState extends State<DeviceListPage> {
  final scaffoldKey = GlobalKey<ScaffoldState>();
  DeviceListController controller = Get.find();
  BoxConstraints? bodysize;

  @override
  void initState() {
    super.initState();
    controller.model.keyword = '';
    controller.getDeviceList();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, cc) {
      bodysize = cc;
      return GestureDetector(
          onTap: () => FocusScope.of(context).unfocus(),
          child: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/new_background.png'), // 本地图片
                fit: BoxFit.fill, // 填满整个 Container
              ),
            ),
            child: Scaffold(
                resizeToAvoidBottomInset: false,
                appBar: AppBar(
                  backgroundColor: Colors.transparent,
                  automaticallyImplyLeading: false,
                  iconTheme: IconThemeData(color: Colors.white),
                  titleSpacing: 0,
                  // leading: returnIconButtomAddCallback(() {
                  //   controller.saveDataApi();
                  //   updateParm(isShowToast: false);
                  // }),
                  // leading: returnIconButtomNew,
                  title: Container(
                    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,
                        ),
                      ],
                    ),
                  ),

                  actions: [],
                  centerTitle: false,
                ),
                backgroundColor: Colors.transparent,
                body: Container(
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Padding(
                        padding: EdgeInsetsDirectional.fromSTEB(0, 10, 0, 23),
                        child: SearchWidget(
                          keyword: controller.model.keyword,
                          color: controller.model.color,
                          hint: "检索设备",
                          onChange: (d) {
                            controller.model.keyword = d;
                          },
                          findCallback: () {
                            controller.getDeviceList();
                          },
                        ),
                      ),
                      Obx(() {
                        if (controller.model.deviceList == null ||
                            controller.model.deviceList.isEmpty) {
                          return Expanded(child: NullDataWidget());
                        }

                        // 如果 deviceList 不为空，渲染列表
                        return Expanded(
                          child: ListView(
                            shrinkWrap: true,
                            scrollDirection: Axis.vertical,
                            children: controller.model.deviceList
                                .asMap()
                                .entries
                                .map((e) => DeviceInfoWidget(
                                      index: e.key,
                                      deviceListController: controller,
                                    ))
                                .toList()
                                .divide(const SizedBox(height: 10))
                                .addToEnd(const SizedBox(height: 100)),
                          ),
                        );
                      }),
                    ],
                  ),
                )

                //  Container(
                //   width: bodysize!.maxWidth,
                //   height: bodysize!.maxHeight,

                // ),
                ),
          ));
    });
  }
}
