/*
 * @Author: ChenYaJin
 * @Date: 2021-11-05 10:49:19
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-31 09:12:39
 * @Description: 费用列表
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../../view_models/private_fee_model.dart';

final Color redColor = Color(0xFFF94F48);
final Color greenColor = Color(0xFF00BC84);
final Color yellowColor = Color(0xFFFD9025);
final Color blueColor = Color(0xFF4FA3FB);
Map payStatus = {'unCleared': '未结清', 'cleared': '已结清'};
Map payStatusColor = {'unCleared': redColor, 'cleared': greenColor};

class ListViewItem extends StatefulWidget {
  const ListViewItem({
    Key? key,
    this.detail,
    this.showCheck = false,
  }) : super(key: key);

  final detail;
  final bool showCheck;
  @override
  _ListViewItemState createState() => _ListViewItemState();
}

class _ListViewItemState extends State<ListViewItem> {
  @override
  Widget build(BuildContext context) {
    FeeIndexStore store;
    store = Provider.of<FeeIndexStore>(context);
    final serviceFeeDetail = store.serviceFeeDetail;
    final double width25 =
        ((MediaQuery.of(context).size.width - 30) * 0.25).toDouble();
    final double width30 =
        ((MediaQuery.of(context).size.width - 30) * 0.3).toDouble();
    final double width20 =
        ((MediaQuery.of(context).size.width - 30) * 0.2).toDouble();
    return store.list!.isNotEmpty
        ? WisCard(
            color: Colors.white,
            elevation: 0,
            margin: EdgeInsets.symmetric(horizontal: 15.pt),
            padding: EdgeInsets.symmetric(horizontal: 15.pt),
            child: Wisdom.column(children: <Widget>[
              Wisdom(
                padding: EdgeInsets.symmetric(vertical: 10.pt),
                alignment: Alignment.centerLeft,
                child: Wisdom.row(
                  children: [
                    WisText.c222('费用明细', size: 16.pt, weight: FontWeight.bold),
                    WTag(
                      child: Text(
                        payStatus[
                            serviceFeeDetail!.paymentStatus ?? 'unCleared'],
                        style: TextStyle(
                            color: payStatusColor[
                                serviceFeeDetail.paymentStatus ?? 'unCleared'],
                            fontSize: 11.pt),
                      ),
                      color: payStatusColor[
                          serviceFeeDetail.paymentStatus ?? 'unCleared'],
                      margin: EdgeInsets.only(left: 10.pt),
                    ),
                  ],
                ),
                border: Border(
                  bottom: BorderSide(
                    color: Color(0xffECECEC),
                  ),
                ),
              ),
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Table(
                  defaultVerticalAlignment: TableCellVerticalAlignment.middle,
                  columnWidths: <int, TableColumnWidth>{
                    0: MaxColumnWidth(
                        FixedColumnWidth(width30), FixedColumnWidth(120)),
                    1: MaxColumnWidth(
                        FixedColumnWidth(width25), FixedColumnWidth(90)),
                    2: MaxColumnWidth(
                        FixedColumnWidth(width20), FixedColumnWidth(60)),
                    3: MaxColumnWidth(
                        FixedColumnWidth(width25), FixedColumnWidth(100)),
                  },
                  children: [
                    TableRow(
                      children: ['项目名称', '单价', '数量', '金额(元)']
                          .asMap()
                          .entries
                          .map(
                            (e) => TableCell(
                              child: Wisdom(
                                padding: EdgeInsets.only(
                                    right: e.key == 3 ? 0 : 10.pt),
                                child: Text(
                                  e.value,
                                  maxLines: 1,
                                  textAlign: TextAlign.left,
                                  strutStyle: StrutStyle(leading: 1.5),
                                  style: TextStyle(fontWeight: FontWeight.bold),
                                ),
                              ),
                            ),
                          )
                          .toList(),
                    ),
                    for (var item in store.list ?? [])
                      TableRow(
                        children: [
                          '${item?.name}',
                          item?.price is num
                              ? '${item?.price.toString()}元/${item?.unit}'
                              : '--',
                          item?.quantity is num
                              ? '${item?.quantity.toString()}'
                              : item?.quantity ?? '--',
                          '${item.fee.toStringAsFixed(2)}'
                        ]
                            .asMap()
                            .entries
                            .map(
                              (e) => TableCell(
                                  child: _TableContentCell(
                                      e: e,
                                      item: item,
                                      check: widget.showCheck)),
                            )
                            .toList(),
                      ),
                  ],
                ),
              ),
              Wisdom(
                padding: EdgeInsets.symmetric(horizontal: 10.pt),
                child: Divider(
                  color: Color(0xffECECEC),
                  height: 20.pt,
                ),
              ),
              Wisdom.row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _MoneyCell(
                      title: '合计',
                      money:
                          '${serviceFeeDetail.serviceFee is num ? serviceFeeDetail.serviceFee!.toStringAsFixed(2) : serviceFeeDetail.serviceFee}',
                      textColor: redColor,
                      index: 0,
                    ).asFlexible(flex: 1),
                    _MoneyCell(
                      title: '已收款',
                      money:
                          '${serviceFeeDetail.receivedFee is num ? serviceFeeDetail.receivedFee!.toStringAsFixed(2) : serviceFeeDetail.receivedFee}',
                      textColor: blueColor,
                      index: 1,
                    ).asFlexible(flex: 1),
                    _MoneyCell(
                      title: '已减免',
                      money:
                          '${serviceFeeDetail.discountFee is num ? serviceFeeDetail.discountFee!.toStringAsFixed(2) : serviceFeeDetail.discountFee}',
                      textColor: greenColor,
                      index: 2,
                    ).asFlexible(flex: 1),
                    _MoneyCell(
                      title: '剩余',
                      money:
                          '${serviceFeeDetail.arrearsFee is num ? serviceFeeDetail.arrearsFee!.toStringAsFixed(2) : serviceFeeDetail.arrearsFee}',
                      textColor: yellowColor,
                      index: 3,
                    ).asFlexible(flex: 1),
                  ]),
            ]),
          )
        : _EmptyContent();
  }
}

class _EmptyContent extends StatelessWidget {
  const _EmptyContent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          WisCard(
              color: Colors.white,
              margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
              padding: EdgeInsets.symmetric(vertical: 35.pt),
              elevation: 0,
              child: Wisdom.column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Wisdom(
                      child: Image.asset(
                        'lib/assets/empty_plain.png',
                        package: 'wisdom_cli',
                        width: 105,
                      ),
                    ),
                    Wisdom(
                      margin: EdgeInsets.only(top: 25.pt),
                      child: Text(
                        '尚无费用',
                        style: TextStyle(
                          fontSize: 16.pt,
                          height: 2,
                        ),
                      ),
                    ),
                  ]))
        ]);
  }
}

class _TableContentCell extends StatelessWidget {
  const _TableContentCell({Key? key, this.e, this.item, this.check})
      : super(key: key);
  final e;
  final item;
  final bool? check;
  @override
  Widget build(BuildContext context) {
    FeeIndexStore store;
    store = Provider.of<FeeIndexStore>(context);
    return Wisdom(
        padding: EdgeInsets.only(right: e.key == 3 ? 0 : 10.pt),
        margin: EdgeInsets.symmetric(vertical: 7.pt),
        child: Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            e.key == 0 && check!
                ? item.isCreateBy
                    ? item.check
                        ? Wisdom(
                            color: Colors.white,
                            margin: EdgeInsets.only(right: 8.pt),
                            onTap: () => store.onCheckboxchange(item, false),
                            child: Image.asset(
                              'lib/assets/checkbox-select.png',
                              width: 16.pt,
                              height: 16.pt,
                              package: 'repair_package',
                            ),
                          )
                        : Wisdom(
                            color: Colors.white,
                            margin: EdgeInsets.only(right: 8.pt),
                            onTap: () => store.onCheckboxchange(item, true),
                            child: Image.asset(
                              'lib/assets/checkbox-normal.png',
                              width: 16.pt,
                              height: 16.pt,
                              package: 'repair_package',
                            ),
                          )
                    : Wisdom(
                        margin: EdgeInsets.only(right: 8.pt),
                        child: Image.asset(
                          'lib/assets/checkbox-disabled.png',
                          width: 16.pt,
                          height: 16.pt,
                          package: 'repair_package',
                        ))
                : Text(''),
            Text(
              e.value,
              textAlign: TextAlign.left,
              style: TextStyle(
                fontSize: 14.pt,
              ),
            ).asFlexible(flex: 1),
          ],
        ));
  }
}

class _MoneyCell extends StatelessWidget {
  const _MoneyCell(
      {Key? key, this.title, this.money, this.textColor, this.index})
      : super(key: key);
  final String? title;
  final String? money;
  final Color? textColor;
  final int? index;
  @override
  Widget build(BuildContext context) {
    return Wisdom(
      width: (MediaQuery.of(context).size.width / 4) - 15,
      margin: EdgeInsets.fromLTRB(0, 5.pt, 5.pt, 10.pt),
      border: Border(
        right: BorderSide(
          color: index == 3 ? Colors.white : Color(0xffECECEC),
        ),
      ),
      child: Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          WisText.c222(title ?? '', size: 14.pt),
          Wisdom(
            margin: EdgeInsets.only(top: 5.pt),
            child: Text.rich(
              TextSpan(
                children: [
                  WidgetSpan(
                    child: Text(
                      '￥${money ?? '0.00'}',
                      style: TextStyle(color: textColor, fontSize: 14.pt),
                    ),
                  ),
                ],
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          // Wisdom(
          //   margin: EdgeInsets.only(top: 5.pt),
          //   child: WisText(
          //     '￥${money ?? '0.00'}',
          //     color: textColor,
          //     size: 14.pt,
          //   ),
          // )
        ],
      ),
    );
  }
}
