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/SafeCheck/safeCheck_list_page.dart';
import 'package:qhd_prevention/pages/home/SafeCheck/CheckPersonSign/safeCheck_sign_list_page.dart';
import 'package:qhd_prevention/pages/home/SafeCheck/CheckPersonSure/check_person_list_page.dart';
import 'package:qhd_prevention/pages/home/SafeCheck/DangeCheck/safeCheck_danger_list_page.dart';
import 'package:qhd_prevention/pages/home/SafeCheck/Record/defend_record_list_page.dart';
import 'package:qhd_prevention/pages/home/SafeCheck/Start/safeCheck_start_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 SafecheckTabList extends StatefulWidget {
  const SafecheckTabList({super.key});

  @override
  State<SafecheckTabList> createState() => _SafecheckTabListState();
}

class _SafecheckTabListState extends State<SafecheckTabList> {
  late List<Map<String, dynamic>> buttonInfos = [
    {
      "icon": "assets/icon-apps/icon-yxkj-1.png",
      "title": "安全检查\n发起",
      "unreadCount": '0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-4.png",
      "title": "检查人\n确认",
      "unreadCount": '0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-2.png",
      "title": "被检查人\n签字/申辩",
      "unreadCount":'0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-2.png",
      "title": "隐患指派\n及验收",
      "unreadCount":'0',
    },
    {
      "icon": "assets/icon-apps/icon-yxkj-2.png",
      "title": "申辩记录",
      "unreadCount":'0',
    },

  ];

  @override
  void initState() {
    super.initState();
    _getData();
  }
  Future<void> _getData() async {
    final data = await ApiService.getSafeCheckCount();
    setState(() {
      final checkedCount = data['checkedCount']['checkedCount'] ?? '';
      final repulseAndCheckCount = data['repulseAndCheckCount']['repulseAndCheckCount'] ?? '';
      final confirmCount = data['confirmCount']['confirmCount'] ?? '';
      final repulseCount = data['repulseCount']['repulseCount'] ?? '';

      buttonInfos = [
        {
          "icon": "assets/icon-apps/icon-yxkj-1.png",
          "title": "安全检查\n发起",
          "unreadCount": repulseCount,
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-4.png",
          "title": "检查人\n确认",
          "unreadCount": confirmCount,
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-2.png",
          "title": "被检查人\n签字/申辩",
          "unreadCount":checkedCount,
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-2.png",
          "title": "隐患指派\n及验收",
          "unreadCount":repulseAndCheckCount,
        },
        {
          "icon": "assets/icon-apps/icon-yxkj-2.png",
          "title": "申辩记录",
          "unreadCount":'0',
        },

      ];
    });
  }
  void _handleItemPressed(int index) async {
    // 根据索引执行不同的导航逻辑
    String title = '';
    switch (index) {
      case 0: {
        title = '安全检查发起';
        await pushPage(SafecheckStartListPage(flow: title), context);
      } break;
      case 1: {
        title = '安全检查核实';
        await pushPage(CheckPersonListPage(flow: title), context);

      } break;
      case 2: {
        title = '安全检查确认';
        await pushPage(SafecheckSignListPage(flow: title), context);

      } break;
      case 3: {
        title = '隐患指派及验收';
        await pushPage(SafecheckDangerListPage(flow: title), context);

      } break;
      case 4: {
        title = '申辩记录';
        await pushPage(DefendRecordListPage(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,
            ),
          ],
        )
      ),
    );
  }
}
