/*
 * @Author: XianKaiQun
 * @Date: 2020-07-14 09:17:39
 * @LastEditors: wjc
 * @LastEditTime: 2023-04-03 11:59:41
 * @Description: 
 */
part of '../index.dart';

///统计模块
class _StatisticsComponent extends StatelessWidget {
  const _StatisticsComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateDashBoardStore>(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final textStyle = Theme.of(context).textTheme;
    final department = store.department;

    ///没有数据可认定为没有同级统计，不显示
    if (department == null) {
      return SliverToBoxAdapter();
    }

    Row buildRow({String? name, num? count, num? ratio}) {
      var _count = count ?? 0;
      var _ratio = ((ratio ?? 0) * 100).toStringAsFixed(1);
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text('$name'),
          Text.rich(
            TextSpan(
              children: [
                TextSpan(text: '$_count'),
                if (ratio != null)
                  TextSpan(
                    text: '($_ratio%)',
                    style: TextStyle(
                      color: colorScheme.danger,
                    ),
                  ),
              ],
            ),
          ),
        ],
      );
    }

    return WisCard(
      isSliver: true,
      margin: EdgeInsets.only(left: 15.pt, right: 15.pt, top: 5.pt, bottom: 10.pt),
      padding: EdgeInsets.all(15.pt),
      child: DefaultTextStyle(
        style: DefaultTextStyle.of(context).style.copyWith(height: 2),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Text(
              '管理部门任务总览',
              style: textStyle.titleSmall,
            ),
            Divider(height: 30.pt),
            buildRow(
              name: '总任务',
              count: department.allExcutiveCount ?? 0,
              ratio: null,
            ),
            buildRow(
              name: '按时完成',
              count: department.onTimeFinishedCount ?? 0,
              ratio: department.onTimeFinishedRate ?? 0,
            ),
            buildRow(
              name: '超时完成',
              count: department.timeOutFinishedCount ?? 0,
              ratio: department.timeOutFinishedRate ?? 0,
            ),
            buildRow(
              name: '未开始',
              count: department.noOpenCount ?? 0,
              ratio: department.noOpenRate ?? 0,
            ),
            buildRow(
              name: '进行中',
              count: department.doingCount ?? 0,
              ratio: department.doingRate ?? 0,
            ),
            Divider(height: 30.pt),
            buildRow(
              name: '本年任务',
              count: department.yearExcutiveCount ?? 0,
              ratio: null,
            ),
            buildRow(
              name: '按时完成',
              count: department.yearOnTimeFinishedCount ?? 0,
              ratio: department.yearOnTimeFinishedRate ?? 0,
            ),
            buildRow(
              name: '超时完成',
              count: department.yearTimeOutFinishedCount ?? 0,
              ratio: department.yearTimeOutFinishedRate ?? 0,
            ),
            buildRow(
              name: '未开始',
              count: department.yearNoOpenCount ?? 0,
              ratio: department.yearNoOpenRate ?? 0,
            ),
            buildRow(
              name: '进行中',
              count: department.yearDoingCount ?? 0,
              ratio: department.yearDoingRate ?? 0,
            ),
          ],
        ),
      ),
    );
  }
}
