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

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

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

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

class __HouseInfoState extends State<_HouseInfo> {
  Future<void> _onInit() async {
    if (widget.onHouseInit != null) {
      await widget.onHouseInit!();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SearchResidentDetailStore>(context);
    if (store.residentHouse == null || store.residentHouse!.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.residentHouse!.length > 0
                  ? (store.residentHouse!
                          .map((item) => new _HouseItem(item: item, isLast: store.residentHouse!.length == (store.residentHouse!.indexOf(item) + 1), index: store.residentHouse!.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) {
    return Wisdom.column(
      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?.buildName ?? ''}/${item?.unitName ?? ''}/${item?.roomNo ?? ''}'),
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            WisText.c999(
              '面积：${item?.buildArea ?? ''}',
              size: 12.pt,
            ),
            WisText.c999(
              '房屋状态：${item.isCheckIn ? '已入住' : ''}',
              size: 12.pt,
            )
          ],
        ),
      ],
    );
  }
}
