/*
 * @Author: WuWei
 * @Date: 2020-06-10 15:00:11
 * @LastEditors: WuWei
 * @LastEditTime: 2020-06-11 16:37:17
 * @Description:
 */
part of '../index.dart';

const List meterHeader = [
  '抄表时间',
  '抄表读数',
  '计费行度',
  '用量',
];

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

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

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

class __MeterInfoState extends State<_MeterInfo> {
  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.houseMeter.length == 0) {
      return InfoEmpty();
    }
    return CustomScrollView(
      physics: ScrollPhysics(),
      slivers: store.houseMeter
          .map((item) => new _MeterItem(
                infoItem: item,
              ))
          .toList(),
    );
  }
}

/// 绑定的微信
class _MeterItem extends StatelessWidget {
  const _MeterItem({
    Key? key,
    this.infoItem,
  }) : super(key: key);

  /// 元素[infoItem]
  final infoItem;

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SearchHouseDetailStore>(context);
    // final style = WTheme.of(context);final colorScheme = style.colorScheme;
    return Container(
      child: 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: <Widget>[
                Wisdom.row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  padding: EdgeInsets.only(bottom: 10.pt),
                  // border: Border(bottom: BorderSide(color: colorScheme.divider)),
                  children: <Widget>[
                    Wisdom.row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Wisdom(
                          borderRadius: BorderRadius.circular(2.5.pt),
                          color: Color(0xFF2D8CF0),
                          width: 3.pt,
                          height: 17.pt,
                          margin: EdgeInsets.only(right: 5.pt),
                        ),
                        WisText.c666(
                          '${infoItem?.meterName ?? ''}',
                          size: 16.pt,
                        )
                      ],
                    ),
                    Wisdom.row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Wisdom(
                          color: Color(0xFFFA6400),
                          borderRadius: BorderRadius.circular(10.pt),
                          margin: EdgeInsets.only(right: 10.pt),
                          padding: EdgeInsets.fromLTRB(8.pt, 2.pt, 8.pt, 2.pt),
                          child: WisText.white(
                            '倍率${infoItem?.multiple ?? 1}',
                            size: 12.pt,
                          ),
                        ),
                        Wisdom(
                          borderRadius: BorderRadius.circular(10.pt),
                          padding: EdgeInsets.fromLTRB(8.pt, 2.pt, 8.pt, 2.pt),
                          color: store
                              .meterColor[infoItem?.meterType ?? 'waterMeter'],
                          child: WisText.white(
                            store.meterType[infoItem?.meterType] ?? '',
                            size: 12.pt,
                          ),
                        ),
                      ],
                    )
                  ],
                ),
                Divider(
                  height: 10.pt,
                ),
                // Flex(direction: ),
                _MeterTable(latelyAmounts: infoItem?.latelyAmounts ?? []),
              ],
            )),
      ),
    );
  }
}

class _MeterTable extends StatelessWidget {
  const _MeterTable({
    Key? key,
    this.latelyAmounts,
  }) : super(key: key);

  final latelyAmounts;

  @override
  Widget build(BuildContext context) {
    return Wisdom.column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Table(columnWidths: <int, TableColumnWidth>{
          0: FlexColumnWidth(1.8),
          1: FlexColumnWidth(1.0),
          2: FlexColumnWidth(1.0),
          3: FlexColumnWidth(1.0),
        }, children: [
          TableRow(
            children: meterHeader
                .map((item) => new Wisdom(
                      padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                      child: WisText.c333(
                        item,
                        size: 14.pt,
                      ),
                    ))
                .toList(),
          ),
        ]),
        Table(
          columnWidths: <int, TableColumnWidth>{
            0: FlexColumnWidth(1.8),
            1: FlexColumnWidth(1.0),
            2: FlexColumnWidth(1.0),
            3: FlexColumnWidth(1.0),
          },
          children: [
            for (var meter in latelyAmounts)
              TableRow(
                children: <Widget>[
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      meter?.readerDate ?? '',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${meter?.transcribeAmount ?? ' '}',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${meter?.chargeAmount ?? ' '}',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${meter?.actualAmount ?? ''}',
                      size: 12.pt,
                    ),
                  ),
                ],
              )
          ],
        ),
      ],
    );
  }
}
