import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:qhd_prevention/customWidget/ItemWidgetFactory.dart';
import 'package:qhd_prevention/customWidget/custom_button.dart';
import 'package:qhd_prevention/customWidget/date_picker_dialog.dart';
import 'package:qhd_prevention/customWidget/photo_picker_row.dart';
import 'package:qhd_prevention/customWidget/single_image_viewer.dart';
import 'package:qhd_prevention/customWidget/toast_util.dart';
import 'package:qhd_prevention/http/ApiService.dart';
import 'package:qhd_prevention/pages/home/tap/item_list_widget.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/tools.dart';



class EquimentDetailListinfo extends StatefulWidget {
  const EquimentDetailListinfo(this. id, {super.key});

  final String id;

  @override
  State<EquimentDetailListinfo> createState() => _EquimentDetailListinfoState();
}

class _EquimentDetailListinfoState extends State<EquimentDetailListinfo> {


  final TextEditingController _xunController = TextEditingController();
  final TextEditingController _neiRongController = TextEditingController();
  // 是否有故障
  bool _isFault = false;
  String dataTime="";

  List<dynamic> faultImgs = [];
  List<dynamic> handleImgs = [];
  List<dynamic> inspectionImgs = [];

  dynamic pd={};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _getEquimentDetailListinfo();
  }

  Future<void> _getEquimentDetailListinfo() async {
    try {
      final result = await ApiService.getEquimentDetailListinfo(widget.id);
      if (result['result'] == 'success') {

        setState(() {

          pd= result['pd'];
          faultImgs.addAll(result['faultImgs'] ?? []);
          handleImgs.addAll(result['handleImgs'] ?? []);
          inspectionImgs.addAll(result['inspectionImgs'] ?? []);

        });
      }
    } catch (e) {
      print('Error fetching data: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "开始巡检"),
      body: ListView(
        children: [
          Container(
            color: Colors.white,
            child: Column(children: [
              ItemListWidget.singleLineTitleText(
                label: '巡检人：',
                isEditable: false,
                controller: _xunController,
                text: pd["INSPECTOR"]??"",
              ),
              const Divider(),
              ItemListWidget.singleLineTitleText(
                label: '巡检时间：',
                isEditable: false,
                controller: _xunController,
                text: pd["INSPECTIONTIME"]??"",
              ),

              const Divider(),
              ItemListWidget.singleLineTitleText(
                label: '巡检内容：',
                isEditable: false,
                controller: _xunController,
                text: pd["INSPECTIONCONTENT"]??"",
              ),

              const Divider(),
              ItemListWidget.singleLineTitleText(
                label: '是否有故障：',
                isEditable: false,
                controller: _xunController,
                text: pd["ISFAULT"]??"",
              ),

              if(faultImgs.isNotEmpty)
               Divider(),
              if(faultImgs.isNotEmpty)
              ItemListWidget.twoRowTitleAndImages(
                onTapCallBack: (val) {
                  presentOpaque(SingleImageViewer(imageUrl: val), context);
                },
                title: '故障照片：',
                imageUrls: faultImgs
                    .map((item) => item['FILEPATH'] as String) // 提取并转换为 String
                    .toList(),
              ),

              if(faultImgs.isNotEmpty)
               Divider(),
              if(faultImgs.isNotEmpty)
              ItemListWidget.twoRowTitleAndImages(
                onTapCallBack: (val) {
                  presentOpaque(SingleImageViewer(imageUrl: val), context);
                },
                title: '故障处理后照片：',
                imageUrls: handleImgs
                    .map((item) => item['FILEPATH'] as String) // 提取并转换为 String
                    .toList(),
              ),

              const Divider(),
              ItemListWidget.twoRowTitleAndImages(
                onTapCallBack: (val) {
                  presentOpaque(SingleImageViewer(imageUrl: val), context);
                },
                title: '巡检照片：',
                imageUrls: inspectionImgs
                    .map((item) => item['FILEPATH'] as String) // 提取并转换为 String
                    .toList(),
              ),

                SizedBox(height: 20,)

            ],
            ),
          ),

        ],
      ),
    );
  }





}
