/*
 * @Author       : WuWei
 * @LastEditors: ChenYaJin
 * @Date         : 2023-08-07 11:01:15
 * @LastEditTime: 2023-11-02 18:09:53
 * @Description  : Do not edit
 */
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../../view_models/filter_index_store.dart';

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

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final FilterIndexStore store = Get.find<FilterIndexStore>();

    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: FilterView(),
      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(),
        ],
      ),
    );
  }
}

class FilterView extends StatefulWidget {
  FilterView({Key? key}) : super(key: key);

  @override
  _FilterViewState createState() => _FilterViewState();
}

class _FilterViewState extends State<FilterView>
    with SingleTickerProviderStateMixin {
  final FilterIndexStore store = Get.find<FilterIndexStore>();

  @override
  void initState() {
    // store.onInitData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return 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: [
              WFilterButtons(
                name: '业务行为',
                child: Obx(
                  () => Wrap(
                    spacing: 15.pt,
                    runSpacing: 12.pt,
                    alignment: WrapAlignment.start,
                    children: [
                      for (var item in store.behaviorType) ...[
                        WButton(
                          onPressed: () => store.onChooseBehavior(item['code']),
                          color: Color.fromRGBO(246, 247, 252, 1),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(4.pt),
                            side: BorderSide(
                              width: 1,
                              color: store.currentBehaviorType.value ==
                                      item['code']
                                  ? colorScheme.primary.withOpacity(0.5)
                                  : colorScheme.background,
                              style: BorderStyle.solid,
                            ),
                          ),
                          child: Wisdom.row(
                            width: 60.pt,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              if (item['icon'] != '')
                                Image.asset(
                                  '${item['icon']}',
                                  height: 18.pt,
                                  width: 18.pt,
                                  package: 'pay_package',
                                ),
                              SizedBox(width: 5.pt),
                              Text(
                                item['name'] ?? '',
                                style: TextStyle(
                                  fontSize: 13.pt,
                                  color: store.currentBehaviorType.value ==
                                          item['code']
                                      ? colorScheme.primary
                                      : colorScheme.minor,
                                ),
                              ),
                            ],
                          ),
                        )
                      ],
                    ],
                  ),
                ),
              ),
              SizedBox(height: 10.pt),
              WFilterButtons(
                name: '支付时间范围',
                child: Obx(
                  () => Wrap(
                    spacing: 15.pt,
                    runSpacing: 12.pt,
                    alignment: WrapAlignment.start,
                    children: [
                      Wisdom.row(
                        children: [
                          Wisdom(
                            color: Color.fromRGBO(246, 247, 252, 1),
                            padding: EdgeInsets.only(right: 5.pt, left: 5.pt),
                            borderRadius: BorderRadius.circular(4.pt),
                            child: WFDatePicker(
                              initialDateTime: store.payBillAtStart.value == ''
                                  ? null
                                  : store.payBillAtStart.value,
                              lastDate: store.payBillAtEnd.value == ''
                                  ? null
                                  : store.payBillAtEnd.value,
                              onChanged: (v) => store.onPickBillStartDate(v),
                              hintText: '开始时间: 不限',
                            ),
                          ).asExpanded(),
                          SizedBox(width: 10.pt),
                          WisText('至'),
                          SizedBox(width: 10.pt),
                          Wisdom(
                            color: Color.fromRGBO(246, 247, 252, 1),
                            padding: EdgeInsets.only(right: 5.pt, left: 5.pt),
                            borderRadius: BorderRadius.circular(4.pt),
                            child: WFDatePicker(
                              initialDateTime: store.payBillAtEnd.value == ''
                                  ? null
                                  : store.payBillAtEnd.value,
                              firstDate: store.payBillAtStart.value == ''
                                  ? null
                                  : store.payBillAtStart.value,
                              onChanged: (v) => store.onPickBillEndDate(v),
                              hintText: '结束时间: 不限',
                            ),
                          ).asExpanded(),
                        ],
                      )
                    ],
                  ),
                ),
              ),
              SizedBox(height: 10.pt),
              WFilterButtons(
                name: '收支方式',
                child: Wrap(
                  spacing: 15.pt,
                  runSpacing: 12.pt,
                  alignment: WrapAlignment.start,
                  children: [
                    Wisdom.row(
                      color: Color.fromRGBO(246, 247, 252, 1),
                      crossAxisAlignment: CrossAxisAlignment.start,
                      padding: EdgeInsets.symmetric(vertical: 15.pt),
                      children: [
                        Obx(
                          () => Wisdom.column(
                            padding: EdgeInsets.symmetric(horizontal: 10.pt),
                            children: [
                              for (var item in store.payWayListFirst)
                                Wisdom.row(
                                  onTap: () => store.onSetShowList(item),
                                  margin: EdgeInsets.only(bottom: 8.pt),
                                  children: [
                                    Obx(
                                      () => Wisdom.row(
                                        children: [
                                          if (item['status'] == 'half')
                                            Wisdom(
                                              onTap: () =>
                                                  store.onChangePayFirst(item),
                                              color: Colors.white,
                                              margin:
                                                  EdgeInsets.only(right: 8.pt),
                                              child: Image.asset(
                                                'lib/assets/checkbox-half.png',
                                                width: 16.pt,
                                                height: 16.pt,
                                                package: 'pay_package',
                                              ),
                                            )
                                          else if (!store.payWayCodesFirst
                                              .contains(item['code']))
                                            Wisdom(
                                              onTap: () =>
                                                  store.onChangePayFirst(item),
                                              color: Colors.white,
                                              margin:
                                                  EdgeInsets.only(right: 8.pt),
                                              child: Image.asset(
                                                'lib/assets/checkbox-normal.png',
                                                width: 16.pt,
                                                height: 16.pt,
                                                package: 'pay_package',
                                              ),
                                            )
                                          else
                                            Wisdom(
                                              onTap: () =>
                                                  store.onChangePayFirst(item),
                                              color: Colors.white,
                                              margin:
                                                  EdgeInsets.only(right: 8.pt),
                                              child: Image.asset(
                                                'lib/assets/checkbox-select.png',
                                                width: 16.pt,
                                                height: 16.pt,
                                                package: 'pay_package',
                                              ),
                                            ),
                                          SizedBox(width: 10.pt),
                                          WisText('${item['name']}',
                                              color:
                                                  store.currentShowCode.value ==
                                                          item['code']
                                                      ? colorScheme.primary
                                                      : colorScheme.mainTitle),
                                        ],
                                      ),
                                    ).asExpanded(),
                                    if (item['code'] != 'all')
                                      Icon(Icons.arrow_forward_ios,
                                          size: 13.pt),
                                  ],
                                )
                            ],
                          ),
                        ).asFlexible(flex: 1),
                        Obx(
                          () => Wisdom.column(
                            padding: EdgeInsets.symmetric(horizontal: 10.pt),
                            border: Border(
                                left: BorderSide(color: colorScheme.divider)),
                            children: [
                              for (var item in store.payWayListSecond)
                                Wisdom.row(
                                  onTap: () => store.onChangePaySecond(item),
                                  margin: EdgeInsets.only(bottom: 8.pt),
                                  children: [
                                    if (!store.payWayCodesSecond
                                        .contains(item.code))
                                      Wisdom(
                                        color: Colors.white,
                                        margin: EdgeInsets.only(right: 8.pt),
                                        child: Image.asset(
                                          'lib/assets/checkbox-normal.png',
                                          width: 16.pt,
                                          height: 16.pt,
                                          package: 'pay_package',
                                        ),
                                      )
                                    else
                                      Wisdom(
                                        color: Colors.white,
                                        margin: EdgeInsets.only(right: 8.pt),
                                        child: Image.asset(
                                          'lib/assets/checkbox-select.png',
                                          width: 16.pt,
                                          height: 16.pt,
                                          package: 'pay_package',
                                        ),
                                      ),
                                    SizedBox(width: 10.pt),
                                    WisText('${item.name}')
                                  ],
                                ),
                            ],
                          ),
                        ).asFlexible(flex: 2),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
