import 'dart:ui';

import 'package:flutter/material.dart';


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

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

class _DangerPageState extends State<DangerPage> {
  String _selectedOption = '全部';
  final TextEditingController _searchController = TextEditingController();
  final List<Map<String, String>> enterprises = [
    {
      'name': '泰安安全',
      'type': '冶金行业',
      'unit': '执法一中队',
      'status': '已划分'
    },
    {
      'name': '秦皇岛杰瑞科技有限公司',
      'type': '轻工行业',
      'unit': '执法一中队',
      'status': '已划分'
    },
    {
      'name': '秦皇岛异特机械设备有限公司',
      'type': '机械行业',
      'unit': '执法一中队',
      'status': '已划分'
    },
    {
      'name': '秦皇岛利华德科技有限公司',
      'type': '机械行业',
      'unit': '执法一中队',
      'status': '已划分'
    },
    {
      'name': '广东胜捷消防科技有限公司秦皇岛分公司',
      'type': '机械行业',
      'unit': '执法一中队',
      'status': '未划分'
    },
    {
      'name': '河北联技安全系统有限公司',
      'type': '轻工行业',
      'unit': '执法一中队',
      'status': '已划分'
    },
    {
      'name': '秦皇岛市万天科技有限公司',
      'type': '轻工行业',
      'unit': '执法一中队',
      'status': '未划分'
    },
    {
      'name': '秦皇岛市非晶科技有限公司',
      'type': '轻工行业',
      'unit': '执法一中队',
      'status': '已划分'
    },
    {
      'name': '海湾安全技术有限公司',
      'type': '轻工行业',
      'unit': '执法一中队,西区消防大队',
      'status': '已划分'
    },
    {
      'name': '秦皇岛海湾塑胶金属制品有限公司',
      'type': '轻工行业',
      'unit': '执法一中队',
      'status': '未划分'
    },
  ];

  List<Map<String, String>> get filteredEnterprises {
    if (_searchController.text.isNotEmpty) {
      return enterprises
          .where((e) => e['name']!.contains(_searchController.text))
          .toList();
    }

    if (_selectedOption == '全部') {
      return enterprises;
    } else {
      return enterprises
          .where((e) => e['status'] == _selectedOption)
          .toList();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('企业信息'),
        centerTitle: true,
        actions: [
          IconButton(
            icon: const Icon(Icons.filter_alt),
            onPressed: () {
              // 筛选功能
            },
          ),
        ],
      ),
      body: Column(
        children: [
          _buildFunctionButtons(),
          _buildSearchSection(),
          _buildFilterSection(),
          _buildEnterpriseList(),
        ],
      ),
    );
  }

  Widget _buildFunctionButtons() {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
      color: Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildFunctionButton('监管划分', Icons.assignment),
          _buildFunctionButton('信息审核', Icons.verified),
          _buildFunctionButton('持证人员', Icons.badge),
        ],
      ),
    );
  }

  Widget _buildFunctionButton(String label, IconData icon) {
    return Column(
      children: [
        Container(
          padding: const EdgeInsets.all(12),
          decoration: BoxDecoration(
            color: const Color(0xFF0D47A1).withOpacity(0.1),
            borderRadius: BorderRadius.circular(12),
          ),
          child: Icon(icon, size: 30, color: const Color(0xFF0D47A1)),
        ),
        const SizedBox(height: 8),
        Text(label, style: const TextStyle(fontSize: 14)),
      ],
    );
  }

  Widget _buildSearchSection() {
    return Container(
      padding: const EdgeInsets.all(16),
      color: Colors.white,
      child: Row(
        children: [
          Expanded(
            child: TextField(
              controller: _searchController,
              decoration: InputDecoration(
                hintText: '请输入关键字',
                prefixIcon: const Icon(Icons.search),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(30),
                  borderSide: BorderSide.none,
                ),
                filled: true,
                fillColor: Colors.grey[100],
                contentPadding: const EdgeInsets.symmetric(
                    vertical: 12, horizontal: 20),
              ),
              onChanged: (value) {
                setState(() {});
              },
            ),
          ),
          const SizedBox(width: 8),
          Container(
            decoration: BoxDecoration(
              color: const Color(0xFF0D47A1),
              borderRadius: BorderRadius.circular(30),
            ),
            child: IconButton(
              icon: const Icon(Icons.search, color: Colors.white),
              onPressed: () {
                setState(() {});
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFilterSection() {
    return Container(
      padding: const EdgeInsets.all(16),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text('请选择部门', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
          const SizedBox(height: 12),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _buildFilterOption('全部'),
              _buildFilterOption('已划分'),
              _buildFilterOption('未划分'),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildFilterOption(String text) {
    final isSelected = _selectedOption == text;

    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedOption = text;
        });
      },
      child: Row(
        children: [
          Container(
            width: 20,
            height: 20,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              border: Border.all(
                color: isSelected ? const Color(0xFF0D47A1) : Colors.grey,
                width: 2,
              ),
            ),
            child: isSelected
                ? Center(
              child: Container(
                width: 12,
                height: 12,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  color: Color(0xFF0D47A1),
                ),
              ),
            )
                : null,
          ),
          const SizedBox(width: 8),
          Text(text, style: TextStyle(
            fontSize: 16,
            color: isSelected ? const Color(0xFF0D47A1) : Colors.grey,
            fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
          )),
        ],
      ),
    );
  }

  Widget _buildEnterpriseList() {
    return Expanded(
      child: ListView.builder(
        padding: const EdgeInsets.only(top: 8),
        itemCount: filteredEnterprises.length,
        itemBuilder: (context, index) {
          final enterprise = filteredEnterprises[index];
          return _buildEnterpriseCard(enterprise);
        },
      ),
    );
  }

  Widget _buildEnterpriseCard(Map<String, String> enterprise) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 8,
            offset: const Offset(0, 4),
          )
        ],
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  enterprise['name']!,
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Container(
                  padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                  decoration: BoxDecoration(
                    color: enterprise['status'] == '已划分'
                        ? const Color(0xFF4CAF50).withOpacity(0.1)
                        : const Color(0xFFF44336).withOpacity(0.1),
                    borderRadius: BorderRadius.circular(4),
                    border: Border.all(
                      color: enterprise['status'] == '已划分'
                          ? const Color(0xFF4CAF50)
                          : const Color(0xFFF44336),
                      width: 1,
                    ),
                  ),
                  child: Text(
                    enterprise['status']!,
                    style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.bold,
                      color: enterprise['status'] == '已划分'
                          ? const Color(0xFF4CAF50)
                          : const Color(0xFFF44336),
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 12),
            Row(
              children: [
                const Icon(Icons.business, size: 16, color: Colors.grey),
                const SizedBox(width: 8),
                Text(
                  '安全监管类型: ${enterprise['type']}',
                  style: const TextStyle(fontSize: 14, color: Colors.grey),
                ),
              ],
            ),
            const SizedBox(height: 8),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Icon(Icons.people, size: 16, color: Colors.grey),
                const SizedBox(width: 8),
                Expanded(
                  child: Text(
                    '管理单位: ${enterprise['unit']}',
                    style: const TextStyle(fontSize: 14, color: Colors.grey),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 8),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton(
                  onPressed: () {
                    // 查看详情
                  },
                  style: TextButton.styleFrom(
                    foregroundColor: const Color(0xFF0D47A1),
                  ),
                  child: const Text('查看详情'),
                ),
                const SizedBox(width: 8),
                ElevatedButton(
                  onPressed: () {
                    // 处理操作
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0D47A1),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                  child: const Text('处理', style: TextStyle(color: Colors.white)),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

