import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/danner_repain_item.dart';
import 'package:qhd_prevention/customWidget/department_picker.dart';
import 'package:qhd_prevention/customWidget/search_bar_widget.dart';
import 'package:qhd_prevention/pages/home/scan_page.dart';
import 'package:qhd_prevention/pages/home/work/risk_list_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/SmallWidget.dart';
import 'package:qhd_prevention/tools/tools.dart';

import '../../http/ApiService.dart';

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

  @override
  _AiAlarmPageState createState() => _AiAlarmPageState();
}

class _AiAlarmPageState extends State<AiAlarmPage>
    with SingleTickerProviderStateMixin {

  late List<dynamic> _list = [];
  int pageNum = 1;

  // 模拟数据
  final List<NotificationItem> _notifications = List.generate(10, (i) {
    bool read = i % 3 == 0;
    String title = '测试数据标题标题 ${i + 1}';
    String time = '2025-06-${10 + i} 12:3${i}';
    return NotificationItem(title, time);
  });

  final TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
      _getAiAlarmList("");
  }

  @override
  void dispose() {

    super.dispose();
  }

  void _handleItemTap(NotificationItem item, int index) {
    print("点击了是: ${item.title}");
    // pushPage(RiskListPage(), context);
  }

  // 显示分类选择器
  void showCategoryPicker() {
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      barrierColor: Colors.black54,
      backgroundColor: Colors.transparent,
      builder:
          (ctx) => DepartmentPicker(onSelected: (id, name) {

          }),
    );
  }

  Future<void> _getAiAlarmList(String keyWord) async {
    // LoadingDialogHelper.show();
    try {
      final result = await ApiService.getAiAlarmList("-1", pageNum.toString(),keyWord);
      if (result['result'] == 'success') {
        final List<dynamic> newList = result['varList'] ?? [];
        setState(() {
          _list.addAll(newList);
        });
      }
    } catch (e) {
      print('加载出错: $e');
    }

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(
        title: "AI报警",
      ),
      body: SafeArea(
        child: Column(
          children: [
            // Search bar
            Padding(
              padding: const EdgeInsets.all(10),
              child: SearchBarWidget(
                showResetButton: true,
                onInputTap: () {
                  showCategoryPicker();
                },
                hintText: "",
                isClickableOnly: true,
                onSearch: (text) {
                  // print('----------');
                  _getAiAlarmList(text);
                },
                controller: _searchController,
              ),
            ),

            // List
            Expanded(
              child: _list.isEmpty
                  ? NoDataWidget.show()
                  : ListView.separated(
                  itemCount: _notifications.length,
                  separatorBuilder: (_, __) => const SizedBox(),
                  itemBuilder: (context, index) {
                    // NotificationItem item = _notifications[index];
                    // return GestureDetector(
                    //   onTap: () => _handleItemTap(item, index),
                    //   child: DannerRepainItem(
                    //     title: '测试--------new',
                    //     details: [
                    //       '清单类型：测试',
                    //       '排查周期：测试',
                    //       '包含检查项：3',
                    //       '负责人：是测试',
                    //       '起始时间：2025-6-20',
                    //       '',
                    //       '测试一下是否跳过时间',
                    //     ],
                    //     showBottomTags: true,
                    //     bottomTags: [
                    //       riskTagText(1, "重大风险:0"),
                    //       riskTagText(2, "较大:3"),
                    //       riskTagText(3, "一般:1"),
                    //       riskTagText(4, "低:0"),
                    //     ],
                    //   ),
                    // );
                  },
                ),
            ),
          ],
        ),
      ),
    );
  }


}



// 模拟数据模版
class NotificationItem {
  final String title;
  final String time;

  NotificationItem(this.title, this.time);
}
