/*
 * @Author: WuWei
 * @Date: 2020-08-07 11:19:29
 * @LastEditors: wjc
 * @LastEditTime: 2023-03-29 17:25:41
 * @Description:
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:app_package/src/models/community_picker.dart';
import '../../view_models/private_index_store.dart';
import '../../components/list-item.dart';
part 'components/bill.dart';
part 'components/pre.dart';
part 'components/deposite.dart';
part 'components/summary.dart';

class IndexPage extends StatelessWidget {
  const IndexPage({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 store = Provider.of<PrivateIndexStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        title: WisText("${store.routingArgs['appBarTitle'] ?? '缴费记录'}",
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        actions: <Widget>[
          if (store.tabNum != 3) ...[
            Wisdom(
              padding: EdgeInsets.only(right: 15.pt, left: 15.pt),
              onTap: () => store.onSetFilterShow(),
              child: Image.asset(
                'lib/assets/shx.png',
                height: 15.pt,
                width: 15.pt,
                package: 'pay_package',
                color: colorScheme.mainTitle,
              ),
            )
          ],
        ],
      ),
      body: Stack(fit: StackFit.expand, children: [
        WisTabPage(
          divider: false,
          initialIndex: store.argtab ?? store.tabNum,
          onChange: (index) {
            store.setTab(index);
            FocusScope.of(context).requestFocus(FocusNode());
          },
          tabs: [
            Text('账单缴费'),
            Text('预存款缴费'),
            Text('押金缴费'),
            Text('收款统计'),
          ],
          views: [
            _BuildBillList(),
            _BuildPreList(),
            _BuildDepositeList(),
            _BuildSummaryList(),
          ],
        ),
      ]),
    );
  }
}

class _BuildBillBLock extends StatelessWidget {
  const _BuildBillBLock(
      {Key? key, this.list, this.name, this.total, this.isBill = false})
      : super(key: key);
  final list;
  final name;
  final total;
  final bool isBill;
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateIndexStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;
    return Wisdom.column(
      margin: EdgeInsets.only(top: 10.pt),
      padding: EdgeInsets.symmetric(vertical: 20.pt),
      color: Colors.white,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Wisdom.column(
          crossAxisAlignment: CrossAxisAlignment.center,
          padding: EdgeInsets.only(bottom: 20.pt),
          margin: EdgeInsets.only(bottom: 20.pt, left: 15.pt, right: 15.pt),
          border: Border(
              bottom: BorderSide(
            width: 0.5.pt,
            color: colorScheme.divider,
          )),
          children: [
            WisText(
              '$name',
              size: 16.pt,
              weight: FontWeight.w600,
              color: colorScheme.mainTitle,
            ),
            SizedBox(height: 5.pt),
            WisText(
              '${store.payAtStartDate ?? '--'}至${store.payAtEndDate ?? '--'}',
              color: colorScheme.placeholder,
              size: 14.pt,
              weight: FontWeight.w300,
            ),
          ],
        ),
        for (int i = 0; i < list.length; i++)
          _BuildBillItem(
            item: list[i],
            index: i,
          ),
        Wisdom.row(
          border: Border(
              top: BorderSide(
            width: 0.5.pt,
            color: colorScheme.divider,
          )),
          margin: EdgeInsets.only(top: 15.pt),
          padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 15.pt),
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            WisText(
              '总计:',
              size: 16.pt,
              weight: FontWeight.w600,
              color: colorScheme.mainTitle,
            ),
            WisText(
              '￥${total ?? 0}',
              size: 16.pt,
              weight: FontWeight.w600,
              color: colorScheme.primary,
            ),
          ],
        ),
        if (isBill == true &&
            store.summary != null &&
            store.summary?.billPayWayCount != null &&
            store.summary!.billPayWayCount!.length > 0) ...[
          for (int i = 0; i < store.summary!.billPayWayCount!.length; i++)
            Wisdom.row(
              margin: EdgeInsets.only(bottom: 15.pt),
              padding: EdgeInsets.symmetric(
                horizontal: 15.pt,
              ),
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                WisText(
                  '${store.summary!.billPayWayCount![i].payWay['name']}:',
                  size: 15.pt,
                  weight: FontWeight.w300,
                  color: colorScheme.mainTitle,
                ),
                WisText(
                  '￥${store.summary?.billPayWayCount![i].payFee ?? '0'}(${store.summary?.billPayWayCount![i].feeRate ?? '0'}%)',
                  size: 15.pt,
                  weight: FontWeight.w300,
                  color: colorScheme.green,
                ),
              ],
            ),
        ]
      ],
    );
  }
}

class _BuildBillItem extends StatelessWidget {
  const _BuildBillItem({Key? key, this.item, this.index}) : super(key: key);
  final item;
  final index;
  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Wisdom.row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      padding: EdgeInsets.symmetric(horizontal: 15.pt),
      margin: EdgeInsets.only(top: index == 0 ? 0 : 15.pt),
      children: [
        WisText(
          item?.name,
          size: 15.pt,
          weight: FontWeight.w300,
          color: colorScheme.mainTitle,
        ),
        WisText(
          '￥${item?.fee ?? '0'}',
          size: 15.pt,
          weight: FontWeight.w300,
          color: colorScheme.mainTitle,
        ),
      ],
    );
  }
}
