/*
 * @Author: WuWei
 * @Date: 2020-06-05 10:20:12
 * @LastEditors: WuWei
 * @LastEditTime: 2020-06-11 14:42:17
 * @Description:
 */
part of '../index.dart';

const List header = ['费用类型', '应收', '减免', '已收', '欠收'];

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

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

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

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

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

  /// 元素[item]
  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?.chargeYearMonth,
                          size: 16.pt,
                        )
                      ],
                    ),
                    Wisdom.row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Wisdom(
                          margin: EdgeInsets.only(right: 10.pt),
                          child: WisText.c999(
                            '待缴金额',
                            size: 12.pt,
                          ),
                        ),
                        WisText(
                          infoItem?.chargeArrearsFee.toString() ?? '',
                          size: 14.pt,
                          color: Color(0xFFE02020),
                        )
                      ],
                    )
                  ],
                ),
                Divider(
                  height: 10.pt,
                ),
                // Flex(direction: ),
                _BillTable(
                    arrearsFeeDetails: infoItem?.arrearsFeeDetails ?? []),
              ],
            )),
      ),
    );
  }
}

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

  final arrearsFeeDetails;

  @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),
          4: FlexColumnWidth(1.0),
        }, children: [
          TableRow(
            children: header
                .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),
            4: FlexColumnWidth(1.0),
          },
          children: [
            for (var bill in arrearsFeeDetails)
              TableRow(
                children: <Widget>[
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${bill?.feeName ?? ''}',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${bill?.receivableFeeTotal ?? ''}',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${bill?.discountFeeTotal ?? ''}',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText.c999(
                      '${bill?.receivedFeeTotal ?? ''}',
                      size: 12.pt,
                    ),
                  ),
                  Wisdom(
                    padding: EdgeInsets.fromLTRB(0, 3.pt, 0, 3.pt),
                    child: WisText(
                      '${bill?.arrearsFeeTotal ?? ''}',
                      size: 12.pt,
                      color: Color(0xFFE02020),
                    ),
                  ),
                ],
              )
          ],
        ),
      ],
    );
  }
}
