import 'package:flutter/material.dart';

class DashboardDemoView extends StatelessWidget {
  const DashboardDemoView({super.key});

  @override
  Widget build(BuildContext context) {
    const borderRadius = Radius.circular(8);
    return ListView(
      children: [
        Container(
          height: 150,
          padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(borderRadius),
          ),
          child: const Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _CountCircleWidget(
                count: 100,
                title: "日新增签约人数",
                color: Colors.red,
              ),
              _CountCircleWidget(
                count: 100,
                title: "已随访总人数",
                color: Colors.purple,
              ),
              _CountCircleWidget(
                count: 100,
                title: "累计签约总人数",
                color: Colors.green,
              ),
              _CountCircleWidget(
                count: 100,
                title: "签约逾期总数",
                color: Colors.blue,
              ),
            ],
          ),
        ),
        const SizedBox(height: 10),
        Column(
          children: [
            Container(
              height: 40,
              padding: const EdgeInsets.symmetric(horizontal: 14),
              decoration: BoxDecoration(
                color: Theme.of(context).primaryColor,
                borderRadius: const BorderRadius.only(
                  topLeft: borderRadius,
                  topRight: borderRadius,
                ),
              ),
              child: const Row(
                children: [
                  Icon(Icons.notifications_active_outlined,
                      color: Colors.white),
                  Text(
                    "工作提醒",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 20),
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  bottomLeft: borderRadius,
                  bottomRight: borderRadius,
                ),
              ),
              // height: 100,
              child: _DemoTable(),
            ),
          ],
        ),
      ],
    );
  }
}

class _DemoTable extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final source = [
      "肺结核",
      "冠心病",
      "脑卒中",
      "严重精神病",
      "糖尿病",
      "高血压",
      "老年人",
      "月产妇",
      "儿童",
    ];
    final rows = <TableRow>[];
    for (var i = 0; i < source.length; i++) {
      rows.add(_buildRow(source[i], i));
    }
    return Table(
      children: [
        TableRow(
          decoration: const BoxDecoration(
            color: Color.fromRGBO(215, 234, 255, 1),
          ),
          children: [
            _buildHeaderCell("重点人群"),
            _buildHeaderCell("首次随访未进行"),
            _buildHeaderCell("本日应随访"),
            _buildHeaderCell("本周应随访"),
            _buildHeaderCell("逾期随访"),
          ],
        ),
        ...rows,
      ],
    );
  }

  TableRow _buildRow(String title, int index) {
    return TableRow(
      decoration: index % 2 == 0
          ? null
          : const BoxDecoration(
              color: Color.fromRGBO(233, 244, 255, 1),
            ),
      children: [
        _buildDataCell(title),
        _buildDataCell("0"),
        _buildDataCell("0"),
        _buildDataCell("0"),
        _buildDataCell("0"),
      ],
    );
  }

  TableCell _buildHeaderCell(String title) {
    return TableCell(
      child: Container(
        alignment: Alignment.center,
        height: 37,
        child: Text(
          title,
          style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  TableCell _buildDataCell(String title) {
    return TableCell(
      child: Container(
        alignment: Alignment.center,
        height: 37,
        child: Text(
          title,
          style: const TextStyle(fontSize: 14),
        ),
      ),
    );
  }
}

class _CountCircleWidget extends StatelessWidget {
  final int count;
  final String title;
  final Color color;

  const _CountCircleWidget({
    super.key,
    required this.count,
    required this.title,
    required this.color,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          width: 94,
          height: 94,
          decoration: BoxDecoration(
            border: Border.all(
              width: 5,
              color: color,
            ),
            borderRadius: BorderRadius.circular(94 / 2),
          ),
          alignment: Alignment.center,
          child: Text(
            count.toString(),
            style: TextStyle(
              color: color,
              fontSize: 20,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        const SizedBox(height: 8),
        Text(
          title,
          style: const TextStyle(fontSize: 14),
        ),
      ],
    );
  }
}
