import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/custom_driver_drawer.dart';
import 'package:qhd_prevention/pages/home/risk/risk_detail_page.dart';
import 'package:qhd_prevention/pages/home/work/danger_repair_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/h_colors.dart';
import 'package:qhd_prevention/tools/tools.dart';
import '/customWidget/search_bar_widget.dart';

enum DangerType {
  wait("待整改隐患", "隐患整改"),
  expired("超期未整改", "超期未整改-详情"),
  waitAcceptance("隐患验收", "隐患验收"),
  acceptance("已验收隐患", "已验收隐患");

  final String displayName;
  final String detailTitle;

  const DangerType(this.displayName, this.detailTitle);
}
class DangerWaitListPage extends StatefulWidget {
  const DangerWaitListPage(this.dangerType,{super.key});
  final DangerType dangerType;
  @override
  State<DangerWaitListPage> createState() => _DangerWaitListPageState();
}

class _DangerWaitListPageState extends State<DangerWaitListPage> {
  // 模拟数据
  final List<Map<String, dynamic>> _dataInfos = List.generate(10, (i) {
    bool read = i % 3 == 0;
    return {
      'title': '测试数据标题标题 ${i + 1}',
      'label1': '2025-06-${10 + i} 12:3${i}',
      'label2': '2025-06-${1 + i} 2:1${i}',
      'level': getRandomWithNum(1, 2),
    };
  });

  // 隐患等级颜色
  final List<Color> _fxColors = [
    const Color(0xFFFCE6D2),
    const Color(0xFFFDF2CE),
  ];
  final TextEditingController _searchController = TextEditingController();

  // 隐患等级文本
  final List<String> _levelTexts = ["较大隐患","一般隐患"];
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    // 取屏幕宽度
    final double screenWidth = MediaQuery.of(context).size.width;

    return Scaffold(
      key: _scaffoldKey, // ② 绑定 key
      appBar: MyAppbar(
        title: widget.dangerType.displayName
      ),

      body: SafeArea(
          child: _vcDetailWidget()),
      backgroundColor: Colors.white,
    );
  }

  Widget _vcDetailWidget() {
    return Column(
      children: [
        Padding(
          padding: EdgeInsets.all(10),
          child: SearchBarWidget(
            controller: _searchController,
            onSearch: (keyboard) {
              // 输入请求接口
            },
          ),
        ),
        Container(
          height: 5,
          color: h_backGroundColor(),
        ),
        Expanded(
          child: ListView.separated(
            separatorBuilder: (_, __) => const Divider(height: 1),
            itemCount: _dataInfos.length,
            itemBuilder: (context, index) {
              final item = _dataInfos[index];
              return _fxitemCell(item);
            },
          ),
        ),
      ],
    );
  }

  Widget _fxitemCell(final item) {
    int level = item['level'] as int;
    // 确保level在有效范围内 (1-4)
    level = level.clamp(1, 4) - 1;

    // 使用GestureDetector包裹整个列表项以添加点击事件
    return GestureDetector(
      onTap: () {
        // 点击后跳转到详情页面
        // pushDetailByType(
        //   type: item.type,
        //   context: context,
        // );
        pushPage(DangerRepairPage(widget.dangerType), context);

      },
      child: Container(
        height: 100,
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 16), // 添加水平内边距
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            // 左侧信息列
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    item['title'] ?? '',
                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 8),
                  Text(
                    item['label1'] ?? '',
                    style: TextStyle(fontSize: 14, color: Colors.grey),
                  ),
                  SizedBox(height: 4),
                  Text(
                    item['label2'] ?? '',
                    style: TextStyle(fontSize: 14, color: Colors.grey),
                  ),
                ],
              ),
            ),

            // 右侧风险等级和箭头
            Padding(
              padding: EdgeInsets.only(top: 15),
              child: Column(
                children: [
                  // 风险等级标签
                  Row(
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(
                          horizontal: 8,
                          vertical: 4,
                        ),
                        decoration: BoxDecoration(
                          color: _fxColors[level],
                          borderRadius: BorderRadius.circular(2),
                        ),
                        child: Text(
                          _levelTexts[level],
                          style: TextStyle(fontSize: 12),
                        ),
                      ),
                      SizedBox(width: 30),
                    ],
                  ),
                  SizedBox(height: 12), // 添加间距
                  Row(
                    children: [
                      SizedBox(width: 110),
                      Icon(Icons.arrow_forward_ios_rounded, size: 16),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}



