/*
 * @Author: XianKaiQun
 * @Date: 2020-07-15 09:20:29
 * @LastEditors: wuwei
 * @LastEditTime: 2023-12-13 16:12:28
 * @Description: 
 */
part of '../index.dart';

class _CardComponent extends StatelessWidget {
  const _CardComponent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<BillPlatformStore>(context, listen: true);
    final String? name = store.assetFullName;

    //只有房屋收单才显示二维码
    // Map<String, String> imageMap = {
    //   'house': 'lib/assets/build.png',
    //   'car': 'lib/assets/car.png',
    //   'parkingSpace': 'lib/assets/park.png',
    //   'owner': 'lib/assets/park.png',
    //   'waterMeter': 'lib/assets/park.png',
    //   'electricMeter': 'lib/assets/park.png'
    // };
    Map<String, dynamic> imageMap = {
      'house': Icons.house_outlined,
      'car': Icons.car_repair_outlined,
      'parkingSpace': Icons.local_parking_outlined,
      'owner': Icons.people_alt_outlined,
      'waterMeter': Icons.water_damage_outlined,
      'electricMeter': Icons.electric_meter_outlined
    };
    var typeName = {
      'house': '房屋',
      'car': '车辆',
      'parkingSpace': '车位',
      'owner': '住户',
      'waterMeter': '水表',
      'electricMeter': '电表',
    };
    return SliverToBoxAdapter(
      child: Stack(
        children: <Widget>[
          Container(height: 190.pt, color: colorScheme.primary),
          Wisdom.column(
              margin: EdgeInsets.all(
                15.pt,
              ),
              children: [
                WisCard(
                  clipBehavior: Clip.antiAlias,
                  margin: EdgeInsets.only(bottom: 15.pt),
                  child: Column(
                    children: <Widget>[
                      /// 顶部资产
                      Wisdom.row(
                        padding: EdgeInsets.all(15.pt),
                        children: <Widget>[
                          WisText(
                            ' $name',
                            size: 18.pt,
                            weight: FontWeight.bold,
                            overflow: TextOverflow.ellipsis,
                          ).asExpanded(),
                          if (store.assetList.length > 0)
                            Wisdom(
                              padding: EdgeInsets.fromLTRB(
                                15.pt,
                                10.pt,
                                0,
                                10.pt,
                              ),
                              onTap: () => store.changeShowAssetList(),
                              child: Row(
                                children: [
                                  WisText(
                                    store.isShowAssetlist ? '收起' : '展开',
                                    size: 14.pt,
                                    bold: true,
                                    color: colorScheme.primary,
                                  ),
                                  Icon(
                                    store.isShowAssetlist
                                        ? Icons.keyboard_double_arrow_up_sharp
                                        : Icons
                                            .keyboard_double_arrow_down_sharp,
                                    color: colorScheme.primary,
                                    size: 16.pt,
                                  ),
                                ],
                              ),
                            ),
                        ],
                      ),

                      /// 资产列表
                      if (store.isShowAssetlist) ...[
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            for (AssetListModel item in store.assetList)
                              if (store.assetType == 'owner' &&
                                  item.type == 'owner')
                                ...[]
                              else ...[
                                Wisdom.column(
                                  crossAxisAlignment:
                                      CrossAxisAlignment.stretch,
                                  mainAxisSize: MainAxisSize.min,
                                  padding: EdgeInsets.all(10.pt),
                                  margin: EdgeInsets.fromLTRB(
                                      15.pt, 0, 15.pt, 15.pt),
                                  color: colorScheme.background4,
                                  borderRadius: BorderRadius.circular(12.pt),
                                  children: [
                                    Row(
                                      children: [
                                        // Image.asset(
                                        //   imageMap[item.type]!,
                                        //   package: 'bill_package',
                                        //   width: 20.pt,
                                        // ),
                                        Icon(imageMap[item.type], size: 22.pt),
                                        SizedBox(
                                          width: 10.pt,
                                        ),
                                        WisText(
                                          typeName[item.type] ?? '',
                                          size: 15.pt,
                                          color: colorScheme.minor,
                                        )
                                      ],
                                    ),
                                    Divider(height: 20.pt),
                                    Wisdom.wrap(
                                      runSpacing: 10.pt,
                                      spacing: 10.pt,
                                      children: [
                                        for (AssetsOfAssetListModel asset
                                            in item.assets ?? [])
                                          Wisdom(
                                            onTap: () => store.changeAsset(
                                                asset, item.type),
                                            child: WisText(
                                              asset.name ?? '',
                                              color: colorScheme.primary,
                                              // color: asset.type != 'waterMeter' &&
                                              //         asset.type !=
                                              //             'electricMeter'
                                              //     ? colorScheme.primary
                                              //     : colorScheme.grey,
                                            ),
                                          ),
                                      ],
                                    ).asFlexible(flex: 1),
                                  ],
                                ).asFlexible(flex: 1),
                              ]
                          ],
                        )
                      ]
                    ],
                  ),
                ),

                /// 账单缴费卡片
                _BuildOweFeeCard(),
                SizedBox(height: 15.pt),

                /// 预存款缴费卡片
                _BuildPreFeeCard(),
                SizedBox(height: 15.pt),

                /// 押金缴费卡片
                _BuildDepositeFeeCard(),
                SizedBox(height: 15.pt),

                /// 智能表充值 暂时不做 2023-10-23
                // _BuildMeterFeeCard(),
              ]),
        ],
      ),
    );
  }
}

