/*
 * @Author: WuWei
 * @Date: 2020-06-19 09:01:07
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-08-11 11:34:57
 * @Description:任务详情-子任务
 */
part of '../index.dart';

class _ChildTaskComponent extends StatelessWidget {
  const _ChildTaskComponent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context, listen: true);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final childTaskCount = store.data?.childTaskCount ?? 0;
    final status = store.data?.status;
    final total = childTaskCount != 0 ? '$childTaskCount' : '0';

    if (status == 'finished' && childTaskCount == 0) {
      return SliverToBoxAdapter();
    }
    return SliverToBoxAdapter(
      child: Wisdom.column(
        color: Colors.white,
        margin: EdgeInsets.symmetric(vertical: 10.pt, horizontal: 15.pt),
        padding: EdgeInsets.all(15.pt),
        borderRadius: BorderRadius.circular(12.pt),
        boxShadow: [
          BoxShadow(
            offset: Offset(0, 2),
            color: Color(0x08000000),
            blurRadius: 10.pt,
            spreadRadius: 2.5.pt,
          ),
        ],
        children: [
          Wisdom.row(
            padding: EdgeInsets.symmetric(vertical: 10.pt),
            children: <Widget>[
              WisText(
                '子任务',
                size: 15.pt,
                weight: FontWeight.w600,
              ).asExpanded(),
              Wisdom.row(
                children: <Widget>[
                  WisText(
                    '共',
                    color: colorScheme.mainTitle,
                    size: 14.pt,
                  ),
                  WisText(
                    '$total',
                    color: colorScheme.primary,
                    size: 14.pt,
                  ),
                  WisText(
                    '项',
                    color: colorScheme.mainTitle,
                    size: 14.pt,
                  ),
                ],
              ),
            ],
          ),
          Observer(builder: (_) {
            return Column(
              children: [
                for (int i = 0; i < store.taskChildList.length; i++)
                  _TaskItem(
                    item: store.taskChildList[i],
                  )
              ],
            );
          })
        ],
      ),
    );
  }
}

class _TaskItem extends StatelessWidget {
  const _TaskItem({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<PrivateTaskDetailStore>(context);

    ///tag状态颜色
    final statusColorMap = {
      // 1: '未开始',
      1: colorScheme.orange,
      // 2: '进行中',
      2: colorScheme.blue,
      // 3: '已完成',
      3: colorScheme.success,
      // 4: '超时',
      4: colorScheme.danger,
      // 5: '取消'
      5: colorScheme.minor,
      // 6: '超时'
      6: colorScheme.danger,
      // 7：'已逾期'
      7: colorScheme.minor,
    };

    return Wisdom.row(
      onTap: () => store.toTaskDetail(item.id, item.isChild, true),
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      color: Colors.white,
      padding: EdgeInsets.symmetric(vertical: 15.pt),
      // border: Border(top: BorderSide(color: colorScheme.divider)),
      children: [
        Wisdom.column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text.rich(
                  TextSpan(children: [
                    if (item?.preset == true)
                      WidgetSpan(
                        child: Wisdom(
                          child: WTag(
                            color: colorScheme.blue,
                            child: WisText(
                              '日常任务',
                              size: 13.pt,
                            ),
                          ),
                        ),
                      )
                    else
                      WidgetSpan(
                        child: Wisdom(
                          child: WTag(
                            color: colorScheme.orange,
                            child: WisText(
                              '专项任务',
                              size: 13.pt,
                            ),
                          ),
                        ),
                      ),
                    TextSpan(
                      text: item?.title ?? '',
                      style: TextStyle(
                        fontSize: 15.pt,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ]),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ).asExpanded(),
              ],
            ),
            Wisdom.row(
              margin: EdgeInsets.only(
                top: 10.pt,
              ),
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Wisdom.row(
                  margin: EdgeInsets.only(bottom: 10.pt),
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    WisText(
                      '任务类型：',
                      size: 14.pt,
                      color: Color(0xFF616178),
                    ),
                    WisText(
                      '${item?.inventoryName ?? '--'}',
                      color: Color(0xFF9FA5B7),
                      size: 14.pt,
                    ),
                  ],
                ).asExpanded(),
                Wisdom.row(
                  children: [
                    if (item.status == 'finished')
                      Wisdom(
                        child: WisText(
                          '已完成',
                          size: 13.pt,
                          color: statusColorMap[3],
                        ),
                      ),
                    if (item.status == 'no_open')
                      Wisdom(
                        child: WisText(
                          '未开始',
                          size: 13.pt,
                          color: statusColorMap[1],
                        ),
                      ),
                    if (item.status == 'doing')
                      Wisdom(
                        child: WisText(
                          '进行中',
                          size: 13.pt,
                          color: statusColorMap[2],
                        ),
                      ),
                    if (item.status == 'cancel')
                      Wisdom(
                        child: WisText(
                          '终止',
                          size: 13.pt,
                          color: statusColorMap[5],
                        ),
                      ),
                    if (item.taskTimeout)
                      Wisdom(
                        child: WisText(
                          '超时',
                          size: 13.pt,
                          color: statusColorMap[4],
                        ),
                      ),
                    Wisdom(
                      padding: EdgeInsets.only(
                        left: 15.pt,
                        right: 5.pt,
                      ),
                      child: Image(
                        image: AssetList.$tzhxq_png_image,
                        height: 10.pt,
                      ),
                    ),
                  ],
                )
              ],
            ),
            Wisdom.row(
              margin: EdgeInsets.only(
                top: 10.pt,
              ),
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Wisdom.row(
                  margin: EdgeInsets.only(bottom: 10.pt),
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    WisText(
                      '发布时间：',
                      size: 14.pt,
                      color: Color(0xFF616178),
                    ),
                    WisText(
                      '${item?.createdAt ?? '--'}',
                      color: Color(0xFF9FA5B7),
                      size: 14.pt,
                    ),
                  ],
                ).asExpanded(),
                Wisdom.row(
                  margin: EdgeInsets.only(bottom: 10.pt),
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    WisText(
                      '执行人：',
                      size: 14.pt,
                      color: Color(0xFF616178),
                    ),
                    WisText(
                      '${item?.operatorName ?? '--'}',
                      color: Color(0xFF9FA5B7),
                      size: 14.pt,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ).asFlexible(flex: 1),
      ],
    );
  }
}
