import 'package:flutter/material.dart';
import 'package:qhdkfq_regulatory_flutter/home/help/danger_edit_page.dart';
import 'package:qhdkfq_regulatory_flutter/home/help/help_detail_page.dart';
import 'package:qhdkfq_regulatory_flutter/home/home_page.dart';
import 'package:qhdkfq_regulatory_flutter/tools/my_appbar.dart';

import '../../tools/custom_button.dart';
import '../../tools/tools.dart';
import '../adaptive_list_item.dart';

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

  @override
  State<HelpRecordPage> createState() => _HelpRecordPageState();
}

class _HelpRecordPageState extends State<HelpRecordPage> {
  int _selectRadioIndex = 0;

  // 选项列表也可以放到 State 里，或者在 build 里声明都行
  final List<String> _radioTitles = [
    "日常检查",
    "专项检查",
    "总和检查",
    "执法检查",
    "举报检查",
  ];
  /// 模拟企业数据
  final List<Map<String, dynamic>> _enterpriseData = List.generate(15, (index) {
    return {
      'id': index,
      'name': '${['化工', '制造', '能源', '食品', '建筑'][index % 5]}企业${index + 1}',
      'creditCode': '9137${10000 + index}',
      'industry': ['化工', '制造', '能源', '食品', '建筑'][index % 5],
      'scale': ['大型', '中型', '小型'][index % 3],
      'safetyLevel': ['A', 'B', 'C'][index % 3],
      'lastCheck': '2025-0${6 + index % 3}-${10 + index % 20}',
      'riskCount': index % 4,
      'hiddenDangerCount': index % 5,
      'isDivided': index % 3 == 0,
    };
  });
  Widget _buildDynamicRadioRow({
    required List<String> options, // 选项文本数组
    required int? selectedValue, // 当前选中的值（索引）
    required ValueChanged<int?> onChanged, // 选项变更回调
  }) {
    return Wrap(
      spacing: 5, // 横向间距
      runSpacing: 5, // 纵向行距
      children: List.generate(options.length, (i) {
        return _buildRadioOption(
          isSelected: selectedValue == i,
          label: options[i],
          value: i,
          onChanged: onChanged,
        );
      }),
    );
  }
  Widget _listViewWidget() {
    return ListView.builder(
      itemCount: 3,
      itemBuilder: (context, index) {
        final enterprise = _enterpriseData[index];
        return Card(
          color: Colors.white,
          margin: const EdgeInsets.symmetric(horizontal: 0, vertical: 6),
          elevation: 0.5,
          shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
          child: Padding(
            padding: const EdgeInsets.all(15),
            child: AdaptiveListItem(
              title: "测试",
              items: _getEnterpriseProperties(enterprise),
              actions: _listItemButtons(),
              type: HomeCategroyType.supervisionSupport,
              selectTabIndex: 0,
            ),
          ),
        );
      },
    );
  }
  List<Widget> _listItemButtons() {

    return [
      CustomButton(
        height: 40,
        text: "编辑",
        backgroundColor: Colors.blue,
        padding: EdgeInsets.symmetric(horizontal: 30),
        onPressed: () {
          pushPage(DangerEditPage(HelpEditType.edit), context);
        },
      ),
      CustomButton(
        height: 40,
        text: "查看",
        backgroundColor: Colors.blue,
        padding: EdgeInsets.symmetric(horizontal: 30),
        onPressed: () {
          pushPage(HelpDetailPage(), context);
        },
      ),
      CustomButton(
        height: 40,
        text: "删除",
        backgroundColor: Colors.red,
        padding: EdgeInsets.symmetric(horizontal: 30),
        onPressed: () {
          // TODO ---
        },
      ),
    ];
  }
  /// 自定义Radio
  Widget _buildRadioOption({
    required bool isSelected,
    required String label,
    required int value,
    required ValueChanged<int?> onChanged,
  }) {
    return InkWell(
      onTap: () => onChanged(value),
      splashColor: Colors.blue,
      borderRadius: BorderRadius.circular(20),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 自定义单选按钮 - 带对勾样式
          SizedBox(
            child:
                isSelected
                    ? const Icon(
                      Icons.check_circle,
                      size: 22,
                      color: Colors.blue,
                    )
                    : const Icon(
                      Icons.circle_outlined,
                      size: 22,
                      color: Colors.grey,
                    ),
          ),
          SizedBox(width: 10),
          Text(
            label,
            style: TextStyle(
              fontSize: 15,
              color: isSelected ? Colors.blue : Colors.grey[700],
              fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
            ),
          ),
          SizedBox(width: 10),
        ],
      ),
    );
  }
  List<Map<String, String>> _getEnterpriseProperties(
      Map<String, dynamic> enterprise,
      ) {
    return [
      {
        "title": "检查类型",
        "value": ["常规", "突击", "专项"][enterprise['id'] % 3],
      },
      {
        "title": "检查结果",
        "value": ["合格", "需整改", "不合格"][enterprise['id'] % 3],
      },
      {"title": "检查日期", "value": enterprise['lastCheck']},
      {
        "title": "检查人",
        "value": "李${['工', '主任', '专员'][enterprise['id'] % 3]}",
      },
    ];
  }
  @override
  Widget build(BuildContext context) {
    late List<String> radioTitles = ["日常检查","专项检查","总和检查","执法检查","举报检查"];

    /// 选择的radio
    late int selectRadioIndex = 0;
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: MyAppbar(title: "帮扶记录"),
      body: SafeArea(
        child: Column(
          children: [
            Container(
              width: double.maxFinite,
              padding: EdgeInsets.all(15),
              color: Colors.white,
              child: _buildDynamicRadioRow(
                options: _radioTitles,
                selectedValue: _selectRadioIndex,
                onChanged: (val) {
                  setState(() {
                    _selectRadioIndex = val!;
                  });
                },
              ),
            ),
            SizedBox(height: 10,),
            Expanded(child: _listViewWidget()),

          ],
        ),
      ),
    );
  }


}