/// 账单缴费卡片
class _BuildOweFeeCard extends StatelessWidget {
  const _BuildOweFeeCard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<BillPlatformStore>(context, listen: true);
    final data = store.data;
    final String name = store.assetFullName ?? '';
    final num total = data?.payFee ?? 0;
    final String? communityId = store.community?.id;
    final String houseId = store.routingArgs?['assetId'];

    final colorScheme = WTheme.of(context).colorScheme;

    _BuildFeeItem(String left, String right) {
      return Wisdom.row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        padding: EdgeInsets.symmetric(vertical: 2.pt),
        children: [
          WisText(
            '$left',
            color: colorScheme.minor,
            overflow: TextOverflow.ellipsis,
          ).asExpanded(),
          SizedBox(
            width: 10.pt,
          ),
          WisText(
            '￥$right',
            color: colorScheme.minor,
          ),
        ],
      );
    }

    return FeeCard(
      title: "账单缴费",
      onTopTap: () => store.toOweFeeDetail(),
      icon: WImage.network(
        "https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/680755916033998848",
        width: 25.pt,
        height: 25.pt,
      ),
      child: Wisdom.column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        padding: EdgeInsets.symmetric(
          vertical: 15.pt,
          horizontal: 10.pt,
        ),
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              WisText(
                '￥$total',
                size: 36.pt,
                weight: FontWeight.w600,
                color: colorScheme.red,
              ),
              SizedBox(width: 10.pt),
              if (data?.liquidatedDamagesFee != null &&
                  data!.liquidatedDamagesFee! > 0) ...[
                WisText(
                  '(违约￥${data.liquidatedDamagesFee})',
                  size: 14.pt,
                  color: colorScheme.red,
                ),
              ]
            ],
          ),
          if (data?.feeTypeFees != null && data!.feeTypeFees!.length > 0) ...[
            Wisdom(
              margin: EdgeInsets.symmetric(
                vertical: 10.pt,
              ),
              borderRadius: BorderRadius.circular(12.pt),
              padding: EdgeInsets.all(15.pt),
              color: colorScheme.background4,
              child: Column(
                children: [
                  for (FeeTypeFeesOfOweFeeCountModel item
                      in data.feeTypeFees ?? [])
                    _BuildFeeItem(item.feeType ?? '',
                        item.fee != null ? '${item.fee}' : ''),
                ],
              ),
            ),
          ] else ...[
            SizedBox(height: 10.pt),
          ],
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (store.assetType == 'house') ...[
                Wisdom.row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  onTap: () => _showButtomModal(
                    context: context,
                    name: '$name',
                    total: '$total',
                    communityId: '$communityId',
                    assetId: '$houseId',
                    assetType: store.assetType ?? '',
                    identification: store.identification,
                    feeTypeId: store.feeTypeId,
                    end: store.endTime,
                    start: store.startTime,
                  ),
                  children: [
                    Wisdom(
                      child: Icon(
                        Icons.qr_code,
                        size: 25.pt,
                        color: colorScheme.mainTitle,
                      ),
                    ),
                    SizedBox(width: 10.pt),
                    WisText(
                      '生成缴费码',
                      size: 16.pt,
                      color: colorScheme.mainTitle,
                    ),
                  ],
                ).asFlexible(flex: 1),
                Wisdom(
                  height: 25.pt,
                  width: 1.pt,
                  color: colorScheme.divider,
                ),
              ],
              Wisdom.row(
                onTap: () => store.toOweFeeFilter(),
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Wisdom(
                    padding: EdgeInsets.only(left: 15.pt),
                    child: Icon(
                      Icons.filter_alt_outlined,
                      size: 25.pt,
                      color: colorScheme.mainTitle,
                    ),
                  ),
                  SizedBox(width: 10.pt),
                  WisText(
                    '费用筛选',
                    size: 16.pt,
                    color: colorScheme.mainTitle,
                  ),
                ],
              ).asFlexible(flex: 1),
            ],
          )
        ],
      ),
    );
  }
}

