/*
 * @Author       : WuWei
 * @LastEditors: wuwei
 * @Date         : 2021-07-19 14:35:33
 * @LastEditTime: 2023-12-14 16:33:50
 * @Description  : 任务详情--巡逻路线
 */

part of '../index.dart';

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context, listen: true);
    return SliverToBoxAdapter(
      child: Wisdom.column(
        color: Colors.white,
        margin: EdgeInsets.only(bottom: 10.pt, right: 15.pt, left: 15.pt),
        padding: EdgeInsets.symmetric(horizontal: 15.pt),
        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(
            padding: EdgeInsets.symmetric(vertical: 15.pt),
            children: <Widget>[
              /// 2023.12.14 巡检路线更改为路线详情。
              WisText(
                '路线详情',
                size: 15.pt,
                bold: true,
              ),
            ],
          ),
          Observer(builder: (_) {
            return Column(
              children: [
                if (store.routeList.length > 0) ...[
                  for (int i = 0; i < store.routeList.length; i++)
                    _RouteItem(
                        item: store.routeList[i],
                        isLast:
                            (i + 1) == store.routeList.length ? true : false),
                ]
              ],
            );
          }),
        ],
      ),
    );
  }
}

class _RouteItem extends StatelessWidget {
  const _RouteItem({
    Key? key,
    this.item,
    this.isLast = false,
  }) : super(key: key);
  final item;
  final bool isLast;
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Wisdom(
      color: colorScheme.background,
      margin: EdgeInsets.only(bottom: 15.pt),
      borderRadius: BorderRadius.circular(8.pt),
      child: WisExpansionTile(
        initiallyExpanded: false,
        showIcon: false,
        title: Wisdom.row(
          height: 44.pt,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            WisText(
              '${item.communityName}',
              size: 15.pt,
              weight: FontWeight.w600,
              color: colorScheme.mainTitle,
            ),
            Wisdom.row(
              children: [
                WisText(
                  '展开',
                  size: 14.pt,
                  weight: FontWeight.w400,
                  color: colorScheme.primary,
                ),
                SizedBox(width: 5.pt),
                Image(
                  image: AssetList.$zhk_png_image,
                  height: 10.pt,
                  color: colorScheme.primary,
                ),
              ],
            ),
          ],
        ),
        expandTitle: Wisdom.row(
          height: 44.pt,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Wisdom.row(
              children: [
                WisText(
                  '${item.communityName}',
                  size: 15.pt,
                  weight: FontWeight.w600,
                  color: colorScheme.mainTitle,
                ),
              ],
            ),
            Wisdom.row(
              children: [
                WisText(
                  '收起',
                  size: 14.pt,
                  weight: FontWeight.w400,
                  color: colorScheme.primary,
                ),
                SizedBox(width: 5.pt),
                Image(
                  image: AssetList.$shq_png_image,
                  height: 10.pt,
                  color: colorScheme.primary,
                ),
              ],
            ),
          ],
        ),
        children: [
          Column(
            children: [
              if (item?.list?.length != null && item.list.length > 0) ...[
                for (int i = 0; i < item.list.length; i++)
                  _RouteItemList(
                    item: item.list[i],
                    isLast: ((i + 1) == item.list.length) ? true : false,
                    isFirst: i == 0 ? true : false,
                  ),
              ],
            ],
          ),
        ],
      ),
    );
  }
}

