import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.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/NullDataComponentWidget.dart';
import 'package:vbvs_app/controller/repair/repair_controller.dart';
import 'package:vbvs_app/model/api_response.dart';
import 'package:vbvs_app/pages/repair/component/RepairHistoryInfoWidget.dart';

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

  @override
  State<RepairListPage> createState() => _RepairListPageState();
}

class _RepairListPageState extends State<RepairListPage> {
  RepairController repairController = Get.find();

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

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

  @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,
              automaticallyImplyLeading: false,
              iconTheme: IconThemeData(
                color: themeController.currentColor.sc3,
              ),
              titleSpacing: 0,
              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,
                        fontSize: 30.rpx,
                      ),
                    ),
                    Positioned(
                      left: 0,
                      // child: returnIconButtom,
                      child: returnIconButtomAddCallback(() {}),
                    ),
                    // Positioned(
                    //   right: 20.rpx,
                    //   child: ClickableContainer(
                    //     backgroundColor: Colors.transparent,
                    //     highlightColor: themeController.currentColor.sc16,
                    //     padding: EdgeInsets.all(8.rpx),
                    //     onTap: () {},
                    //     child: SvgPicture.asset(
                    //       'assets/img/icon/history.svg',
                    //       width: 39.rpx,
                    //       height: 39.rpx,
                    //       color: themeController.currentColor.sc16,
                    //     ),
                    //   ),
                    // ),
                  ],
                ),
              ),
              actions: [],
              centerTitle: false,
            ),
            body: SafeArea(
              top: true,
              child: Padding(
                padding:
                    EdgeInsetsDirectional.fromSTEB(30.rpx, 29.rpx, 30.rpx, 0),
                child: Obx(() {
                  final isEmpty = repairController.repairHistory.value.isEmpty;
                  if (isEmpty) {
                    return Expanded(child: NullDataWidget());
                  }
                  return SingleChildScrollView(
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      children: repairController.repairHistory.value
                          .map((item) => RepairHistoryInfoWidget(
                              data: item)) // 假设组件支持传 data
                          .toList()
                          .divide(SizedBox(height: 25.rpx))
                          .addToEnd(SizedBox(height: 25.rpx)),
                    ),
                  );
                }),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Future<void> loadData() async {
    String serviceAddress = ServiceConstant.service_address;
    String serviceName = ServiceConstant.server_service;
    String serviceApi = ServiceConstant.submit_repair;
    String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
    ApiResponse apiResponse = await requestWithLog(
        logTitle: "查询报修数据", method: MyHttpMethod.get, queryUrl: queryUrl);
    RepairController repairController = Get.find();
    repairController.repairHistory.value = apiResponse.data;
    repairController.updateAll();
  }
}
