/*
 * @Author: XianKaiQun
 * @Date: 2020-04-26 11:31:34
 * @LastEditors: wuwei
 * @LastEditTime: 2023-12-14 17:30:04
 * @Descripttion: 任务详情-顶部任务简介
 */
part of '../index.dart';

///详情模块
class _DescComponent extends StatelessWidget {
  const _DescComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context, listen: true);
    final data = store.data;
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return SliverToBoxAdapter(
      child: Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Container(
              height: 210.pt,
              child: Wisdom.stack(
                clipBehavior: Clip.none,
                children: [
                  /// 新标题
                  Wisdom.column(
                      color: colorScheme.primary,
                      height: 170.pt,
                      children: [
                        Wisdom.row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          padding: EdgeInsets.only(
                              top: 29.pt, left: 15.pt, right: 15.pt),
                          children: [
                            Wisdom.column(children: [
                              Wisdom.row(
                                margin: EdgeInsets.only(bottom: 13.5.pt),
                                children: [
                                  Image(
                                    image: data?.status == 'no_open'
                                        ? AssetList.$fabu_png_image
                                        : AssetList.$fabu_end_png_image,
                                    height: 44.pt,
                                  ),
                                ],
                              ),
                            ]),
                            Wisdom.column(
                              height: 35.pt,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                WisText(
                                  '----------',
                                  color: Colors.white,
                                  size: 15.pt,
                                ),
                              ],
                            ),
                            // 进行中的标题显示
                            Wisdom.column(children: [
                              Wisdom.row(
                                // height: 44.pt,
                                margin: EdgeInsets.only(bottom: 13.5.pt),
                                children: [
                                  Image(
                                    image: data?.status == 'doing'
                                        ? AssetList.$proceed_cur_png_image
                                        : data?.status == 'finished'
                                            ? AssetList.$proceed_end_png_image
                                            : AssetList.$proceed_png_image,
                                    height: 44.pt,
                                  ),
                                ],
                              ),
                            ]),
                            Wisdom.column(
                              height: 35.pt,
                              children: [
                                WisText(
                                  '----------',
                                  color: Colors.white,
                                  size: 15.pt,
                                ),
                              ],
                            ),
                            Wisdom.row(
                              // height: 44.pt,
                              margin: EdgeInsets.only(bottom: 13.5.pt),
                              children: [
                                Image(
                                  image: data?.status == 'finished'
                                      ? AssetList.$finish_end_png_image
                                      : AssetList.$finish_png_image,
                                  height: 44.pt,
                                ),
                              ],
                            ),
                          ],
                        ),
                        Wisdom.row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          padding: EdgeInsets.only(
                            left: 15.pt,
                            right: 15.pt,
                            bottom: 15.pt,
                          ),
                          children: [
                            Wisdom.column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Wisdom.row(
                                    children: [
                                      WisText(
                                        '发布时间',
                                        color: data?.status == 'no_open'
                                            ? Colors.white
                                            : Color(0x80FFFFFF),
                                        size: 13.pt,
                                      ),
                                    ],
                                  ),
                                  WisText(
                                    data?.createdAt ?? '--',
                                    color: data?.status == 'no_open'
                                        ? Colors.white
                                        : Color(0x80FFFFFF),
                                    size: 13.pt,
                                  ),
                                ]).asFlexible(flex: 1),
                            // 进行中的标题显示
                            Wisdom.column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  WisText(
                                    '进行中',
                                    color: data?.status == 'doing'
                                        ? Colors.white
                                        : Color(0x80FFFFFF),
                                    size: 13.pt,
                                  ),
                                ]).asFlexible(flex: 1),
                            Wisdom.column(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Wisdom.row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      WisText(
                                        '完成时间',
                                        color: data?.status == 'finished'
                                            ? Colors.white
                                            : Color(0x80FFFFFF),
                                        size: 13.pt,
                                      ),
                                    ],
                                  ),
                                  WisText(
                                    data?.finishedTime ?? '',
                                    color: data?.status == 'finished'
                                        ? Colors.white
                                        : Color(0x80FFFFFF),
                                    size: 13.pt,
                                  ),
                                ]).asFlexible(flex: 1),
                          ],
                        ),
                      ]),

                  Positioned(
                    top: 0,
                    left: 0,
                    right: 0,
                    child: Wisdom.column(
                      height: 30.pt,
                      padding: EdgeInsets.symmetric(horizontal: 15.pt),
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        /// 如果任务即将过期或开始执行的话，有个顶部提示
                        if ((store.data?.isTimeOut == true &&
                                store.data?.overdueTime != null &&
                                store.data?.closeTime != null) ||
                            (store.data?.isTimeOut == false &&
                                store.data?.overdueTime != null &&
                                store.data?.closeTime == null)) ...[
                          Wisdom.row(
                            borderRadius: BorderRadius.circular(5.pt),
                            padding: EdgeInsets.only(left: 15.pt),
                            height: 30.pt,
                            color: Color(0xB32D3051),
                            children: [
                              Wisdom.row(
                                margin: EdgeInsets.only(right: 8.pt),
                                children: [
                                  Image(
                                    image: AssetList.$tips_png_image,
                                    height: 16.pt,
                                  ),
                                ],
                              ),
                              WisText(
                                '该任务于 ${store.data?.overdueTime} 后到期',
                                size: 13.pt,
                                weight: FontWeight.w300,
                                color: Colors.white,
                              ),
                            ],
                          ),
                        ],
                        if (store.data?.canStartTime != null &&
                            store.data?.status == 'no_open') ...[
                          Wisdom.row(
                            borderRadius: BorderRadius.circular(2.5.pt),
                            padding: EdgeInsets.only(left: 15.pt),
                            height: 30.pt,
                            color: Color(0xB32D3051),
                            children: [
                              Wisdom.row(
                                margin: EdgeInsets.only(right: 8.pt),
                                children: [
                                  Image(
                                    image: AssetList.$tips_info_png_image,
                                    height: 16.pt,
                                  ),
                                ],
                              ),
                              WisText(
                                '该任务于 ${store.data?.canStartTime} 后可开始执行',
                                size: 13.pt,
                                weight: FontWeight.w300,
                                color: Colors.white,
                              ),
                            ],
                          ),
                        ],
                      ],
                    ),
                  ),

                  /// 任务动态
                  Positioned(
                    bottom: 0,
                    left: 0,
                    right: 0,
                    child: _DynamicLast(),
                  ),
                ],
              )),

          /// 任务内容
          _BuildContent(),

          /// 任务附件
          if (store.data?.accessoryVos?.length != null &&
              (store.data?.accessoryVos?.length ?? 0) > 0) ...[
            _BuildFile(),
          ]
        ],
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Wisdom.column(
      color: Colors.white,
      height: 75.pt,
      margin: EdgeInsets.symmetric(horizontal: 15.pt),
      padding: EdgeInsets.all(15.pt),
      crossAxisAlignment: CrossAxisAlignment.stretch,
      borderRadius: BorderRadius.circular(10.pt),
      boxShadow: [
        BoxShadow(
          offset: Offset(0, 2.pt),
          color: Color(0x08000000),
          blurRadius: 10.pt,
          spreadRadius: 2.5.pt,
        ),
      ],
      children: [
        Wisdom.row(
          onTap: () => store.toTaskDynamic(),
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Wisdom.row(children: [
              Image(
                image: AssetList.$dongtai_png_image,
                height: 32.pt,
                width: 25.pt,
              ),
              Wisdom.column(
                margin: EdgeInsets.symmetric(horizontal: 10.pt),
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Wisdom(
                    child: WisText(
                      store.firstStatus?.title ?? '--',
                      color: colorScheme.mainTitle,
                      size: 16.pt,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      weight: FontWeight.w600,
                    ),
                  ),
                  Wisdom(
                    margin: EdgeInsets.only(top: 5.pt),
                    child: WisText(
                      store.firstStatus?.createdAt ?? '--',
                      color: Color(0xFF9FA5B7),
                      size: 14.pt,
                    ),
                  ),
                ],
              ).asExpanded(),
            ]).asExpanded(),
            Image(
              image: AssetList.$tzhxq_png_image,
              height: 14.pt,
            ),
          ],
        ),
      ],
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context);
    final data = store.data;
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Wisdom.column(
      color: Colors.white,
      margin: EdgeInsets.symmetric(vertical: 10.pt, horizontal: 15.pt),
      padding: EdgeInsets.all(15.pt),
      crossAxisAlignment: CrossAxisAlignment.stretch,
      borderRadius: BorderRadius.circular(10.pt),
      boxShadow: [
        BoxShadow(
          offset: Offset(0, 2.pt),
          color: Color(0x08000000),
          blurRadius: 10.pt,
          spreadRadius: 2.5.pt,
        ),
      ],
      children: [
        Wisdom.row(
          margin: EdgeInsets.only(bottom: 10.pt),
          children: [
            Wisdom.rich(
              children: [
                TextSpan(
                  text: data?.title ?? '',
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    color: colorScheme.mainTitle,
                    fontSize: 18.pt,
                  ),
                ),
              ],
            ).asExpanded(),
          ],
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          margin: EdgeInsets.only(bottom: 10.pt),
          // padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 0),
          children: <Widget>[
            RichText(
              overflow: TextOverflow.ellipsis,
              maxLines: 2,
              softWrap: true,
              text: TextSpan(children: [
                if (data?.closeTime != null)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: data?.status == 'finished'
                            ? colorScheme.success
                            : colorScheme.red,
                        child: WisText(
                          '限时 ${data?.closeTime ?? '--'}',
                          color: data?.status == 'finished'
                              ? colorScheme.minor
                              : colorScheme.red,
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),
                // 是否是环节任务
                if (data?.isMultiStep == true)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.green,
                        child: WisText(
                          '环节任务',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),
                // 是否日常任务
                if (data?.preset == true)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.blue,
                        child: WisText(
                          '日常任务',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  )
                else
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.orange,
                        child: WisText(
                          '专项任务',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),
                if (data?.preset == true) ...[
                  if (data?.seqencing == true) ...[
                    WidgetSpan(
                      child: Wisdom(
                        margin: EdgeInsets.only(bottom: 5.pt),
                        child: WTag(
                          color: colorScheme.cyan,
                          child: WisText(
                            /// 2023.12.14 不分类型
                            '按序执行',
                            size: 13.pt,
                          ),
                        ),
                      ),
                    ),
                  ] else ...[
                    WidgetSpan(
                      child: Wisdom(
                        margin: EdgeInsets.only(bottom: 5.pt),
                        child: WTag(
                          color: colorScheme.blue,
                          child: WisText(
                            '不限次序',
                            size: 13.pt,
                          ),
                        ),
                      ),
                    ),
                  ],
                ],

                ///状态
                if (data?.status == 'doing')
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.blue,
                        child: WisText(
                          '进行中',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  )
                else if (data?.status == 'finished')
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.green,
                        child: WisText(
                          '已完成',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  )
                else if (data?.status == 'no_open')
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.orange,
                        child: WisText(
                          '未开始',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  )
                else if (data?.status == 'cancel')
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.minor,
                        child: WisText(
                          '终止',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  )
                else if (data?.status == 'overdue')
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.minor,
                        child: WisText(
                          '已逾期',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  )
                else if (data?.status == 'waitReceive')
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.red,
                        child: WisText(
                          '待领取',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),

                /// 是否超时
                if (data?.isTimeOut == true)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.red,
                        child: WisText(
                          '超时',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),

                ///类型
                if (data?.isChild == true)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.warning,
                        child: WisText(
                          '子任务',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),
                if (data?.isPublic == false)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.cyan,
                        child: WisText(
                          '隐藏',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),
                if (data?.examine == true)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: colorScheme.success,
                        child: WisText(
                          '已审查',
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),

                if (data?.iscreatedBy == true && data?.readState != null)
                  WidgetSpan(
                    child: Wisdom(
                      margin: EdgeInsets.only(bottom: 5.pt),
                      child: WTag(
                        color: data?.readState == true
                            ? colorScheme.success
                            : colorScheme.red,
                        child: WisText(
                          '${data?.readState == true ? '已读' : '未读'}',
                          color: data?.readState == true
                              ? colorScheme.success
                              : colorScheme.red,
                          size: 13.pt,
                        ),
                      ),
                    ),
                  ),
              ]),
            ).asFlexible(flex: 1),
          ],
        ),
        Wisdom.row(
          margin: EdgeInsets.only(bottom: 10.pt),
          children: <Widget>[
            WisText(
              '添加人',
              size: 14.pt,
              color: Color(0xFF616178),
            ).asExpanded(),
            WisText(
              data?.createdBy ?? '--',
              color: Color(0xFF9FA5B7),
              size: 14.pt,
            ),
          ],
        ),
        Wisdom.row(
          margin: EdgeInsets.only(bottom: 10.pt),
          children: <Widget>[
            WisText(
              '任务类型',
              size: 14.pt,
              color: Color(0xFF616178),
            ).asExpanded(),
            WisText(
              '${data?.inventoryName ?? '--'}',
              color: Color(0xFF9FA5B7),
              size: 14.pt,
            ),
          ],
        ),
        Wisdom(
          alignment: Alignment.centerLeft,
          color: colorScheme.background,
          borderRadius: BorderRadius.circular(8.pt),
          padding: EdgeInsets.all(10.pt),
          child: WisText(
            '${data?.content ?? ''}',
            maxLines: 100,
            height: 1.8,
            size: 14.pt,
            color: colorScheme.minor,
          ),
        ),
      ],
    );
  }
}

