/*
 * @Author: XianKaiQun
 * @Date: 2020-07-14 08:37:29
 * @LastEditors: wjc
 * @LastEditTime: 2023-04-03 11:58:27
 * @Description: 
 */
part of '../index.dart';

class _TaskComponent extends StatelessWidget {
  const _TaskComponent({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;

    var person = store.person;
    var myTimeOut = person?.myTimeOut ?? 0;
    var myNormal = person?.myNormal ?? 0;
    double taskValue = myTimeOut / (myTimeOut + myNormal);
    if (taskValue.isNaN) taskValue = 0;

    var myAssignTimeOut = person?.myAssignTimeOut ?? 0;
    var myAssignNormal = person?.myAssignNormal ?? 0;
    double assignValue = myAssignTimeOut / (myAssignTimeOut + myAssignNormal);
    if (assignValue.isNaN) assignValue = 0;

    var myccTimeOut = person?.myccTimeOut ?? 0;
    var myccNormal = person?.myccNormal ?? 0;
    double ccValue = myccTimeOut / (myccTimeOut + myccNormal);
    if (ccValue.isNaN) ccValue = 0;

    return WisCard(
      isSliver: true,
      margin: EdgeInsets.only(left: 15.pt, right: 15.pt, top: 5.pt, bottom: 5.pt),
      padding: EdgeInsets.all(15.pt),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Row(
            children: <Widget>[
              Text(
                '进行中的任务',
                style: TextStyle(color: Colors.black, fontSize: 16.pt),
              ).asExpanded(),
              Wisdom(
                color: colorScheme.warning,
                width: 20.pt,
                height: 6.pt,
                margin: EdgeInsets.only(right: 10.pt),
                borderRadius: BorderRadius.circular(3.pt),
              ),
              Text(
                '超时',
                style: TextStyle(fontSize: 12.pt),
              ),
              Wisdom(
                color: colorScheme.primary,
                width: 20.pt,
                height: 6.pt,
                margin: EdgeInsets.symmetric(horizontal: 10.pt),
                borderRadius: BorderRadius.circular(3.pt),
              ),
              Text(
                '正常',
                style: TextStyle(fontSize: 12.pt),
              ),
              Divider(height: 30.pt),
            ],
          ),
          Divider(height: 30.pt),
          _Liner(
            name: '我的任务',
            normal: myNormal,
            timeOut: myTimeOut,
          ),
          SizedBox(height: 5.pt),
          _Liner(
            name: '我指派的',
            normal: myAssignNormal,
            timeOut: myAssignTimeOut,
          ),
          SizedBox(height: 5.pt),
          _Liner(
            name: '抄送我的',
            normal: myccNormal,
            timeOut: myccTimeOut,
          ),
          Divider(height: 30.pt),
          Text(
            '已完成的任务',
            style: TextStyle(color: Colors.black, fontSize: 16.pt),
          ),
          Divider(height: 30.pt),
          Row(
            children: <Widget>[
              Text('本月任务').asExpanded(),
              Text.rich(
                TextSpan(
                  children: [
                    TextSpan(text: '共${person?.monthFinished ?? 0}项（'),
                    TextSpan(
                      text: '超时：${person?.monthFinishedTimeOut ?? 0}',
                      style: TextStyle(
                        color: (person?.monthFinishedTimeOut ?? 0) != 0
                            ? colorScheme.danger
                            : null,
                      ),
                    ),
                    TextSpan(text: ')'),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: 5.pt),
          Row(
            children: <Widget>[
              Text('本年任务').asExpanded(),
              Text.rich(
                TextSpan(
                  children: [
                    TextSpan(text: '共${person?.yearFinished ?? 0}项（'),
                    TextSpan(
                      text: '超时：${person?.yearFinishedTimeOut ?? 0}',
                      style: TextStyle(
                        color: (person?.yearFinishedTimeOut ?? 0) != 0
                            ? colorScheme.danger
                            : null,
                      ),
                    ),
                    TextSpan(text: ')'),
                  ],
                ),
              ),
            ],
          ),
          Divider(height: 30.pt),
          Text(
            '注：统计数据为本年创建任务',
            textAlign: TextAlign.right,
            style: TextStyle(
              color: colorScheme.grey,
              fontSize: 10.pt,
            ),
          ),
        ],
      ),
    );
  }
}

///
class _Liner extends StatelessWidget {
  const _Liner({
    Key? key,
    required this.name,
    required this.normal,
    required this.timeOut,
  }) : super(key: key);

  final String name;
  final num normal;
  final num timeOut;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    double value = timeOut / (normal + timeOut);
    if (value.isNaN) value = 0;
    final hasTask = normal == 0 && timeOut == 0;
    return Row(
      children: <Widget>[
        Text('$name'),
        SizedBox(width: 20.pt),
        ClipRRect(
          borderRadius: BorderRadius.circular(6.pt),
          child: Stack(
            children: <Widget>[
              SizedBox(
                height: 12.pt,
                child: LinearProgressIndicator(
                  backgroundColor:
                      hasTask ? Color(0xffe4e4e4) : colorScheme.primary,
                  value: value,
                  valueColor: AlwaysStoppedAnimation(colorScheme.warning),
                ),
              ),
              Positioned(
                left: 10.pt,
                child: Text(
                  '$timeOut',
                  style: TextStyle(color: Colors.white, fontSize: 10.pt),
                ),
              ),
              Positioned(
                right: 10.pt,
                child: Text(
                  '$normal',
                  style: TextStyle(color: Colors.white, fontSize: 10.pt),
                ),
              ),
            ],
          ),
        ).asExpanded(),
      ],
    );
  }
}
