import 'dart:io';
import 'package:flutter/material.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/custom_button.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/dateTime_picker_bottom_sheet.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/edit_list_items.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/photo_picker_row.dart';
import 'package:qhdkfq_regulatory_flutter/tools/my_appbar.dart';
import '../../Custom/dashed_line_text.dart';
import '../../Custom/single_image_viewer.dart';
import '../../tools/h_colors.dart';
import '../../tools/tools.dart';

enum DangerDetailType { detail, repeat }

class DangerDetailPage extends StatefulWidget {
  const DangerDetailPage(this.type, {super.key});

  final DangerDetailType type;

  @override
  State<DangerDetailPage> createState() => _DangerDetailPageState();
}

class _DangerDetailPageState extends State<DangerDetailPage> {
  bool _isQualified = true;
  String _repeatTime = "请选择";
  DateTime _selectedDateTime = DateTime.now();
  final _reasonEditController = TextEditingController();
  void _showDateTimePicker(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return DateTimePickerBottomSheet(
          initialDateTime: _selectedDateTime, // 可选：初始时间
          onDateTimeSelected: (selectedDateTime) {
            // 处理选择的时间
            print("选择的时间: $selectedDateTime");
            // 更新页面状态等操作
          },
        );
      },
    );
  }
  // 构建带分隔符的列表项
  List<Widget> _buildSectionItems(List<Widget> items) {
    return List.generate(items.length * 2 - 1, (index) {
      if (index.isEven) {
        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: items[index ~/ 2],
        );
      }
      return const Divider(height: 15);
    });
  }

  @override
  Widget build(BuildContext context) {
    // 隐患信息部分
    final List<Widget> dangerMessages = _buildSectionItems([
      EditListItems.createColumnTextItem(
        title: "帮扶内容",
        text: "测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据",
      ),
      EditListItems.createColumnTextItem(title: "隐患描述", text: "测试数据"),
      EditListItems.createColumnTextItem(title: "隐患编码", text: "测试数据"),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "隐患部位",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "隐患级别",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "帮扶人员",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "发现时间",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "整改截止时间",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createColumnTextItem(title: "整改意见", text: "测试数据"),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "隐患状态",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createTextImageItem(
        text: "隐患照片",
        imageUrls: ["https://picsum.photos/id/237/200/300"],
        onImageTapped: (index) {
          present(
            SingleImageViewer(imageUrl: "https://picsum.photos/id/237/200/300"),
            context,
          );
        },
      ),
    ]);

    // 整改信息部分
    final List<Widget> correctionMessages = _buildSectionItems([
      EditListItems.createRowSpaceBetweenItem(
        leftText: "整改人",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createRowSpaceBetweenItem(
        leftText: "整改时间",
        rightText: "测试数据",
        verticalPadding: 0,
      ),
      EditListItems.createColumnTextItem(title: "整改描述", text: "测试数据"),
      EditListItems.createTextImageItem(
        text: "整改图片",
        imageUrls: ["https://picsum.photos/id/237/200/300"],
        onImageTapped: (index) {
          present(
            SingleImageViewer(imageUrl: "https://picsum.photos/id/237/200/300"),
            context,
          );
        },
      ),
      EditListItems.createTextImageItem(
        text: "本次整改报告照片",
        imageUrls: ["https://picsum.photos/id/237/200/300"],
        onImageTapped: (index) {
          present(
            SingleImageViewer(imageUrl: "https://picsum.photos/id/237/200/300"),
            context,
          );
        },
      ),
    ]);

    // 复查信息部分
    final List<Widget> reviewMessages = _buildSectionItems([
      EditListItems.createYesNoSection(
        title: "是否合格",
        yesLabel: "是",
        noLabel: "否",
        horizontalPadding: 0,
        verticalPadding: 0,
        groupValue: _isQualified,
        isImportant: true,
        onChanged: (val) {
          setState(() => _isQualified = val);
        },
      ),
      if (!_isQualified)
        EditListItems.createBuildMultilineInput(label: "原因", hint: "请输入原因",isImportent: true,isVal: true, controller: _reasonEditController),

      EditListItems.createRowSpaceBetweenItem(
        leftText: "复查时间",
        rightText: _repeatTime,
        isImportent: true,
        isRight: true,
        verticalPadding: 0,
        onTap: () {
          _showDateTimePicker(context);
        },
      ),
    ]);

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: MyAppbar(
        title: widget.type == DangerDetailType.detail ? "隐患详情" : "复查详情",
      ),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                // 添加滚动支持
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    const SizedBox(height: 10),
                    _buildSectionHeader("隐患信息"),
                    ...dangerMessages,
                    const SizedBox(height: 20),

                    if (widget.type == DangerDetailType.repeat) ...[
                      _buildSectionHeader("整改信息"),
                      ...correctionMessages,
                      const SizedBox(height: 20),

                      _buildSectionHeader("隐患复查"),
                      ...reviewMessages,
                      Divider(),
                      EditListItems.createRowSpaceBetweenItem(
                        leftText: "现场复查照片",
                        isImportent: true,
                        rightText: "",
                        isRight: false,
                        horizontalPadding: 10,
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        child: MediaPickerRow(
                          maxCount: 16,
                          onChanged: (List<File> images) {},
                        ),
                      ),
                      const SizedBox(height: 20),

                      CustomButton(
                        text: "保存",
                        backgroundColor: Colors.blue,
                        onPressed: () {},
                      ),
                      const SizedBox(height: 20),
                    ],
                  ],
                ),
              ),
            ),
            if (widget.type == DangerDetailType.repeat)
              EditListItems.bottomTipWidget()
          ],
        ),
      ),
    );
  }

  // 构建部分标题
  Widget _buildSectionHeader(String title) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 10),
      child: DashedLineText(
        text: title,
        lineColor: h_mainBlueColor(),
        textStyle: TextStyle(fontSize: 16, color: h_mainBlueColor()),
      ),
    );
  }
}
