/*
 * @Author: XianKaiQun
 * @Date: 2020-09-14 11:46:16
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-31 10:09:30
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:dynamic_package/src/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

///服务动态列表卡片
class DynamicCardComponent extends StatelessWidget {
  DynamicCardComponent({
    Key? key,
    this.id,
    this.publisher,
    this.creator,
    this.content,
    this.photos,
    this.dateTime,
    this.likeNum,
    this.onDelete,
    this.onEdit,
    this.onUndo,
    this.type,
    this.dtype,
    this.imgUrl,
    this.toDetail,
  }) : super(key: key);
  final String? type;
  final String? dtype;
  final String? id;
  final String? imgUrl;

  ///发布人名称
  final String? publisher;

  ///录入人名称
  final String? creator;

  ///内容
  final String? content;

  ///相册图片的Id
  final List<String>? photos;

  ///创建时间
  final String? dateTime;

  ///点赞数
  final String? likeNum;

  ///删除点击事件
  final void Function()? onDelete;

  /// 编辑
  final void Function()? onEdit;

  /// 撤回
  final void Function()? onUndo;

  /// 跳转详情
  final void Function()? toDetail;

  /// 确认删除
  _onDelete() async {
    if (onDelete == null) return;
    bool? boolean = await WModalUtil.showSync('删除后无法恢复，确认删除吗？', title: '删除确认');
    if (boolean == true) {
      onDelete!();
    }
  }

  _onUndo() async {
    if (onUndo == null) return;
    bool? boolean = await WModalUtil.showSync('确认撤回此条动态吗？', title: '撤回确认');
    if (boolean == true) {
      onUndo!();
    }
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    ///图片
    final _photosProvider = photos?.map((e) {
          return CachedNetworkImageProvider(AppParseUtil.image(e));
        }).toList() ??
        [];
    var _imgUrl = imgUrl != null
        ? CachedNetworkImageProvider(AppParseUtil.image(imgUrl!))
        : null;

    return WisCard(
      color: Colors.white,
      margin: EdgeInsets.fromLTRB(15.pt, 10.pt, 15.pt, 0),
      child: Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        color: Colors.white,
        children: [
          Wisdom.row(
            onTap: () => toDetail != null ? toDetail!() : null,
            padding: EdgeInsets.all(15.pt),
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Wisdom.row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  if (_imgUrl != null)
                    Wisdom(
                      margin: EdgeInsets.only(right: 5.pt),
                      child: WImage(
                        image: _imgUrl,
                        height: 25.pt,
                        width: 25.pt,
                        borderRadius: BorderRadius.circular(4.pt),
                        fit: BoxFit.cover,
                      ),
                    ),
                  if (publisher != null) ...[
                    WisText('$publisher', color: colorScheme.primary),
                    SizedBox(width: 5.pt),
                  ],
                  WTag(
                    color: type == 'serviceBulletin'
                        ? colorScheme.primary
                        : colorScheme.green,
                    child: WisText(
                      type == 'serviceBulletin' ? '服务简报' : '服务详报',
                      size: 13.pt,
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  WisText(
                    '$dateTime',
                    color: colorScheme.grey,
                  ),
                ],
              )
            ],
          ),
          Divider(),
          Wisdom.column(
            onTap: () => toDetail != null ? toDetail!() : null,
            padding: EdgeInsets.symmetric(
              horizontal: 15.pt,
              vertical: 15.pt,
            ),
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              WisText(
                '$content',
                height: 1.5,
              ),
              if (_photosProvider.length > 0) ...[
                SizedBox(height: 15.pt),
                Wrap(
                  crossAxisAlignment: WrapCrossAlignment.start,
                  runSpacing: 10.pt,
                  spacing: 10.pt,
                  children: [
                    for (var i = 0; i < _photosProvider.length; i++)
                      Wisdom(
                        onTap: () => WisPhotoViewUtil.show(_photosProvider, i),
                        child: WImage(
                          image: _photosProvider[i],
                          height:
                              (MediaQuery.of(context).size.width - 80.pt) / 3,
                          width:
                              (MediaQuery.of(context).size.width - 80.pt) / 3,
                          borderRadius: BorderRadius.circular(4.pt),
                          fit: BoxFit.cover,
                        ),
                      ),
                  ],
                ),
              ],
              SizedBox(height: 5.pt),
            ],
          ),
          Divider(),
          DefaultTextStyle(
            style: DefaultTextStyle.of(context).style.copyWith(fontSize: 13.pt),
            child: Wisdom.row(
              padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 15.pt, 10.pt),
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (dtype != null) WisText('类型：${dtype}'),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    if (onUndo != null)
                      Wisdom.row(
                        onTap: () => _onUndo(),
                        margin: EdgeInsets.only(
                          left: 15.pt,
                        ),
                        children: [
                          Icon(
                            Icons.undo_outlined,
                            size: 15.pt,
                          ),
                          SizedBox(width: 5.pt),
                          WisText(
                            '撤回',
                            size: 13.pt,
                          ),
                        ],
                      ),
                    if (onEdit != null)
                      Wisdom.row(
                        onTap: () => onEdit!(),
                        margin: EdgeInsets.only(
                          left: 15.pt,
                        ),
                        children: [
                          ImageIcon(
                            AssetList.$edit_png_image,
                            size: 13.pt,
                          ),
                          WisText(
                            '编辑',
                            size: 13.pt,
                          ),
                        ],
                      ),
                    if (onDelete != null)
                      Wisdom.row(
                        onTap: () => _onDelete(),
                        margin: EdgeInsets.only(
                          left: 15.pt,
                        ),
                        children: [
                          ImageIcon(
                            AssetList.$delete_png_image,
                            size: 13.pt,
                          ),
                          WisText(
                            ' 删除',
                            size: 13.pt,
                          ),
                        ],
                      ),
                  ],
                ).asFlexible(flex: 1),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