class _BuildFile extends StatelessWidget {
  const _BuildFile({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;
    return Wisdom.column(
      color: Colors.white,
      margin: EdgeInsets.symmetric(vertical: 10.pt, horizontal: 15.pt),
      padding: EdgeInsets.all(15.pt),
      crossAxisAlignment: CrossAxisAlignment.stretch,
      borderRadius: BorderRadius.circular(10.pt),
      boxShadow: [
        BoxShadow(
          offset: Offset(0, 2.pt),
          color: Color(0x08000000),
          blurRadius: 10.pt,
          spreadRadius: 2.5.pt,
        ),
      ],
      children: <Widget>[
        Wisdom.row(
          margin: EdgeInsets.only(bottom: 10.pt),
          children: <Widget>[
            WisText(
              '任务附件',
              size: 15.pt,
              color: colorScheme.mainTitle,
              weight: FontWeight.w600,
            ),
          ],
        ),
        Wisdom.column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            /// 附件展示为链接，图片展示为缩略图
            if (store.data?.accessoryVos!.length != null &&
                store.data!.accessoryVos!.length > 0) ...[
              for (int i = 0; i < store.data!.accessoryVos!.length; i++)
                if (store.data!.accessoryVos![i].type == 'file') ...[
                  _FileItem(itemData: store.data!.accessoryVos![i]),
                ] else if (store.data!.accessoryVos![i].type == 'img') ...[
                  Wisdom.wrap(
                      runSpacing: 10.pt,
                      spacing: 5.pt,
                      margin: EdgeInsets.only(top: 10.pt),
                      children: <Widget>[
                        Wisdom(
                          onTap: () => store.onShowImage(
                              store.data!.accessoryVos![i].accessoryId),
                          child: WImage.network(
                            AppParseUtil.image(
                              store.data!.accessoryVos![i].accessoryId!,
                            ),
                            fit: BoxFit.cover,
                            width: 60.pt,
                            height: 60.pt,
                            borderRadius: BorderRadius.circular(4.pt),
                          ),
                        ),
                      ]),
                ]
            ]
          ],
        ),
      ],
    );
  }
}
