import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/ItemWidgetFactory.dart';
import 'package:qhd_prevention/http/ApiService.dart';
import 'package:qhd_prevention/pages/KeyProjects/Danger/danger_list_page.dart';
import 'package:qhd_prevention/pages/KeyProjects/KeyProject/keyProject_list_page.dart';
import 'package:qhd_prevention/pages/KeyProjects/Punishment/punishment_list_page.dart';
import 'package:qhd_prevention/pages/KeyProjects/SafeCheck/safeCheck_list_page.dart';
import 'package:qhd_prevention/pages/home/tap/tabList/work_tab_icon_grid.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/tools.dart';


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

  @override
  State<KeyprojectsTabList> createState() => _KeyprojectsTabListState();
}

class _KeyprojectsTabListState extends State<KeyprojectsTabList> {
  late List<Map<String, dynamic>> buttonInfos = [
    {
      "icon": "assets/icon-apps/icon-yxkj-1.png",
      "title": "重点工程管理",
      "unreadCount": '0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-1.png",
      "title": "安全检查管理",
      "unreadCount": '0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-1.png",
      "title": "隐患管理",
      "unreadCount":'0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-1.png",
      "title": "处罚管理",
      "unreadCount":'0',
    },

  ];

  @override
  void initState() {
    super.initState();
    _getData();
  }
  Future<void> _getData() async {
    LoadingDialogHelper.show();
    final data = await ApiService.getKeyProjectCount();
    LoadingDialogHelper.hide();
    setState(() {
      final eight_work_count = data['pd'] ?? {};
      buttonInfos = [
        {
          "icon": "assets/icon-apps/icon-yxkj-1.png",
          "title": "重点工程管理",
          "unreadCount": eight_work_count['GC_COUNT'] ?? '0',
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-1.png",
          "title": "安全检查管理",
          "unreadCount": '0',
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-1.png",
          "title": "隐患管理",
          "unreadCount": eight_work_count['CF_COUNT'] ?? '0',
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-1.png",
          "title": "处罚管理",
          "unreadCount": eight_work_count['HIDDEN_COUNT'] ?? '0',
        },

      ];
    });
  }
  void _handleItemPressed(int index) async {
    // 根据索引执行不同的导航逻辑
    String title = '';
    switch (index) {
      case 0: {
        title = '重点工程管理';
        await pushPage(KeyprojectListPage(flow: title), context);
      } break;
      case 1: {
        title = '安全检查管理';
        await pushPage(SafecheckListPage(flow: title), context);

      } break;
      case 2: {
        title = '隐患管理';
        await pushPage(DangerListPage(flow: title), context);

      } break;
      case 3: {
        title = '处罚管理';
        await pushPage(PunishmentListPage(flow: title), context);

      } break;

      default:
        print("按钮 $index 被点击");
    }

    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: '重点工程管理'),
      body: SafeArea(
        child: Column(
          children: [
            ListItemFactory.createBuildSimpleSection('重点工程管理'),
            WorkTabIconGrid(
              buttonInfos: buttonInfos,
              onItemPressed: _handleItemPressed,
            ),
          ],
        )
      ),
    );
  }
}
