/*
 * @Author: WuWei
 * @Date: 2020-08-13 19:30:34
 * @LastEditors: wjc
 * @LastEditTime: 2023-03-30 15:34:46
 * @Description:
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/private_index_store.dart';

// part 'components/list.dart';
class DetailPage extends StatelessWidget {
  const DetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) => PrivateIndexStore(context, providerContext),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<PrivateIndexStore>(context);
    final feeDetail = store.routingArgs;
    final detail = store.payDetail;

     final list = store.payDetail?.list ?? [];

    String calcFeeCount(feeType) {
      num count = 0;
      list.forEach((element) {
        if (element.feeType == feeType) {
          count = count + double.parse(element.fee!);
        }
      });
      return count.toStringAsFixed(2);
    }
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('缴费详情',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WisScrollView(
        onInit: () => store.getDetail(),
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: WisCard(
              margin: EdgeInsets.only(left: 15.pt, right: 15.pt, top: 10.pt),
              color: Colors.white,
              child: Wisdom.column(
                color: Colors.white,
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                padding:
                    EdgeInsets.symmetric(horizontal: 20.pt, vertical: 10.pt),
                margin: EdgeInsets.only(bottom: 15.pt),
                children: <Widget>[
                  Wisdom(
                    margin: EdgeInsets.only(top: 27.pt, bottom: 3.pt),
                    child: WisText(
                      '￥${detail?.totalPayFee ?? '0'}',
                      color: colorScheme.primary,
                      size: 19.pt,
                      weight: FontWeight.w600,
                      textAlign: TextAlign.center,
                    ),
                  ),
                  Wisdom(
                    margin: EdgeInsets.fromLTRB(0, 0, 0, 27.pt),
                    child: WisText(
                      '缴费金额',
                      color: colorScheme.placeholder2,
                      weight: FontWeight.w300,
                      size: 12.pt,
                      textAlign: TextAlign.center,
                    ),
                  ),
                  DottedLine(
                    color: Color(0xffE5E5E5),
                  ),
                  Divider(
                    height: 15.pt,
                    color: Colors.white,
                  ),
                  InfoItem(
                    title: '编号',
                    content: feeDetail['id'] ?? '--',
                  ),
                  InfoItem(
                    title: '缴费人',
                    content: detail?.payer ?? '--',
                  ),
                  InfoItem(
                    title: '缴费时间',
                    content: detail?.payAt ?? '--',
                  ),
                  InfoItem(
                    title: '资产名称',
                    content: detail?.assetName ?? '--',
                  ),
                  InfoItem(
                    title: '支付方式',
                    content: '${detail?.payWay ?? ''} ${detail?.payType ?? ''}',
                  ),
                  InfoItem(
                    title: '操作员',
                    content: detail?.createBy ?? '--',
                  ),
                  InfoItem(
                    title: '备注',
                    content: detail?.remark == null || detail?.remark == ''
                        ? '--'
                        : detail?.remark,
                  ),
                  Divider(
                    height: 15.pt,
                    color: Colors.white,
                  ),
                  DottedLine(
                    color: Color(0xffE5E5E5),
                  ),
                  Divider(
                    height: 15.pt,
                    color: Colors.white,
                  ),
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: detail?.receiptTickets != null &&
                            detail!.receiptTickets!.length > 0
                        ? CrossAxisAlignment.start
                        : CrossAxisAlignment.center,
                    padding: EdgeInsets.only(bottom: 10.pt),
                    children: <Widget>[
                      WisText(
                        '收据',
                        color: colorScheme.minor,
                        size: 15.pt,
                        weight: FontWeight.w300,
                      ),
                      SizedBox(width: 15.pt),
                      Wrap(
                        spacing: 10.pt,
                        runSpacing: 10.pt,
                        children: [
                          if (detail?.receiptTickets != null &&
                              detail!.receiptTickets!.length > 0) ...[
                            for (int i = 0;
                                i < detail.receiptTickets!.length;
                                i++)
                              Wisdom(
                                padding: EdgeInsets.all(
                                  1.5.pt,
                                ),
                                margin: EdgeInsets.only(
                                    right:
                                        i == (detail.receiptTickets!.length - 1)
                                            ? 0
                                            : 10.pt),
                                color: Color(0xffEAF3FD),
                                border: Border.all(
                                    width: 1.pt, color: Color(0xff2D8CF0)),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(2.pt)),
                                child: WisText(
                                    detail.receiptTickets![i].ticketNo!,
                                    color: Color(0xff2D8CF0)),
                              ),
                          ] else ...[
                            WisText(
                              '--',
                              color: colorScheme.minor,
                              size: 15.pt,
                              weight: FontWeight.w300,
                            )
                          ]
                        ],
                      ).asFlexible(flex: 1)
                    ],
                  ),
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: detail?.invoiceTickets != null &&
                            detail!.invoiceTickets!.length > 0
                        ? CrossAxisAlignment.start
                        : CrossAxisAlignment.center,
                    children: <Widget>[
                      WisText(
                        '发票',
                        color: colorScheme.minor,
                        size: 15.pt,
                        weight: FontWeight.w300,
                      ),
                      SizedBox(width: 15.pt),
                      Wrap(
                        // alignment: WrapAlignment.end,
                        spacing: 10.pt,
                        runSpacing: 10.pt,
                        children: [
                          if (detail?.invoiceTickets != null &&
                              detail!.invoiceTickets!.length > 0) ...[
                            for (int i = 0;
                                i < detail.invoiceTickets!.length;
                                i++)
                              Wisdom(
                                padding: EdgeInsets.all(
                                  1.5.pt,
                                ),
                                margin: EdgeInsets.only(
                                    right:
                                        i == (detail.invoiceTickets!.length - 1)
                                            ? 0
                                            : 10.pt),
                                color: detail.invoiceTickets![i].type ==
                                        'electronic'
                                    ? Color(0xffEAF3FD)
                                    : detail.invoiceTickets![i].type ==
                                            'redSpecial'
                                        ? Color(0xffFEEDEC)
                                        : colorScheme.background3,
                                border: Border.all(
                                  width: 1.pt,
                                  color: detail.invoiceTickets![i].type ==
                                          'electronic'
                                      ? Color(0xff2D8CF0)
                                      : detail.invoiceTickets![i].type ==
                                              'redSpecial'
                                          ? Color(0xffF94F48)
                                          : colorScheme.minor,
                                ),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(2.pt)),
                                child: WisText(
                                  detail.invoiceTickets![i].ticketNo ?? '--',
                                  color: detail.invoiceTickets![i].type ==
                                          'electronic'
                                      ? Color(0xff2D8CF0)
                                      : detail.invoiceTickets![i].type ==
                                              'redSpecial'
                                          ? Color(0xffF94F48)
                                          : colorScheme.minor,
                                ),
                              ),
                          ] else ...[
                            WisText(
                              '--',
                              color: colorScheme.minor,
                              size: 15.pt,
                              weight: FontWeight.w300,
                            )
                          ]
                        ],
                      ).asFlexible(flex: 1),
                    ],
                  ),
                ],
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: WisCard(
                margin:
                    EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
                color: Colors.white,
                child: Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  padding:
                      EdgeInsets.symmetric(horizontal: 20.pt, vertical: 25.pt),
                  color: Colors.white,
                  children: [
                    Wisdom.row(children: [
                      WisText(
                        '费用小计',
                        size: 17.pt,
                        weight: FontWeight.w600,
                        color: colorScheme.mainTitle,
                      ),
                    ]),
  Divider(
                      color: colorScheme.placeholder2,
                      height: 25.pt,
                    ),
                      /// 将相同的收费项目名称的进行账期合并
                    for (int j = 0; j < store.feeTypes.length; j++)
                      Wisdom.column(
                        margin: EdgeInsets.only(
                          top: 10.pt,
                        ),
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Wisdom.row(
                            margin: EdgeInsets.only(left: 10.pt, right: 10.pt),
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              WisText(
                                store.feeTypes[j],
                                size: 16.pt,
                                bold: true,
                                color: colorScheme.mainTitle,
                              ),
                              WisText(
                                '￥${calcFeeCount(store.feeTypes[j])}',
                                size: 16.pt,
                                bold: true,
                                color: colorScheme.mainTitle,
                              ),
                            ],
                          ),
                          Wisdom.column(
                            color: Color.fromRGBO(246, 247, 252, 1),
                            margin: EdgeInsets.symmetric(vertical: 10.pt),
                            padding: EdgeInsets.symmetric(horizontal: 10.pt),
                            borderRadius:
                                BorderRadius.all(Radius.circular(8.pt)),
                            children: [
                              if (detail?.list != null &&
                                  detail!.list!.length > 0) ...[
                                for (int i = 0; i < detail.list!.length; i++)
                                  Column(children: [
                                    if (detail.list![i].feeType ==
                                        store.feeTypes[j]) ...[
                                      InfoItem2(
                                        title:
                                            '${detail.list![i].feeType ?? '--'}',
                                        content: detail.list![i].fee != null
                                            ? '¥ ' +
                                                detail.list![i].fee.toString()
                                            : '0',
                                        chargeYearMonth:
                                            detail.list![i].chargeYearMonth,
                                      ),
                                    ]
                                  ]),
                              ],
                            ],
                          ),
                        ],
                      )
                  ],
                )),
          ),
        ],
      ),
    );
  }
}

class InfoItem extends StatelessWidget {
  const InfoItem({Key? key, this.title, this.content}) : super(key: key);
  final String? title;
  final content;
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Wisdom.row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      padding: EdgeInsets.symmetric(vertical: 10.pt),
      children: <Widget>[
        WisText(
          title!,
          color: colorScheme.minor,
          size: 15.pt,
          weight: FontWeight.w300,
        ),
        SizedBox(width: 15.pt),
        WisText(
          content,
          color: colorScheme.minor,
          size: 15.pt,
          weight: FontWeight.w300,
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        ).asFlexible(flex: 1),
      ],
    );
  }
}

class DottedLine extends StatelessWidget {
  final double height;
  final Color color;
  final Axis direction;

  const DottedLine({
    this.height = 1,
    this.color = Colors.black,
    this.direction = Axis.horizontal,
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        final boxWidth = direction == Axis.horizontal
            ? constraints.constrainWidth()
            : constraints.constrainHeight();
        final dashWidth = 10.0;
        final dashHeight = height;
        final dashCount = (boxWidth / (2 * dashWidth)).floor();
        return Flex(
          children: List.generate(dashCount, (_) {
            return SizedBox(
              width: direction == Axis.horizontal ? dashWidth : dashHeight,
              height: direction == Axis.horizontal ? dashHeight : dashWidth,
              child: DecoratedBox(
                decoration: BoxDecoration(color: color),
              ),
            );
          }),
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          direction: direction,
        );
      },
    );
  }
}


class InfoItem2 extends StatelessWidget {
  const InfoItem2({
    Key? key,
    this.title,
    this.content,
    this.chargeYearMonth,
    this.showTag = false,
    this.tagName = '',
  }) : super(key: key);
  final String? title;
  final content;
  final showTag;
  final tagName;
  final chargeYearMonth;
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Wisdom.row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      padding: EdgeInsets.symmetric(vertical: 10.pt),
      children: <Widget>[
        if (chargeYearMonth != null) ...[
          WisText(
            chargeYearMonth ?? '',
            size: 15.pt,
            color: colorScheme.minor,
          ),
          SizedBox(width: 10.pt),
        ],
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            if (showTag)
              WTag(
                plain: true,
                color: Color(0xffBC68F3),
                border: Border.all(color: Color(0xffBC68F3)),
                backgroundColor: Color(0xffBC68F3).withOpacity(0.1),
                child: WisText(
                  '${tagName}',
                  size: 13.pt,
                ),
              ),
            WisText(
              content,
              color: colorScheme.minor,
              size: 15.pt,
              // weight: FontWeight.bold,
              textAlign: TextAlign.right,
              // overflow: TextOverflow.ellipsis,
              // maxLines: 1,
            ).asExpanded()
          ],
        ).asFlexible(flex: 1),
      ],
    );
  }
}