/*
 * @Author: WuWei
 * @Date: 2020-06-04 17:08:31
 * @LastEditors: WuWei
 * @LastEditTime: 2020-06-10 15:38:38
 * @Description:
 */

part of '../index.dart';

final Map parkType = {
  'titleParking': '产权车位',
  'publicParking': '公共车位',
  'defenseParking': '人防车位',
  'parkingCard': '停车卡',
  'storageRoom': '储藏间',
};

final Map parkStatus = {
  'sold': '已售',
  'rented': '已租',
  'vacant': '空置',
  'personal': '自用',
  'waitSales': '待售',
  'waitRent': '待租',
  'tempStop': '临停'
};

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

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

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

class __ParkinfoState extends State<_Parkinfo> {
  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.residentPark == null || store.residentPark!.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.residentPark!.length > 0
                  ? (store.residentPark!
                          .map((item) => new _ParkItem(
                              item: item,
                              isLast: store.residentPark!.length ==
                                  (store.residentPark!.indexOf(item) + 1),
                              index: store.residentPark!.indexOf(item)))
                          .toList()
                      )
                  : [],
            ),
          ),
        )
      ],
    );
  }
}

/// 绑定的微信
class _ParkItem extends StatelessWidget {
  const _ParkItem({
    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?.code ?? ''}'),
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            WisText.c999(
              '车辆类型：${parkType[item?.type] ?? ''}',
              size: 12.pt,
            ),
            WisText.c999(
              '车位状态：${parkStatus[item?.status] ?? ''}',
              size: 12.pt,
            )
          ],
        ),
      ],
    );
  }
}