/// 预存款缴费卡片
class _BuildPreFeeCard extends StatelessWidget {
  const _BuildPreFeeCard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<BillPlatformStore>(context, listen: true);
    return FeeCard(
      title: '预存款缴费',
      fee: store.preDetail.totalFee,
      onTopTap: () => store.toPreFee(),
      icon: WImage.network(
        "https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/685540739772563456",
        width: 25.pt,
        height: 25.pt,
      ),
      child: Column(
        children: [
          if (store.isShowPreFee == false) ...[
            Wisdom.row(
              onTap: () => store.changeShowPreFee(true),
              padding: EdgeInsets.symmetric(vertical: 15.pt),
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.add_circle_outline,
                  size: 18.pt,
                  color: colorScheme.placeholder2,
                ),
                SizedBox(
                  width: 10.pt,
                ),
                WisText(
                  '点击添加预存款缴费录单',
                  color: colorScheme.placeholder2,
                ),
              ],
            ),
          ],
          if (store.isShowPreFee == true) ...[
            Wisdom.column(
              padding: EdgeInsets.symmetric(
                horizontal: 15.pt,
                vertical: 10.pt,
              ),
              children: [
                _BuildFormItem(
                  title: '预存资产',
                  onChange: () => store.choosePreAsset(),
                  value: store.preAssetItem.name,
                ),
                SizedBox(height: 10.pt),
                _BuildFormItem(
                  title: '预存项目',
                  onChange: () => store.choosePrefeeCharge(),
                  value: store.preFeeChargeItem.feeName,
                ),
                if (store.preFeeChargeItem.prepay == true) ...[
                  SizedBox(height: 10.pt),
                  Wisdom.row(
                    children: [
                      WisText(
                        '每月费用',
                        size: 16.pt,
                      ),
                      SizedBox(
                        width: 15.pt,
                      ),
                      Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        color: colorScheme.background4,
                        padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 5.pt, 10.pt),
                        borderRadius: BorderRadius.circular(4.pt),
                        children: [
                          WisText(
                            '${store.preFeeChargeItem.receivableFee != null ? store.preFeeChargeItem.receivableFee : ''}',
                            color: colorScheme.placeholder2,
                          ),
                        ],
                      ).asFlexible(flex: 1),
                    ],
                  ),
                  SizedBox(height: 10.pt),
                  Wisdom.row(
                    children: [
                      WisText(
                        '预存金额',
                        size: 16.pt,
                      ),
                      SizedBox(
                        width: 15.pt,
                      ),
                      Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        color: colorScheme.background4,
                        padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 5.pt, 10.pt),
                        borderRadius: BorderRadius.circular(4.pt),
                        children: [
                          WisText(
                            '${store.preFee}',
                            color: colorScheme.placeholder2,
                          ),
                        ],
                      ).asFlexible(flex: 1),
                    ],
                  ),
                ],
                if (store.preFeeChargeItem.identification != null) ...[
                  SizedBox(height: 10.pt),
                  Wisdom.row(
                    children: [
                      WisText(
                        store.preFeeChargeItem.prepay == true ? '预存月份' : '预存金额',
                        size: 16.pt,
                      ),
                      SizedBox(
                        width: 15.pt,
                      ),
                      Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        color: colorScheme.background4,
                        padding: EdgeInsets.fromLTRB(6.pt, 1.pt, 5.pt, 1.pt),
                        borderRadius: BorderRadius.circular(4.pt),
                        children: [
                          Expanded(
                            child: TextField(
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: 15.pt,
                                color: colorScheme.primary,
                              ),
                              keyboardType: TextInputType.numberWithOptions(
                                decimal: store.preFeeChargeItem.prepay == true
                                    ? false
                                    : true,
                              ),
                              decoration: InputDecoration(
                                fillColor: colorScheme.background4,
                                hintText: store.preFeeChargeItem.prepay == true
                                    ? '输入预存月份'
                                    : '输入预存金额',
                                hintStyle: TextStyle(
                                  fontSize: 14.pt,
                                  color: colorScheme.placeholder2,
                                  fontWeight: FontWeight.w400,
                                ),
                              ),
                              scrollPadding: EdgeInsets.zero,
                              onChanged: (value) {
                                String trimmedValue = value.trim();
                                store.inputPrefee(trimmedValue);
                              },
                            ),
                          ),
                        ],
                      ).asFlexible(flex: 1),
                    ],
                  ),
                ],
              ],
            ),
          ]
        ],
      ),
    );
  }
}

