/*
 * @Author: WuWei
 * @Date: 2020-06-04 17:08:09
 * @LastEditors: WuWei
 * @LastEditTime: 2020-06-17 11:09:11
 * @Description:
 */
part of '../index.dart';

final Map carsType = {
  'smallCar': '小型汽车',
  'mediumCar': '中型汽车',
  'largeCar': '大型汽车',
  'motorcycle': '摩托车',
  'electricBike': '电动自行车',
  'tricycle': '三轮车'
};

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

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

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

class __CarinfoState extends State<_Carinfo> {
  Future<void> _onInit() async {
    if (widget.onCarInit != null) {
      await widget.onCarInit!();
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SearchResidentDetailStore>(context);
    if (store.residentCar == null || store.residentCar!.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.residentCar!.length > 0
                  ? (store.residentCar!
                          .map((item) => new _CarItem(
                              item: item,
                              isLast: store.residentCar!.length ==
                                  (store.residentCar!.indexOf(item) + 1),
                              index: store.residentCar!.indexOf(item)))
                          .toList())
                  : [],
            ),
          ),
        )
      ],
    );
  }
}

/// 绑定的微信
class _CarItem extends StatelessWidget {
  const _CarItem({
    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?.cardNo ?? ''}'),
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            WisText.c999(
              '车辆类型：${carsType[item?.type] ?? ''}',
              size: 12.pt,
            ),
            WisText.c999(
              '是否包月：${item.monthSubscribe ? '是' : '否'}',
              size: 12.pt,
            )
          ],
        ),
      ],
    );
  }
}
