import 'dart:ui';

import 'package:flutter/material.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';

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

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

class _DangerPageState extends State<DangerPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  int _selectedTab = 0;

  // 模拟数据
  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 List<Category> data = [
    Category(
      id: '1',
      title: '分类一',
      children: [
        Category(id: '1-1', title: '子项 1-1'),
        Category(id: '1-2', title: '子项 1-2'),
      ],
    ),
    Category(id: '2', title: '分类二'),
    Category(
      id: '3',
      title: '分类三',
      children: [
        Category(
          id: '3-1',
          title: '子项 3-1',
          children: [Category(id: '3-1-1', title: '子项 3-1-1')],
        ),
      ],
    ),
  ];
  final TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    _tabController.addListener(() {
      if (!_tabController.indexIsChanging) {
        setState(() => _selectedTab = _tabController.index);
      }
    });
  }

  @override
  void dispose() {
    _tabController.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(
            data: data,
            onSelected: (selectedId) {
              setState(() {});
            },
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(
        title: "隐患排查",
        actions: [
          TextButton(
            onPressed: () {
              pushPage(ScanPage(), context);
            },
            child: Text(
              "清单扫描",
              style: TextStyle(color: Colors.white, fontSize: 16),
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: Column(
          children: [
            // Tab bar
            TabBar(
              controller: _tabController,
              labelStyle: TextStyle(fontSize: 16),
              indicator: UnderlineTabIndicator(
                borderSide: BorderSide(width: 3.0, color: Colors.blue),
                insets: EdgeInsets.symmetric(horizontal: 100.0),
              ),
              labelColor: Colors.blue,
              unselectedLabelColor: Colors.grey,
              tabs: const [Tab(text: '待排查'), Tab(text: '已排查')],
            ),

            // Search bar
            Padding(
              padding: const EdgeInsets.all(10),
              child: SearchBarWidget(
                showResetButton: true,
                onInputTap: () {
                  showCategoryPicker();
                },
                hintText: "",
                isClickableOnly: true,
                onSearch: (text) {
                  print('----------');
                },
                controller: _searchController,
              ),
            ),

            // List
            Expanded(
              child: ListView.separated(
                itemCount: _notifications.length,
                separatorBuilder: (_, __) => const SizedBox(),
                itemBuilder: (context, index) {
                  NotificationItem item = _notifications[index];
                  return GestureDetector(
                    onTap: () => _handleItemTap(item, index),
                    child: _itemCell(item),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _itemCell(final item) {
    return Padding(
      padding: EdgeInsets.only(left: 15, right: 15, bottom: 15),
      child: Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5)),
        ),
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Icon(
                        Icons.star_rate_sharp,
                        color: Colors.green,
                        size: 18,
                      ),
                      SizedBox(width: 5),
                      Text("测试--------", style: TextStyle(fontSize: 14)),
                    ],
                  ),
                  Icon(
                    Icons.arrow_forward_ios_rounded,
                    color: Colors.grey,
                    size: 15,
                  ),
                ],
              ),
            ),
            Divider(height: 1),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              child: Column(
                spacing: 5,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      HhTextStyleUtils.secondaryTitle("清单类型：测试"),
                      HhTextStyleUtils.secondaryTitle("排查周期：测试"),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      HhTextStyleUtils.secondaryTitle("包含检查项：3"),
                      HhTextStyleUtils.secondaryTitle("负责人：是测试"),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      HhTextStyleUtils.secondaryTitle(
                        "起始时间：2025-6-20 - 2026-3-31",
                      ),
                      SizedBox(),
                    ],
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 0, bottom: 15, left: 15, right: 15),
              child: Row(
                spacing: 5,
                children: [
                  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);
}