/// 押金缴费卡片
class _BuildDepositeFeeCard extends StatelessWidget {
  const _BuildDepositeFeeCard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<BillPlatformStore>(context, listen: true);
    return FeeCard(
      title: '押金缴费',
      fee: store.depositeDetail.balance,
      onTopTap: () => store.toDepositeFee(),
      icon: WImage.network(
        "https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/733026334022205440",
        width: 25.pt,
        height: 25.pt,
      ),
      child: Column(
        children: [
          if (store.isShowDepositeFee == false) ...[
            Wisdom.row(
              onTap: () => store.changeShowDepositeFee(true),
              padding: EdgeInsets.symmetric(vertical: 15.pt),
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.add_circle_outline,
                  size: 18.pt,
                  color: colorScheme.placeholder2,
                ),
                SizedBox(
                  width: 10.pt,
                ),
                WisText(
                  '点击添加押金缴费录单',
                  color: colorScheme.placeholder2,
                ),
              ],
            ),
          ],
          if (store.isShowDepositeFee == true) ...[
            Wisdom.column(
              padding: EdgeInsets.symmetric(
                horizontal: 15.pt,
                vertical: 10.pt,
              ),
              children: [
                _BuildFormItem(
                  title: '押金资产',
                  onChange: () => store.chooseDepositeAsset(),
                  value: store.depositeAssetItem.name,
                ),
                SizedBox(height: 10.pt),
                _BuildFormItem(
                  title: '押金项目',
                  onChange: () => store.chooseDepositeCharge(),
                  value: store.depositeChargeItem.name,
                ),
                if (store.depositeChargeItem.identification != null) ...[
                  SizedBox(height: 10.pt),
                  Wisdom.row(
                    children: [
                      WisText(
                        '押金金额',
                        size: 16.pt,
                      ),
                      SizedBox(
                        width: 15.pt,
                      ),
                      Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        color: colorScheme.background4,
                        padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 5.pt, 10.pt),
                        borderRadius: BorderRadius.circular(4.pt),
                        children: [
                          WisText(
                            '${store.depositeChargeItem.fee != null ? store.depositeChargeItem.fee : 0}',
                            color: colorScheme.placeholder2,
                          ),
                        ],
                      ).asFlexible(flex: 1),
                    ],
                  ),
                ],
              ],
            ),
          ]
        ],
      ),
    );
  }
}

/// 智能表充值
// class _BuildMeterFeeCard extends StatelessWidget {
//   const _BuildMeterFeeCard({Key? key}) : super(key: key);

//   @override
//   Widget build(BuildContext context) {
//     return FeeCard(
//       title: '智能表充值',
//       icon: WImage.network(
//         "https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/677111202936242176",
//         width: 25.pt,
//         height: 25.pt,
//       ),
//       child: Column(),
//     );
//   }
// }

