/*
 * @Author       : WuWei
 * @LastEditors: ChenYaJin
 * @Date         : 2021-11-09 09:53:24
 * @LastEditTime: 2023-09-25 15:20:05
 * @Description  : Do not edit
 */
part of '../index.dart';

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

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

    /// 收款统计
    return WisScrollView(
      key: store.indexScorllViewKey3,
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: WisCard(
            margin: EdgeInsets.only(top: 10.pt, left: 15.pt, right: 15.pt),
            color: Colors.white,
            child: Wisdom.column(children: [
              Wisdom.row(
                padding:
                    EdgeInsets.symmetric(horizontal: 15.pt, vertical: 15.pt),
                children: [
                  WisText(
                    '温馨提示：最大可查询范围不超过一年',
                    color: Color(0xff00BA7B),
                  )
                ],
              ),
              Wisdom.column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                margin: EdgeInsets.symmetric(horizontal: 15.pt),
                children: [
                  Text(
                    '小区选择',
                    style: TextStyle(
                      fontSize: 16.pt,
                      fontWeight: FontWeight.bold,
                    ),
                  ).asPaddingSymmetric(vertical: 8.pt),
                  ButtonTheme(
                    height: 40.pt,
                    minWidth: MediaQuery.of(context).size.width,
                    textTheme: ButtonTextTheme.accent,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15.pt),
                    ),
                    child: Wrap(
                      alignment: WrapAlignment.start,
                      children: [
                        WButton(
                          onPressed: () async {
                            final res =
                                await WPickerUtil.single<CommunityPickerModel>(
                              context,
                              title: Text('选择小区'),
                              initialValue: store.communityList,
                              getData: () => store.getAsyncCommunityList(),
                              isSearch: true,
                            );
                            if (res != null) {
                              store.onChooseCommunity(res);
                            }
                          },
                          color: store.communityId != null
                              ? colorScheme.primary
                              : colorScheme.background,
                          child: Text(
                            store.communityId != null
                                ? "${store.communityIName}"
                                : '请选择小区',
                            style: TextStyle(
                              fontSize: 14.pt,
                              color: store.communityId != null
                                  ? Colors.white
                                  : colorScheme.minor,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  margin: EdgeInsets.symmetric(horizontal: 15.pt),
                  children: [
                    Text(
                      '时间范围',
                      style: TextStyle(
                        fontSize: 16.pt,
                        fontWeight: FontWeight.bold,
                      ),
                    ).asPaddingOnly(top: 8.pt),
                  ]),
              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                color: Colors.white,
                padding: EdgeInsets.all(15.pt),
                border: Border(
                  bottom: BorderSide(
                    width: 0.5.pt,
                    color: colorScheme.divider,
                  ),
                ),
                children: [
                  _BuildFilterItemButton(
                      title: '今日',
                      chooseKey: 'today',
                      onChoose: () => store.onChooseDateButton('today')),
                  _BuildFilterItemButton(
                      title: '昨日',
                      chooseKey: 'yesterday',
                      onChoose: () => store.onChooseDateButton('yesterday')),
                  _BuildFilterItemButton(
                      title: '近七日',
                      chooseKey: 'week',
                      onChoose: () => store.onChooseDateButton('week')),
                  _BuildFilterItemButton(
                      title: '本月',
                      chooseKey: 'thismonth',
                      onChoose: () => store.onChooseDateButton('thismonth')),
                  _BuildFilterItemButton(
                      title: '上月',
                      chooseKey: 'lastmonth',
                      onChoose: () => store.onChooseDateButton('lastmonth')),
                ],
              ),
              Wisdom.column(
                  padding: EdgeInsets.symmetric(
                    horizontal: 15.pt,
                    vertical: 0,
                  ),
                  border: Border(
                    bottom: BorderSide(
                      width: 0.5.pt,
                      color: colorScheme.divider,
                    ),
                  ),
                  color: Colors.white,
                  children: [
                    WFDatePicker(
                      firstDate: store.payAtStart != null
                          ? DateTime.parse(store.payAtStart!)
                              .add(Duration(days: -365))
                              .toFormat(WisDateTimeFormat.date)
                          : null,
                      lastDate: store.payAtEnd,
                      initialDateTime: store.payAtStart,
                      onChanged: (v) => store.onPickStartDate(v),
                      title: Text.rich(
                        TextSpan(
                          children: [
                            WidgetSpan(
                              child: Wisdom(
                                margin:
                                    EdgeInsets.only(right: 10.pt, bottom: 2.pt),
                                child: Image.asset(
                                  'lib/assets/rl.png',
                                  width: 17.pt,
                                  height: 14.pt,
                                  package: 'pay_package',
                                ),
                              ),
                            ),
                            TextSpan(
                              text: '起始时间',
                              style: TextStyle(
                                color: colorScheme.mainTitle,
                                fontSize: 15.pt,
                                fontWeight: FontWeight.w300,
                              ),
                            ),
                          ],
                        ),
                      ),
                      hintText: '选择起始时间',
                    ),
                    WFDatePicker(
                      firstDate: store.payAtStart,
                      lastDate: store.payAtStart != null
                          ? DateTime.parse(store.payAtStart!)
                              .add(Duration(days: 365))
                              .toFormat(WisDateTimeFormat.date)
                          : null,
                      initialDateTime: store.payAtEnd,
                      onChanged: (v) => store.onPickEndDate(v),
                      title: Text.rich(
                        TextSpan(
                          children: [
                            WidgetSpan(
                              child: Wisdom(
                                margin: EdgeInsets.only(
                                  right: 10.pt,
                                  bottom: 2.pt,
                                ),
                                child: Image.asset(
                                  'lib/assets/rl.png',
                                  width: 17.pt,
                                  height: 14.pt,
                                  package: 'pay_package',
                                ),
                              ),
                            ),
                            TextSpan(
                              text: '结束时间',
                              style: TextStyle(
                                color: colorScheme.mainTitle,
                                fontSize: 15.pt,
                                fontWeight: FontWeight.w300,
                              ),
                            ),
                          ],
                        ),
                      ),
                      hintText: '选择结束时间',
                    ),
                  ]),
              Wisdom.row(
                color: Colors.white,
                children: [
                  WButton(
                    color: colorScheme.primary,
                    margin: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 15.pt),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(25.pt),
                    ),
                    onPressed: () => store.onFilterSummary(),
                    disabledColor: colorScheme.disabled,
                    child: Text('查询'),
                  ).asExpanded(),
                ],
              )
            ]),
          ),
        ),
        if (store.isShowList == true) ...[
          SliverToBoxAdapter(
              child: Wisdom.column(children: [
            if (store.billList != null && store.billList!.length > 0) ...[
              WisCard(
                margin: EdgeInsets.only(
                    left: 15.pt, right: 15.pt, top: 10.pt, bottom: 5.pt),
                color: Colors.white,
                child: _BuildBillBLock(
                  list: store.billList ?? [],
                  name: '${store.communityINameBySubmit}账单实收统计表',
                  total: store.summary?.totalBillFee,
                  isBill: true,
                ),
              ),
            ],
            if (store.preList != null && store.preList!.length > 0) ...[
              WisCard(
                margin: EdgeInsets.only(
                    left: 15.pt, right: 15.pt, top: 5.pt, bottom: 10.pt),
                color: Colors.white,
                child: _BuildBillBLock(
                  list: store.preList ?? [],
                  name: '${store.communityINameBySubmit}预存款收款统计表',
                  total: store.summary?.totalPrePayFee,
                ),
              ),
            ],
            if (store.depositeList != null &&
                store.depositeList!.length > 0) ...[
              WisCard(
                margin: EdgeInsets.only(
                    left: 15.pt, right: 15.pt, top: 5.pt, bottom: 10.pt),
                color: Colors.white,
                child: _BuildBillBLock(
                  list: store.depositeList ?? [],
                  name: '${store.communityINameBySubmit}押金收款统计表',
                  total: store.summary?.totalDepositeFee,
                ),
              )
            ],
            if ((store.billList == null || store.billList!.length == 0) &&
                (store.preList == null || store.preList!.length == 0) &&
                (store.depositeList == null ||
                    store.depositeList!.length == 0)) ...[
              WisCard(
                margin: EdgeInsets.only(
                    left: 15.pt, right: 15.pt, top: 5.pt, bottom: 10.pt),
                color: Colors.white,
                child: WEmptyWidget(text: Text('查询期间无缴费数据')),
              )
            ]
          ])),
        ],
      ],
    );
  }
}

class _BuildFilterItemButton extends StatelessWidget {
  const _BuildFilterItemButton(
      {Key? key, this.title, this.chooseKey, this.onChoose})
      : super(key: key);
  final String? title;
  final String? chooseKey;
  final Function()? onChoose;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<PrivateIndexStore>(context, listen: true);
    return Wisdom(
      onTap: onChoose!,
      alignment: Alignment.center,
      color: store.chooseButton == chooseKey
          ? Color(0xffEAF3FD)
          : colorScheme.background,
      height: 30.pt,
      border: Border.all(
        color: store.chooseButton == chooseKey
            ? colorScheme.primary
            : Colors.transparent,
        width: 0.5,
      ),
      padding: EdgeInsets.symmetric(
        horizontal: 15.pt,
      ),
      borderRadius: BorderRadius.circular(15.pt),
      child: WisText(
        title!,
        size: 14.pt,
        weight: FontWeight.w300,
        color: store.chooseButton == chooseKey
            ? colorScheme.primary
            : colorScheme.mainTitle,
      ),
    );
  }
}
