/*
 * @Author: WuWei
 * @Date: 2020-06-02 16:33:37
 * @LastEditors: WuWei
 * @LastEditTime: 2020-06-12 15:21:08
 * @Description:
 */

part of '../index.dart';

class _WechatInfo extends StatefulWidget {
  _WechatInfo({
    Key? key,
    this.onInit,
  }) : super(key: key);

  ///首次加载
  final Future<void> Function()? onInit;

  @override
  __WechatState createState() => __WechatState();
}

class __WechatState extends State<_WechatInfo> {
  Future<void> _onInit() async {
    if (widget.onInit != null) {
      await widget.onInit!();
    }
  }

  @override
  void initState() {
    super.initState();
    if (widget.onInit != null) {
      ///触发首次加载
      Future.microtask(() async {
        await _onInit();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SearchResidentDetailStore>(context);
    if (store.residentWechat == null || store.residentWechat!.length == 0) {
      return InfoEmpty();
    }
    return CustomScrollView(
      physics: ScrollPhysics(),
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: Wisdom(
            padding: EdgeInsets.all(10.pt),
            child: Wisdom.column(
              mainAxisSize: MainAxisSize.min,
              color: Colors.white,
              borderRadius: BorderRadius.circular(14.pt),
              padding: EdgeInsets.all(15.pt),
              boxShadow: [
                BoxShadow(
                  color: Color(0x40000000),
                  blurRadius: 10.pt,
                  spreadRadius: -5.pt,
                )
              ],
              children: store.residentWechat!.length > 0
                  ? (store.residentWechat!
                          .map((item) => new _InfoItem(
                              item: item,
                              isLast: store.residentWechat!.length ==
                                  (store.residentWechat!.indexOf(item) + 1),
                              index: store.residentWechat!.indexOf(item)))
                          .toList())
                  : [],
            ),
          ),
        )
      ],
    );
  }
}

/// 绑定的微信
class _InfoItem extends StatelessWidget {
  const _InfoItem({
    Key? key,
    this.item,
    this.index,
    this.isLast = false,
  }) : super(key: key);

  /// 是否最后一个元素[isLast]
  final bool isLast;

  /// 元素索引 [index]
  final int? index;

  /// 元素[item]
  final item;

  @override
  Widget build(BuildContext context) {
    return Wisdom.row(
      padding:
          EdgeInsets.fromLTRB(0, index == 0 ? 0 : 15.pt, 0, isLast ? 0 : 15.pt),
      border: Border(
          bottom: BorderSide(color: isLast ? Colors.white : Color(0xFFF4F4F4))),
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        WImage.network(
          item.headPortrait,
          height: 48.pt,
          width: 48.pt,
        ),
        Wisdom.column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          margin: EdgeInsets.only(left: 10.pt),
          children: <Widget>[
            WisText.c333(
              '${item?.nickname ?? ''}',
              size: 16.pt,
            ),
            WisText.c999(
              '来源客户端：${item?.thirdpartyName ?? ''}',
              size: 12.pt,
            )
          ],
        ),
      ],
    );
  }
}