class FeeCard extends StatelessWidget {
  const FeeCard({
    Key? key,
    required this.title,
    required this.icon,
    required this.child,
    this.onTopTap,
    this.fee,
  }) : super(key: key);

  final Function()? onTopTap;
  final Widget child;
  final num? fee;
  final Widget icon;
  final String title;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return WisCard(
      child: Column(
        children: [
          Wisdom.row(
            onTap: () => onTopTap!(),
            // color: Color(0xffE9F3FC),
            // color: Color(0xffE9E9E9),
            padding: EdgeInsets.all(15.pt),
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  icon,
                  SizedBox(width: 10.pt),
                  WisText(
                    title,
                    size: 16.pt,
                    weight: FontWeight.w600,
                    color: colorScheme.mainTitle,
                  )
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  WisText(
                    '${fee != null ? '余额:' : ''}',
                    color: colorScheme.minor,
                  ),
                  WisText(
                    '${fee != null ? '￥$fee' : ''}',
                    color: colorScheme.minor,
                    bold: true,
                  ),
                  SizedBox(width: 3.pt),
                  Icon(
                    Icons.arrow_forward_ios,
                    size: 13.pt,
                    color: colorScheme.grey,
                  ),
                ],
              )
            ],
          ),
          Divider(
            color: colorScheme.divider,
          ),
          child,
        ],
      ),
    );
  }
}

class _BuildFormItem extends StatelessWidget {
  const _BuildFormItem({
    Key? key,
    required this.title,
    required this.onChange,
    this.value,
  }) : super(key: key);

  final Function() onChange;
  final String title;
  final dynamic value;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Wisdom.row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      onTap: () => onChange(),
      children: [
        WisText(
          '$title',
          size: 16.pt,
        ),
        SizedBox(
          width: 15.pt,
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          color: colorScheme.background4,
          padding: EdgeInsets.fromLTRB(15.pt, 7.pt, 5.pt, 7.pt),
          borderRadius: BorderRadius.circular(4.pt),
          children: [
            WisText(
              '${value != null ? '$value' : '选择$title'}',
              color: value != null
                  ? colorScheme.primary
                  : colorScheme.placeholder2,
              overflow: TextOverflow.ellipsis,
            ).asExpanded(),
            Icon(
              Icons.arrow_right_rounded,
              color: colorScheme.placeholder2,
              size: 30.pt,
            ),
          ],
        ).asFlexible(flex: 1),
      ],
    );
  }
}

/// 扫码弹窗
_showButtomModal({
  required context,
  required String name,
  required String total,
  required String communityId,
  required String assetId,
  required String assetType,
  required String identification,
  required String feeTypeId,
  required String start,
  required String end,
}) {
  final colorScheme = WTheme.of(context).colorScheme;
  final entCode = WisStorageUtil.get(key: 'storage-user-entCode');
  WBottomModalUtil.show(
    context,
    plain: true,
    title: WisText(
      '收费二维码',
      size: 16.pt,
    ),
    bodyConstraints: BoxConstraints(maxHeight: 400.pt),
    body: Column(
      children: [
        WisText(
          '$name',
          size: 15.pt,
        ),
        SizedBox(height: 15.pt),
        Wisdom.rich(
          children: [
            TextSpan(
              text: '欠费总额：',
            ),
            TextSpan(
              text: '￥$total',
              style: TextStyle(
                color: colorScheme.danger,
                fontSize: 25.pt,
              ),
            ),
          ],
        ),
        SizedBox(height: 15.pt),
        // url拼接规则:assetId_assetType_开始账期(空则传none)_结束账期_费用类型还是收费项目_项目id_小区id_企业id
        QrImageView(
          data:
              'https://m.huifuwu.cn/qrcode/payCashier_${assetId}_${assetType}${start != '' ? '_$start' : '_none'}_${end}_${identification == '' ? 'feeType' : 'project'}_${identification == '' ? feeTypeId == '' ? 'all' : feeTypeId : identification}_${communityId}_${entCode}',
          version: QrVersions.auto,
          size: 320.pt,
        ),
        SizedBox(height: 15.pt),
        WisText(
          '微信扫描二维码进行缴费',
          size: 15.pt,
        ),
      ],
    ),
  );
}
