/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2023-08-07 11:01:15
 * @LastEditTime : 2023-08-14 15:15:11
 * @Description  : Do not edit
 */
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';

class FilterPage extends StatelessWidget {
  const FilterPage({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
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<PrivateIndexStore>(context, listen: true);

    return Scaffold(
      backgroundColor: Color(0xFFF8F8F8),
      appBar: WAppBar(
        title: WisText(
          '筛选',
          color: colorScheme.mainTitle,
          weight: FontWeight.w600,
          size: 18.pt,
        ),
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: IconButton(
          onPressed: () {
            store.back();
          },
          icon: Icon(
            Icons.close,
            color: colorScheme.mainTitle,
            size: 25.pt,
          ),
        ),
      ),
      body: WisScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Wisdom.column(
              color: Colors.white,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              padding: EdgeInsets.only(
                top: 5.pt,
                left: 15.pt,
                right: 15.pt,
                bottom: 15.pt,
              ),
              children: [
                Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  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,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 10.pt),
                WFilterButtons(
                  name: '支付方式',
                  child: Wrap(
                    spacing: 15.pt,
                    runSpacing: 12.pt,
                    alignment: WrapAlignment.start,
                    children: [
                      for (var i = 0; i < store.payWayList.length; i++) ...[
                        WButton(
                          onPressed: () =>
                              store.onChoosePayWay(store.payWayList[i]['code']),
                          color: Color.fromRGBO(246, 247, 252, 1),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(4.pt),
                            side: BorderSide(
                              width: 1,
                              color: store.payWay == store.payWayList[i]['code']
                                  ? colorScheme.primary.withOpacity(0.5)
                                  : colorScheme.background,
                              style: BorderStyle.solid,
                            ),
                          ),
                          child: Text(
                            store.payWayList[i]['name'] ?? '',
                            style: TextStyle(
                              fontSize: 13.pt,
                              color: store.payWay == store.payWayList[i]['code']
                                  ? colorScheme.primary
                                  : colorScheme.minor,
                            ),
                          ),
                        )
                      ],
                    ],
                  ),
                ),
                SizedBox(height: 10.pt),
                WFDatePicker(
                  initialDateTime: store.payBillAtStart ?? null,
                  lastDate: store.payBillAtEnd,
                  onChanged: (v) => store.onPickBillStartDate(v),
                  title: Text.rich(
                    TextSpan(
                      children: [
                        WidgetSpan(
                          child: Wisdom(
                            margin: EdgeInsets.only(right: 10.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: '选择起始时间',
                ),
                SizedBox(height: 10.pt),
                WFDatePicker(
                  initialDateTime: store.payBillAtEnd ?? null,
                  firstDate: store.payBillAtStart,
                  onChanged: (v) => store.onPickBillEndDate(v),
                  title: Text.rich(
                    TextSpan(
                      children: [
                        WidgetSpan(
                          child: Wisdom(
                            margin: EdgeInsets.only(right: 10.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: '选择结束时间',
                ),
              ],
            ),
          ),
        ],
      ),
      bottomNavigationBar: Wisdom.row(
        padding: EdgeInsets.symmetric(horizontal: 15.pt),
        color: Colors.white,
        children: [
          SafeArea(
            child: WLoadingButton(
              color: Colors.white,
              height: 44.pt,
              minWidth: 120.pt,
              margin: EdgeInsets.symmetric(vertical: 8.pt),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(25.pt),
                side: BorderSide(
                  width: 1,
                  color: colorScheme.primary.withOpacity(0.5),
                  style: BorderStyle.solid,
                ),
              ),
              onPressed: () => store.onResetInner(),
              child: WisText('重置', color: colorScheme.primary),
            ),
          ),
          SafeArea(
            child: WLoadingButton(
              height: 44.pt,
              margin: EdgeInsets.only(top: 8.pt, bottom: 8.pt, left: 10.pt),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(25.pt),
              ),
              onPressed: () => store.submit(),
              child: Text("确认"),
            ),
          ).asExpanded(),
        ],
      ),
    );
  }
}
