import 'package:flutter/material.dart';
import 'package:dotted_border/dotted_border.dart';
import 'package:qhdkfq_regulatory_flutter/Custom/edit_list_items.dart';
import 'package:qhdkfq_regulatory_flutter/home/department_picker.dart';

import '../mock/mock_data.dart';

class HelpDeptPersonSection extends StatefulWidget {
  const HelpDeptPersonSection({Key? key}) : super(key: key);

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

class HelpDeptPersonSectionState extends State<HelpDeptPersonSection> {
  final List<Map<String, String>> _entries = [
    {'dept': '请选择', 'person': '请选择'},
  ];
// 已选择的分类 id
  String? _selectedCategoryId;
  /// 外部调用：新增一条
  void addEntry() {
    setState(() => _entries.add({'dept': '', 'person': ''}));
  }

  /// 外部调用：获取当前所有条目
  List<Map<String, String>> get entries => List.unmodifiable(_entries);

  void _removeEntry(int i) {
    setState(() => _entries.removeAt(i));
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        for (var i = 0; i < _entries.length; i++) ...[
          Stack(
            clipBehavior: Clip.none,  // 关闭裁剪
            children: [
              DottedBorder(
                color: Colors.grey,
                strokeWidth: 1,
                dashPattern: [4, 2],
                borderType: BorderType.RRect,
                radius: const Radius.circular(8),
                child: Padding(
                  padding: const EdgeInsets.all(12),
                  child: Column(
                    children: [
                      EditListItems.createRowSpaceBetweenItem(
                        leftText: '帮扶部门',
                        rightText: _entries[i]['dept']!,
                        isImportent: true,
                        isRight: true,
                        onTap: () {
                          // TODO: 外部弹框或选择逻辑
                          showModalBottomSheet(
                            context: context,
                            isScrollControlled: true,
                            barrierColor: Colors.black54,
                            backgroundColor: Colors.transparent,
                            builder:
                                (ctx) => DepartmentPicker(
                              data: mockCategories,
                              onSelected: (selectedId) {
                                setState(() {
                                  _selectedCategoryId = selectedId;
                                });
                              },
                            ),
                          );
                          print(mockCategories.map((e) => e.title));

                        },
                      ),
                      const Divider(height: 8),
                      EditListItems.createRowSpaceBetweenItem(
                        leftText: '帮扶人员',
                        rightText: _entries[i]['person']!,
                        isImportent: true,
                        isRight: true,
                        onTap: () {
                          // TODO: 外部弹框或选择逻辑
                        },
                      ),
                    ],
                  ),
                ),
              ),
              if (_entries.length > 1)
                // 删除按钮
                Positioned(
                  top: -2,
                  right: -2,
                  child: GestureDetector(
                    onTap: () => _removeEntry(i),
                    child: Container(
                      width: 24,
                      height: 24,
                      decoration: const BoxDecoration(
                        color: Colors.red,
                        shape: BoxShape.circle,
                      ),
                      child: const Icon(
                        Icons.close,
                        size: 16,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
            ],
          ),
          const SizedBox(height: 12),
        ],
      ],
    );
  }
}
