/*
 * @Author: WuWei
 * @Date: 2020-06-05 10:20:25
 * @LastEditors: WuWei
 * @LastEditTime: 2020-06-12 15:18:20
 * @Description:
 */
part of '../index.dart';

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

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

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

class __ResidentState extends State<_ResidentInfo> {
  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<SearchHouseDetailStore>(context);
    if (store.houseAllResident.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),
                boxShadow: [
                  BoxShadow(
                    color: Color(0x40000000),
                    blurRadius: 10.pt,
                    spreadRadius: -5.pt,
                  )
                ],
                children: <Widget>[
                  Wisdom.row(
                    mainAxisSize: MainAxisSize.max,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    borderRadius: BorderRadius.vertical(
                      top: Radius.circular(14.pt),
                    ),
                    padding: EdgeInsets.all(15.pt),
                    color: Color(0xFFEF8F8F),
                    children: <Widget>[
                      Wisdom(
                        borderRadius: BorderRadius.circular(100.pt),
                        width: 12.pt,
                        height: 12.pt,
                        color: Colors.white,
                        margin: EdgeInsets.only(right: 5.pt),
                      ),
                      WisText.white(
                        '当前住户',
                        size: 16.pt,
                      )
                    ],
                  ),
                  Wisdom.column(
                    mainAxisSize: MainAxisSize.min,
                    padding: EdgeInsets.all(15.pt),
                    children: (store.houseResident.length > 0)
                        ? (store.houseResident
                            .map((item) => new _HouseItem(
                                item: item,
                                isLast: store.houseResident.length ==
                                    (store.houseResident.indexOf(item) + 1),
                                index: store.houseResident.indexOf(item)))
                            .toList())
                        : [],
                  ),
                ],
              )),
        ),
        if (store.houseHistoryResident.length > 0) ...[
          SliverToBoxAdapter(
            child: Wisdom(
                padding: EdgeInsets.all(10.pt),
                child: Wisdom.column(
                  mainAxisSize: MainAxisSize.min,
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(14.pt),
                  boxShadow: [
                    BoxShadow(
                      color: Color(0x40000000),
                      blurRadius: 10.pt,
                      spreadRadius: -5.pt,
                    )
                  ],
                  children: <Widget>[
                    Wisdom.row(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      borderRadius: BorderRadius.vertical(
                        top: Radius.circular(14.pt),
                      ),
                      padding: EdgeInsets.all(15.pt),
                      color: Color(0xFFCCCCCC),
                      children: <Widget>[
                        Wisdom(
                          borderRadius: BorderRadius.circular(100.pt),
                          width: 12.pt,
                          height: 12.pt,
                          color: Colors.white,
                          margin: EdgeInsets.only(right: 5.pt),
                        ),
                        WisText.white(
                          '历史住户',
                          size: 16.pt,
                        )
                      ],
                    ),
                    Wisdom.column(
                      mainAxisSize: MainAxisSize.min,
                      padding: EdgeInsets.all(15.pt),
                      children: (store.houseHistoryResident
                          .map((item) => new _HouseItem(
                              item: item,
                              isLast: store.houseHistoryResident.length ==
                                  (store.houseHistoryResident.indexOf(item) +
                                      1),
                              index: store.houseHistoryResident.indexOf(item)))
                          .toList()),
                    ),
                  ],
                )),
          ),
        ],
      ],
    );
  }
}

/// 绑定的微信
class _HouseItem extends StatelessWidget {
  const _HouseItem({
    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) {
    final store = Provider.of<SearchHouseDetailStore>(context);

    return Wisdom.column(
      onTap: () => store.route2ResidentDetail(item),
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      padding:
          EdgeInsets.fromLTRB(0, index == 0 ? 0 : 15.pt, 0, isLast ? 0 : 15.pt),
      border: Border(
          bottom: BorderSide(color: isLast ? Colors.white : Color(0xFFF4F4F4))),
      children: <Widget>[
        Wisdom(
          margin: EdgeInsets.only(bottom: 5.pt),
          child: WisText('${item?.name ?? ''}'),
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Wisdom(
              onTap: () => launch('tel:${item?.mobile}'),
              child: Wisdom.row(
                children: <Widget>[
                  Icon(FaIcons.phone, size: 12.pt, color: Color(0xFF2D8CF0)),
                  WisText('${item?.mobile ?? ''}',
                      size: 12.pt, color: Color(0xFF2D8CF0)),
                ],
              ),
            ),
            WisText.c999(
              '住户性质：${store.residentType[item?.identity] ?? ''}',
              size: 12.pt,
            )
          ],
        ),
      ],
    );
  }
}
