/*
 * @Author       : WuWei
 * @LastEditors: wuwei
 * @Date         : 2023-03-13 17:15:13
 * @LastEditTime: 2024-02-01 17:13:55
 * @Description  : Do not edit
 */

part of '../index.dart';

class _ListComponent extends StatelessWidget {
  final detail;
  const _ListComponent({Key? key, this.detail}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<_Store>(context);
    final colorScheme = WTheme.of(context).colorScheme;

    /// 明细元素
    _buildFeeItem(String left, String right) {
      return Padding(
        padding: EdgeInsets.symmetric(vertical: 6.pt),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            WisText(
              left,
              // size: 15.pt,
              color: colorScheme.minor,
            ),
            WisText(
              right,
              color: colorScheme.mainTitle,
            )
          ],
        ),
      );
    }

    /// 缴费明细弹窗
    feeDetailDialog(BillsOfDetailInfosOfOweFeeInfoModel data, assetName,
        LiquidatedDamagesModel info) async {
      Get.dialog(
        Center(
          child: Container(
            padding: EdgeInsets.all(15.pt),
            height: MediaQuery.of(context).size.height * 0.7,
            width: 307.pt,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(15.pt),
            ),
            child: Column(children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  WisText('账单明细', size: 18.pt, bold: true),
                  Wisdom(
                    onTap: () => Get.back(),
                    child: Icon(Icons.close, size: 20.pt),
                  )
                ],
              ),
              SizedBox(
                height: 15.pt,
              ),
              SingleChildScrollView(
                child: Column(
                  children: [
                    _buildFeeItem('资产', assetName),
                    _buildFeeItem('账期', data.chargeYearMonth ?? '-'),
                    _buildFeeItem(
                      '收费类型',
                      data.feeType ?? '-',
                    ),
                    _buildFeeItem('收费项目', data.feeName ?? '-'),
                    _buildFeeItem(
                      '单价',
                      '￥${data.price ?? '-'} ${data.unit ?? ''}',
                    ),
                    _buildFeeItem(
                      '数量',
                      '${data.quantity ?? '-'}',
                    ),
                    _buildFeeItem(
                      '应缴',
                      '￥${data.receivableFee ?? '-'}',
                    ),
                    _buildFeeItem(
                      '减免',
                      '￥${data.discountFee ?? '-'}',
                    ),
                    _buildFeeItem(
                      '已缴',
                      '￥${data.receivedFee ?? '-'}',
                    ),
                    _buildFeeItem(
                      '欠费',
                      '￥${data.arrearsFee ?? '-'}',
                    ),
                    if (data.serviceStartAt != null) ...[
                      _buildFeeItem(
                        '服务开始时间',
                        '${data.serviceStartAt ?? '-'}',
                      ),
                    ],
                    if (data.serviceEndAt != null) ...[
                      _buildFeeItem(
                        '服务结束时间',
                        '${data.serviceEndAt ?? '-'}',
                      ),
                    ],
                    if (info.liquidatedDamagesStartAt != null) ...[
                      _buildFeeItem(
                        '违约起计日期',
                        '${info.liquidatedDamagesStartAt ?? '-'}',
                      ),
                    ],
                    if (info.breachQuantity != null) ...[
                      _buildFeeItem(
                        '违约天数',
                        '${info.breachQuantity ?? '-'}',
                      ),
                    ],
                    if (info.coefficient != null) ...[
                      _buildFeeItem(
                        '违约系数',
                        info.coefficient != null
                            ? ' ${info.coefficient}‰'
                            : '-',
                      ),
                    ],
                    if (info.receivableFee != null &&
                        info.receivableFee! > 0) ...[
                      _buildFeeItem(
                        '违约金应缴',
                        '￥${info.receivableFee ?? '-'}',
                      ),
                    ],
                    if (info.discountFee != null && info.discountFee! > 0) ...[
                      _buildFeeItem(
                        '违约金减免',
                        '￥${info.discountFee ?? '-'}',
                      ),
                    ],
                    if (info.receivedFee != null && info.receivedFee! > 0) ...[
                      _buildFeeItem(
                        '违约金已缴',
                        '￥${info.receivedFee ?? '-'}',
                      ),
                    ],
                    if (info.arrearsFee != null) ...[
                      _buildFeeItem(
                        '违约金欠费',
                        '￥${info.arrearsFee ?? '-'}',
                      ),
                    ],
                  ],
                ),
              ).asExpanded(),
            ]),
          ),
        ),
        routeSettings: RouteSettings(arguments: data),
        name: '账单明细',
      );
    }

    bool isInteger(num value) => value is int || value == value.roundToDouble();

    return Column(
      children: [
        for (var item in detail?.detailInfos ?? [])
          Wisdom.column(
            // color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 15.pt),
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(bottom: 10.pt),
                child: Wisdom.row(
                  children: [
                    // Image.asset(
                    //   'lib/assets/bj2.png',
                    //   package: 'bill_package',
                    //   width: 22.pt,
                    // ),
                    // SizedBox(width: 5.pt),
                    WisText(
                      item.assetName ?? '',
                      size: 16.pt,
                      bold: true,
                      color: colorScheme.mainTitle,
                    ),
                  ],
                ),
              ),
              Wisdom(
                color: colorScheme.background,
                borderRadius: BorderRadius.circular(8.pt),
                child: Column(
                  children: [
                    Padding(
                      padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 15.pt, 0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Wisdom(
                            width: 100.pt,
                            child: WisText(
                              '费项名称',
                              size: 14.pt,
                              color: colorScheme.mainTitle,
                              bold: true,
                            ),
                          ),
                          Wisdom(
                            child: WisText(
                              '应缴',
                              size: 14.pt,
                              textAlign: TextAlign.right,
                              bold: true,
                              color: colorScheme.mainTitle,
                            ),
                          ),
                          Wisdom(
                            child: WisText(
                              '已缴',
                              size: 14.pt,
                              textAlign: TextAlign.right,
                              bold: true,
                              color: colorScheme.mainTitle,
                            ),
                          ),
                          Wisdom(
                            child: WisText(
                              '欠费',
                              size: 14.pt,
                              textAlign: TextAlign.right,
                              bold: true,
                              color: colorScheme.mainTitle,
                            ),
                          ),
                        ],
                      ),
                    ),
                    for (var bill in item.bills ?? []) ...[
                      Padding(
                        padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 15.pt, 0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Wisdom.row(
                              width: 100.pt,
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Wisdom(
                                  onTap: () async => {
                                    await store.getLiquidatedDetails(bill),
                                    feeDetailDialog(
                                        bill, item.assetName, store.info)
                                  },
                                  child: WisText(
                                    bill?.feeName ?? '',
                                    size: 14.pt,
                                    color: colorScheme.green,
                                    overflow: TextOverflow.ellipsis,
                                    decoration: TextDecoration.underline,
                                  ),
                                ),
                                if (bill.discountFee > 0 ||
                                    bill.breachDiscountFee > 0) ...[
                                  Icon(
                                    Icons.info,
                                    color: colorScheme.green,
                                    size: 12.pt,
                                  ),
                                ],
                                if (bill.breachReceivableFee != null &&
                                    bill.breachReceivableFee > 0) ...[
                                  Icon(
                                    Icons.info,
                                    color: colorScheme.red,
                                    size: 12.pt,
                                  ),
                                ]
                              ],
                            ),
                            Wisdom(
                              alignment: Alignment.centerRight,
                              child: WisText(
                                '${bill.receivableFee != null || bill.discountFee != null || bill.breachReceivableFee != null || bill.breachDiscountFee != null ? isInteger(bill.receivableFee - bill.discountFee + bill.breachReceivableFee - bill.breachDiscountFee) ? (bill.receivableFee - bill.discountFee + bill.breachReceivableFee - bill.breachDiscountFee).round() : (bill.receivableFee - bill.discountFee + bill.breachReceivableFee - bill.breachDiscountFee).toStringAsFixed(2) : 0}',
                                size: 14.pt,
                                textAlign: TextAlign.right,
                                color: colorScheme.minor,
                              ),
                            ).asFlexible(flex: 1),
                            Wisdom(
                              alignment: Alignment.centerRight,
                              child: WisText(
                                '${bill.receivedFee != null || bill.breachReceivedFee != null ? isInteger(bill.receivedFee + bill.breachReceivedFee) ? (bill.receivedFee + bill.breachReceivedFee).round() : (bill.receivedFee + bill.breachReceivedFee).toStringAsFixed(2) : 0}',
                                size: 14.pt,
                                textAlign: TextAlign.right,
                                color: colorScheme.minor,
                              ),
                            ).asFlexible(flex: 1),
                            Wisdom(
                              alignment: Alignment.centerRight,
                              child: WisText(
                                '${bill.arrearsFee != null || bill.breachArrearsFee != null ? isInteger(bill.arrearsFee + bill.breachArrearsFee) ? (bill.arrearsFee + bill.breachArrearsFee).round() : (bill.arrearsFee + bill.breachArrearsFee).toStringAsFixed(2) : 0}',
                                size: 14.pt,
                                textAlign: TextAlign.right,
                                color: colorScheme.red,
                              ),
                            ).asFlexible(flex: 1),
                          ],
                        ),
                      ),
                    ],
                    SizedBox(height: 10.pt),
                  ],
                ),
              ),
            ],
          ),
      ],
    );
  }
}
