/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-10-26 10:34:36
 * @LastEditTime : 2023-03-29 10:58:21
 */
part of '../view.dart';

class _RecordListComponent extends StatelessWidget {
  const _RecordListComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateProcessStore>(context, listen: false);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Observer(builder: (context) {
      return WisScrollView(
        onInit: () => store.onGetUserHistoryList(),
        onLoad: () => store.onGetUserHistoryList(),
        key: store.taskHistoryScrollViewKey,
        emptyWidget: SliverToBoxAdapter(
          child: Wisdom.column(
            mainAxisAlignment: MainAxisAlignment.center,
            margin: EdgeInsets.only(bottom: 10.pt),
            padding: EdgeInsets.symmetric(vertical: 50.pt),
            border: Border(top: BorderSide(color: Color(0xffe6e6e6))),
            color: Colors.white,
            children: [
              WEmptyWidget(
                text: Text('空空如也~~'),
              ),
              Text(
                '没有事项哦！',
                style: TextStyle(
                  fontSize: 14.pt,
                  fontWeight: FontWeight.w300,
                  color: colorScheme.minor,
                ),
              ),
            ],
          ),
        ),
        slivers: [
          WisSliverList.childBuilder(
            childCount: store.taskHistoryList?.length ?? 0,
            builder: (_, i) {
              final recordItem = store.taskHistoryList![i];
              return _BuilRecordItem(item: recordItem);
            },
          )
        ],
      ).asExpanded();
    });
  }
}

class _BuilRecordItem extends StatelessWidget {
  const _BuilRecordItem({
    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<PrivateProcessStore>(context, listen: false);
    return WisCard(
      margin: EdgeInsets.only(
        bottom: 10.pt,
        left: 15.pt,
        right: 15.pt,
      ),
      child: Wisdom.column(
        onTap: () => store.onToTaskDetail(item),
        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),
            ],
          ),
          Divider(),
          ...() {
            final map = {
              '流程名称': '${item?.processName}',
              '发起人': '${item?.creator ?? '--'}',
              '发起人部门': '${item?.organName ?? '--'}',
              '提交时间': '${item?.starTime ?? '--'}',
              '当前环节耗时': _dateDurationFormat(item?.useTime ?? 0),
            };
            return map.keys
                .map(
                  (key) => Wisdom.row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 0),
                    children: [
                      Text(
                        '$key：',
                        style: TextStyle(
                          color: colorScheme.minor,
                          fontSize: 14.pt,
                        ),
                      ),
                      Text(
                        '${map[key]}',
                        style: TextStyle(
                          color: colorScheme.mainTitle,
                          fontSize: 14.pt,
                        ),
                      ).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分钟';
  }
}
