/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-10-26 17:50:36
 * @LastEditTime : 2021-04-09 10:56:18
 */
import 'package:flutter/material.dart';
import 'package:oa_package/src/view_models/management_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class ItemComponent extends StatelessWidget {
  const ItemComponent({
    Key? key,
    this.item,
  }) : super(key: key);

  ///元素
  final item;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<ManagementStore>(context, listen: false);

    return WisCard(
      margin: EdgeInsets.only(top: 15.pt, left: 15.pt, right: 15.pt,),
        child: Wisdom.column(
      onTap: () => store.onToTaskDetail(item),
      // margin: EdgeInsets.only(top: 10.pt),
      color: Colors.white,
      children: [
        Wisdom.row(
          padding: EdgeInsets.symmetric(horizontal: 15.pt),
          minHeight: 44.pt,
          children: [
            // WTag(
            //   margin: EdgeInsets.only(right: 10.pt),
            //   child: Text('${item?.status}'),
            // ),
            Text('单号：'),
            Text(
              '${item?.orderId ?? '--'}',
              maxLines: 1,
              style: TextStyle(
                color: style.colorScheme.blue,
                fontSize: 14.pt,
              ),
            ).asExpanded(),
            SizedBox(width: 10.pt),
            Wisdom(
              child: Text(
                '${item?.groupName ?? '--'}',
                style: TextStyle(
                  color: style.colorScheme.blue,
                  fontSize: 14.pt,
                ),
              ),
            ),
          ],
        ),
        Divider(),
        ...() {
          final map = {
            '流程名称': '${item?.processName}',
            '发起人': '${item?.creator ?? '--'}',
            '发起人部门': '${item?.organName ?? '--'}',
            '提交时间': '${item?.starTime ?? '--'}',
            '当前环节耗时': '${_dateDurationFormat(item?.useTime ?? 0)}',
          };
          return map.keys
              .map(
                (key) => Wisdom.row(
                  minHeight: 30.pt,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 0),
                  children: [
                    Wisdom.row(
                      width: 100.pt,
                      children: [
                        Text(
                          '$key：',
                          style: TextStyle(
                            color: colorScheme.minor,
                            fontSize: 14.pt,
                            height: 1.1,
                          ),
                        ),
                      ],
                    ),
                    Text(
                      '${map[key]}',
                      style: TextStyle(
                          color: colorScheme.mainTitle,
                          fontSize: 14.pt,
                          height: 1.1),
                    ).asExpanded(),
                  ],
                ),
              )
              .toList();
        }(),
        SizedBox(height: 15.pt),
      ],
    ),);
  }
}

String _dateDurationFormat(int minutes) {
  if (minutes >= 60) {
    num m = minutes % 60; // 分钟余数
    num h = (minutes / 60).truncate();
    if (h >= 24) {
      num dayHour = h % 24; // 天数的小时余数
      num d = (h / 24).truncate(); // 天数的小时余数
      return '$d天${dayHour != '' && dayHour != 0 ? '$dayHour小时' : ''}';
    } else {
      return '$h小时${m != '' && m != 0 ? '$m分钟' : ''}';
    }
  } else {
    return '$minutes分钟';
  }
}