class _RouteItemList extends StatelessWidget {
  const _RouteItemList({
    Key? key,
    this.item,
    this.isLast = false,
    this.isFirst = false,
  }) : super(key: key);
  final item;
  final bool isLast;
  final bool isFirst;
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context, listen: true);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final bool isPrimary = (store.data?.status != 'no_open') &&
        (store.data?.status != 'cancel') &&
        (store.data?.status != 'overdue') &&
        (item?.status == 'unFinished') &&
        (item?.executable == true) &&
        (store.data?.isoperator == true);
    return Wisdom.stack(
      onTap: () => store.toTaskPointForm(item),
      clipBehavior: Clip.none,
      padding: EdgeInsets.fromLTRB(15.pt, isFirst ? 5 : 15.pt, 15.pt, 20.pt),
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Wisdom.column(
              margin: EdgeInsets.only(left: 15.pt),
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Wisdom.row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Wisdom.row(
                      children: [
                        // 2023.12.14 去掉设备间、设备点标签。
                        // WTag(
                        //   plain: true,
                        //   backgroundColor: Colors.transparent,
                        //   borderRadius: BorderRadius.circular(4.pt),
                        //   color: colorScheme.primary,
                        //   child: WisText(
                        //     '${item?.relationType == 'deviceRoom' || item?.relationType == 'devicePoint' ? '类型' : '区域'}',
                        //     color: colorScheme.primary,
                        //     size: 11.pt,
                        //     weight: FontWeight.bold,
                        //   ),
                        // ),
                        WisText(
                          '${item?.relationType == 'deviceRoom' || item?.relationType == 'devicePoint' ? '${store.repairPointType[item?.relationType] ?? ''}' : '${item?.nodeData['communityAreaName'] ?? ''}'}',
                          color: isPrimary
                              ? colorScheme.mainTitle
                              : colorScheme.placeholder,
                          size: 14.pt,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),
                  ],
                ),
                SizedBox(height: 3.pt),
                WisText(
                  '${item?.relationType == 'deviceRoom' || item?.relationType == 'devicePoint' ? '名称' : '点位'}：${item?.relationName ?? '--'}',
                  color: item.status == 'finished'
                      ? colorScheme.mainTitle
                      : colorScheme.placeholder,
                  size: 14.pt,
                  height: 1.5,
                ),
                if (item.status == 'finished') ...[
                  SizedBox(height: 3.pt),
                  WisText(
                    '完成时间：${item?.nodeData['updateAt'] ?? '--'}',
                    color: colorScheme.placeholder,
                    size: 14.pt,
                    height: 1.5,
                  ),
                ]
              ],
            ).asFlexible(flex: 1),
            if (item.status == 'finished') ...[
              Wisdom.row(
                margin: EdgeInsets.only(left: 5.pt),
                children: [
                  if (item?.nodeData['resultAbnormal'] != null &&
                      item?.nodeData['resultAbnormal'] == false) ...[
                    Image.asset(
                      'lib/assets/bj26@3x.png',
                      package: 'task_package',
                      width: 14.pt,
                    ),
                  ] else ...[
                    Image.asset(
                      'lib/assets/bjh@3x.png',
                      package: 'task_package',
                      width: 14.pt,
                    ),
                  ],
                  SizedBox(width: 3.pt),
                  WisText(
                    '已完成',
                    color: colorScheme.minor,
                    size: 14.pt,
                  )
                ],
              ),
            ] else ...[
              Wisdom(
                padding: EdgeInsets.symmetric(
                  vertical: 5.pt,
                  horizontal: 15.pt,
                ),
                border: Border.all(
                  color:
                      isPrimary ? colorScheme.primary : colorScheme.placeholder,
                ),
                borderRadius: BorderRadius.circular(20.pt),
                child: WisText(
                  /// 操作对应名称
                  /// 2023.12.14 巡检按钮改为开始按钮
                  '开始',
                  color:
                      isPrimary ? colorScheme.primary : colorScheme.placeholder,
                  size: 14.pt,
                  weight: FontWeight.bold,
                ),
              ),
            ],
          ],
        ),

        /// 最后一个去掉分割线
        if (!isLast) ...[
          Positioned(
            left: 0.pt,
            top: 5.pt,
            bottom: -40.pt,
            child: Container(
              width: 0.9.pt,
              color: colorScheme.divider,
            ),
          ),
        ],

        /// 是否按序
        // if (store.data.seqencing == true) ...[
        /// 节点完成状态
        if (item.status == 'finished') ...[
          /// 节点完成项是否异常
          Positioned(
            left: -5.pt,
            top: 5.pt,
            child: Wisdom(
              width: 10.pt,
              height: 10.pt,
              color: colorScheme.primary,
              borderRadius: BorderRadius.circular(10.pt),

              /// 根据是否按序显示序号
              // child: WisText('${item.nodeSequenceNumber ?? ''}'),
            ),
          ),
        ] else ...[
          if (isPrimary) ...[
            Positioned(
              left: -8.pt,
              top: 2.pt,
              child: Stack(
                children: [
                  Wisdom(
                    color: Color(0x330F67F8),
                    width: 16.pt,
                    height: 16.pt,
                    borderRadius: BorderRadius.circular(10.pt),
                  ),
                  Positioned(
                    left: 3.pt,
                    top: 3.pt,
                    child: Wisdom(
                      color: Colors.white,
                      width: 10.pt,
                      height: 10.pt,
                      borderRadius: BorderRadius.circular(10.pt),
                      border: Border.all(
                        color: colorScheme.primary,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ] else ...[
            Positioned(
              left: -5.pt,
              top: 5.pt,
              child: Wisdom(
                width: 10.pt,
                height: 10.pt,
                color: colorScheme.placeholder,
                borderRadius: BorderRadius.circular(10.pt),

                /// 根据是否按序显示序号
                // child: WisText('${item.nodeSequenceNumber ?? ''}'),
              ),
            ),
          ]
        ],
      ],
    );
  }
}
