/*
 * @Author       : WuWei
 * @LastEditors: wuwei
 * @Date         : 2023-03-23 17:11:18
 * @LastEditTime: 2024-02-06 14:35:59
 * @Description  : Do not edit
 */

part of 'index.dart';

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 colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<_Store>(context, listen: true);
    final userphone = store.appGlobalStore.currentUserinfo?.phone ?? '';
    final phone = userphone.substring(userphone.length - 4, userphone.length);
    final uname = store.appGlobalStore.currentUserinfo?.name;

    /// 判断是否为整数
    bool isInteger(num value) => value is int || value == value.roundToDouble();

    /// 费用项显示
    _BuildFeeItem(fee, String name, Widget? tip) {
      return Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.center,
        // padding: EdgeInsets.symmetric(
        //   horizontal: 5.pt,
        // ),
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          WisText(
            name,
            size: 12.pt,
            color: colorScheme.minor,
          ),
          tip ?? Container(),
          SizedBox(height: 5.pt),
          WisText(
            '￥${fee ?? 0}',
            size: 14.pt,
            color: colorScheme.mainTitle,
            bold: true,
          ),
        ],
      ).asFlexible(flex: 1);
    }

    bool isPre(monthYear) =>
        (int.parse(monthYear.split('-')[0]) > DateTime.now().year) ||
        (int.parse(monthYear.split('-')[0]) == DateTime.now().year &&
            int.parse(monthYear.split('-')[1]) > DateTime.now().month);

    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText(
          '${store.args['appBarTitle'] ?? '账单信息'}',
          size: 19.pt,
          weight: FontWeight.w600,
          color: colorScheme.mainTitle,
        ),
        actions: [
          // Wisdom(
          //   padding: EdgeInsets.only(
          //     right: 15.pt,
          //     left: 5.pt,
          //   ),
          //   onTap: () => store.toFilter(),
          //   child: Image.asset(
          //     'lib/assets/shx.png',
          //     height: 15.pt,
          //     width: 15.pt,
          //     package: 'dynamic_package',
          //     color: colorScheme.mainTitle,
          //   ),
          // ).asFlexible(flex: 1),
        ],
      ),
      body: WisTabPage(
        tabs: [
          WisText('欠费账单'),
          WisText('全部账单'),
        ],
        views: [
          WisScrollView(
            key: store.scorllViewKey,
            onInit: () => store.getDetail('arrears'),
            onRefresh: () => store.getDetail('arrears'),
            slivers: <Widget>[
              if (store.oweCount?.arrearsFee != null &&
                  store.oweCount!.arrearsFee! > 0) ...[
                _TotalFeeCard(),
              ],
              Wisdom(
                padding: EdgeInsets.all(15.pt),
                child: ExpansionTileGroup(
                  spaceBetweenItem: 10.pt,
                  toggleType: ToggleType.expandOnlyCurrent,
                  onExpansionItemChanged: (index, isExpanded) => {
                    if (isExpanded)
                      {
                        store.chooseMonthByArrears(
                            store.oweFeeListByMonth[index])
                      }
                    else
                      {store.clearChooseMonth()}
                  },
                  children: [
                    for (var item in store.oweFeeListByMonth)
                      ExpansionTileItem(
                        backgroundColor: Colors.white,
                        collapsedBackgroundColor: Colors.white,
                        isHasTopBorder: false,
                        isHasBottomBorder: false,
                        iconColor: colorScheme.green,
                        textColor: colorScheme.green,
                        borderRadius: BorderRadius.circular(12.pt),
                        initiallyExpanded:
                            item.chargeYearMonth == store.chooseMonth,
                        collapsedTextColor: colorScheme.grey,
                        title: Wisdom.row(
                          height: 44.pt,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Wisdom.row(
                              children: [
                                Icon(
                                  Icons.calendar_month_outlined,
                                  size: 18.pt,
                                ),
                                SizedBox(
                                  width: 10.pt,
                                ),
                                WisText(
                                  '${item.chargeYearMonth}',
                                  color: (item.amount ?? 0) > 0
                                      ? isPre(item.chargeYearMonth)
                                          ? colorScheme.orange
                                          : colorScheme.red
                                      : colorScheme.green,
                                  size: 15.pt,
                                ),
                                WisText(
                                  '${isPre(item.chargeYearMonth) ? '(预缴:${item.amount})' : '(欠费:${item.amount})'}',
                                  size: 13.pt,
                                  color: isPre(item.chargeYearMonth)
                                      ? colorScheme.orange
                                      : colorScheme.red,
                                ),
                              ],
                            ),
                            Wisdom.row(
                              children: [
                                WisText(
                                  item.chargeYearMonth == store.chooseMonth
                                      ? '收起明细'
                                      : '展开明细',
                                  size: 14.pt,
                                  weight: FontWeight.w400,
                                  color:
                                      item.chargeYearMonth == store.chooseMonth
                                          ? colorScheme.green
                                          : colorScheme.grey,
                                ),
                              ],
                            ),
                          ],
                        ),
                        children: [
                          Wisdom(
                            height: 0.3.pt,
                            color: colorScheme.divider2,
                            margin: EdgeInsets.only(bottom: 10.pt),
                          ),
                          Column(
                            children: [
                              Row(
                                children: [
                                  _BuildFeeItem(
                                      isInteger(
                                              store.detail?.receivableFee ?? 0)
                                          ? store.detail?.receivableFee?.round()
                                          : store.detail?.receivableFee,
                                      '应缴',
                                      null),
                                  Wisdom(
                                    width: 1.pt,
                                    height: 25.pt,
                                    color: colorScheme.divider,
                                  ),
                                  _BuildFeeItem(
                                      isInteger(store.detail?.receivedFee ?? 0)
                                          ? store.detail?.receivedFee?.round()
                                          : store.detail?.receivedFee,
                                      '已缴',
                                      null),
                                  Wisdom(
                                    width: 1.pt,
                                    height: 25.pt,
                                    color: colorScheme.divider,
                                  ),
                                  _BuildFeeItem(
                                      isInteger(store.detail?.arrearsFee ?? 0)
                                          ? store.detail?.arrearsFee?.round()
                                          : store.detail?.arrearsFee,
                                      '欠费',
                                      null),
                                ],
                              )
                            ],
                          ),
                          Wisdom(
                            padding: EdgeInsets.symmetric(horizontal: 15.pt),
                            child: Wisdom(
                              height: 0.3.pt,
                              color: colorScheme.divider2,
                              margin: EdgeInsets.only(top: 10.pt),
                            ),
                          ),
                          _ListComponent(detail: store.detail),
                        ],
                      ),
                  ],
                ),
              ).asSliver(),
              if (store.oweFeeListByMonth.length > 0) ...[
                Wisdom.row(
                  padding: EdgeInsets.only(top: 5.pt, bottom: 10.pt),
                  children: <Widget>[
                    Expanded(
                      child: Container(
                        height: 1,
                        color: Color(0xffdddddd),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 20),
                      child: Text(
                        '暂无更多内容',
                        style:
                            TextStyle(color: Color(0xffaaaaaa), fontSize: 12),
                      ),
                    ),
                    Expanded(
                      child: Container(
                        height: 1,
                        color: Color(0xffdddddd),
                      ),
                    ),
                  ],
                ).asSliver(),
              ],
              SizedBox(
                height: 15.pt,
              ).asSliver(),
            ],
          ),
          _AllListPage(),
        ],
        indicator: RoundUnderlineTabIndicator(
          width: 25.pt,
        ),
      ).addWatermark(
        text: '${uname} ${phone}',
        color: Colors.black.withOpacity(0.05),
      ),
      // bottomNavigationBar: _BottomComponent(),
    );
  }
}

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

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

    /// 判断是否为整数
    bool isInteger(num value) => value is int || value == value.roundToDouble();

    /// 费用项显示
    _BuildFeeItem(fee, String name, Widget? tip) {
      return Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.center,
        // padding: EdgeInsets.symmetric(
        //   horizontal: 5.pt,
        // ),
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          WisText(
            name,
            size: 12.pt,
            color: colorScheme.minor,
          ),
          tip ?? Container(),
          SizedBox(height: 5.pt),
          WisText(
            '￥${fee ?? 0}',
            size: 14.pt,
            color: colorScheme.mainTitle,
            bold: true,
          ),
        ],
      ).asFlexible(flex: 1);
    }

    bool isPre(monthYear) =>
        (int.parse(monthYear.split('-')[0]) > DateTime.now().year) ||
        (int.parse(monthYear.split('-')[0]) == DateTime.now().year &&
            int.parse(monthYear.split('-')[1]) > DateTime.now().month);
    return WisScrollView(
      key: store.scorllViewKey1,
      onInit: () => store.getOweFeeMonthListByAll(isLoad: false),
      onRefresh: () => store.reloadAllList(),
      onLoad: () => store.getOweFeeMonthListByAll(isLoad: true),
      slivers: <Widget>[
        Wisdom(
          padding: EdgeInsets.all(15.pt),
          child: ExpansionTileGroup(
            spaceBetweenItem: 10.pt,
            toggleType: ToggleType.expandOnlyCurrent,
            onExpansionItemChanged: (index, isExpanded) => {
              if (isExpanded)
                {store.chooseMonthByAllList(store.oweFeeListByAll[index])}
              else
                {store.clearChooseMonthAll()}
            },
            children: [
              for (var item in store.oweFeeListByAll)
                ExpansionTileItem(
                  backgroundColor: Colors.white,
                  collapsedBackgroundColor: Colors.white,
                  isHasTopBorder: false,
                  isHasBottomBorder: false,
                  iconColor: colorScheme.green,
                  textColor: colorScheme.green,
                  collapsedTextColor: colorScheme.grey,
                  borderRadius: BorderRadius.circular(12.pt),
                  initiallyExpanded:
                      item.chargeYearMonth == store.chooseMonthAll,
                  title: Wisdom.row(
                    height: 44.pt,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Wisdom.row(
                        children: [
                          Icon(
                            Icons.calendar_month_outlined,
                            size: 18.pt,
                          ),
                          SizedBox(
                            width: 10.pt,
                          ),
                          WisText(
                            '${item.chargeYearMonth}',
                            size: 15.pt,
                            color: (item.amount ?? 0) > 0
                                ? isPre(item.chargeYearMonth)
                                    ? colorScheme.orange
                                    : colorScheme.red
                                : colorScheme.green,
                          ),
                          SizedBox(width: 10.pt),
                          WisText(
                            '${(item.amount ?? 0) > 0 ? isPre(item.chargeYearMonth) ? '(预缴:${item.amount})' : '(欠费:${item.amount})' : ''}',
                            size: 13.pt,
                            color: (item.amount ?? 0) > 0
                                ? isPre(item.chargeYearMonth)
                                    ? colorScheme.orange
                                    : colorScheme.red
                                : colorScheme.green,
                          ),
                        ],
                      ),
                      Wisdom.row(
                        children: [
                          WisText(
                            item.chargeYearMonth == store.chooseMonthAll
                                ? '收起明细'
                                : '展开明细',
                            size: 14.pt,
                            weight: FontWeight.w400,
                            color: item.chargeYearMonth == store.chooseMonthAll
                                ? colorScheme.green
                                : colorScheme.grey,
                          ),
                        ],
                      ),
                    ],
                  ),
                  children: [
                    Wisdom(
                      height: 0.3.pt,
                      color: colorScheme.divider2,
                      margin: EdgeInsets.only(bottom: 10.pt),
                    ),
                    Column(
                      children: [
                        Row(
                          children: [
                            _BuildFeeItem(
                                isInteger(store.detailAll?.receivableFee ?? 0)
                                    ? store.detailAll?.receivableFee?.round()
                                    : store.detailAll?.receivableFee,
                                '应缴',
                                null),
                            Wisdom(
                              width: 1.pt,
                              height: 25.pt,
                              color: colorScheme.divider,
                            ),
                            _BuildFeeItem(
                                isInteger(store.detailAll?.receivedFee ?? 0)
                                    ? store.detailAll?.receivedFee?.round()
                                    : store.detailAll?.receivedFee,
                                '已缴',
                                null),
                            Wisdom(
                              width: 1.pt,
                              height: 25.pt,
                              color: colorScheme.divider,
                            ),
                            _BuildFeeItem(
                                isInteger(store.detailAll?.arrearsFee ?? 0)
                                    ? store.detailAll?.arrearsFee?.round()
                                    : store.detailAll?.arrearsFee,
                                '欠费',
                                null),
                          ],
                        )
                      ],
                    ),
                    Wisdom(
                      child: Wisdom(
                        height: 0.3.pt,
                        color: colorScheme.divider2,
                        margin: EdgeInsets.only(top: 10.pt),
                      ),
                    ),
                    _ListComponent(detail: store.detailAll),
                  ],
                ),
            ],
          ),
        ).asSliver(),
        SizedBox(
          height: 15.pt,
        ).asSliver(),
      ],
    );
  }
}
