import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/department_picker.dart';
import '../tools/h_colors.dart';
import '/customWidget/custom_button.dart';
import '../tools/tools.dart';

/// 自定义抽屉
class CustomDriverDrawer extends StatefulWidget {
  const CustomDriverDrawer({super.key});

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

class _CustomDriverDrawerState extends State<CustomDriverDrawer> {
  // 四个选项的单选 index
  int _selectedOption = -1;
  // 已选择的分类 id
  String? _selectedCategoryId;

  @override
  Widget build(BuildContext context) {
    final List<String> items = ["重大风险", "较大风险", "一般风险", "低风险"];
    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'),
          ]),
        ],
      ),
    ];

    // 显示分类选择器
    void showCategoryPicker() {
      showModalBottomSheet(
        context: context,
        isScrollControlled: true,
        barrierColor: Colors.black54,
        backgroundColor: Colors.transparent,
        builder: (ctx) => DepartmentPicker(
          data: data,
          onSelected: (selectedId) {
            setState(() {
              _selectedCategoryId = selectedId;
            });
          },
        ),
      );
    }

    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: const [
                Text(
                  "高级查询",
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
              ],
            ),
            const Divider(height: 24, color: Colors.grey),

            // 管控部门
            _buildDropdownBox(
              "管控部门",
              display: _selectedCategoryId ?? '请选择',
              onTap: showCategoryPicker,
            ),
            const SizedBox(height: 12),

            // 风险点（单元）
            _buildDropdownBox(
              "风险点（单元）",
              display: '请选择',
              onTap: () {
                // TODO: 打开 B 的下拉
              },
            ),

            const SizedBox(height: 24),
            const Text(
              "风险等级",
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
            ),
            const SizedBox(height: 12),

            // 风险等级：一行一个
            Column(
              children: List.generate(items.length, (idx) {
                final bool selected = _selectedOption == idx;
                return GestureDetector(
                  onTap: () => setState(() => _selectedOption = idx),
                  child: Container(
                    width: double.infinity,
                    margin: const EdgeInsets.only(bottom: 8),
                    padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                    decoration: BoxDecoration(
                      color: selected ? Colors.blue : Colors.transparent,
                      borderRadius: BorderRadius.circular(4),
                      border: Border.all(color: Colors.blue, width: 1),
                    ),
                    child: Text(
                      items[idx],
                      style: TextStyle(
                        fontSize: 14,
                        color: selected ? Colors.white : Colors.blue,
                      ),
                    ),
                  ),
                );
              }),
            ),

            const Spacer(),
            Row(
              children: [
                Expanded(
                  flex: 1,
                  child: CustomButton(
                    text: "重置",
                    backgroundColor: h_backGroundColor(),
                    textStyle: const TextStyle(color: Colors.black45),
                    onPressed: () {
                      setState(() {
                        _selectedOption = -1;
                        _selectedCategoryId = null;
                      });
                    },
                  ),
                ),
                const SizedBox(width: 12),
                Expanded(
                  flex: 2,
                  child: CustomButton(
                    text: "完成",
                    backgroundColor: Colors.blue,
                    onPressed: () {
                      // TODO: 提交筛选条件
                    },
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDropdownBox(String title,
      {required String display, required VoidCallback onTap}) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 48,
        padding: const EdgeInsets.symmetric(horizontal: 12),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4),
          border: Border.all(color: Colors.grey.shade400),
          color: Colors.white,
        ),
        child: Row(
          children: [
            Text(title, style: const TextStyle(fontSize: 14)),
            const Spacer(),
            Row(
              children: [
                Text(display),
                const Icon(Icons.arrow_drop_down, color: Colors.grey),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
