// ignore_for_file: unnecessary_set_literal

/*
 * @Author: ChenYaJin
 * @Date: 2023-04-11 11:02:29
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-24 17:31:05
 * @Description:  作业统计-筛选
 */

import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:app_package/src/models/community_picker.dart';
import 'package:task_package/src/view_models/work_statistic_store.dart';
import 'package:task_package/src/assets.gen.dart';

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

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

    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: Image(
            image: AssetList.$close_0_png_image,
            width: 25.pt,
          ),
        ),
      ),
      body: FilterView(),
      bottomNavigationBar: Wisdom.row(
        padding: EdgeInsets.symmetric(horizontal: 15.pt),
        color: Colors.white,
        children: [
          if(store.isDate != true) ...[
            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 WorkStatisticStore store = Get.find<WorkStatisticStore>();

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

    _getSelectValue() {
      List _initialValue = store.organIdsPra;
      List<SinglePickerEntity> _selectValue = [];
      if (_initialValue.length == 0) return null;
      List tempList;
      (_initialValue).forEach((item) => {
            tempList = store.organTree.where((v) => v['id'] == item).toList(),
            _selectValue.add(SinglePickerEntity(
              name: tempList[0]['name'],
              unique: tempList[0]['id'],
            )),
          });
      return _selectValue;
    }

    _getCommunitySelectValue() {
      List _initialValue = store.communityIdsPra;
      List<CommunityPickerModel> _selectValue = [];
      if (_initialValue.length == 0) return null;
      List tempList;
      (_initialValue).forEach((item) => {
            tempList = store.communityList.where((v) => v.id == item).toList(),
            _selectValue.add(tempList[0]),
          });
      return _selectValue;
    }

    return WisScrollView(
      onInit: () => store.onGetTaskListTreeItem(),
      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: [
              if (store.isDate) ...[
                Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Text(
                      '任务类型',
                      style: TextStyle(
                        fontSize: 16.pt,
                        fontWeight: FontWeight.bold,
                      ),
                    ).asPaddingSymmetric(vertical: 10.pt),
                    Obx(() => Wisdom.row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(4.pt),
                          height: 240.pt,
                          children: [
                            SingleChildScrollView(
                              key: Key('taskStatFilterInner1'),
                              child: Wisdom.column(
                                width: (MediaQuery.of(context).size.width -
                                        100.pt) /
                                    2,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  for (var i = 0;
                                      i < store.taskItemListTree.length;
                                      i++) ...[
                                    if (store.taskItemListTree[i].children
                                            .length >
                                        0 && store.taskItemListTree[i].type == "preset") ...[
                                      Wisdom.row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        padding:
                                            EdgeInsets.fromLTRB(15.pt, 0, 0, 0),
                                        height: 38.pt,
                                        onTap: () => {
                                          store.changeFirstInventoryValue(
                                              store.taskItemListTree[i].id),
                                          store.setInventorySecondList(store
                                              .taskItemListTree[i].children),
                                          store.setInventoryFirstItem(
                                              store.taskItemListTree[i]),
                                        },
                                        color: store.inventoryFirstId.value ==
                                                store.taskItemListTree[i].id
                                            ? Color.fromRGBO(246, 247, 252, 1)
                                            : Colors.white,
                                        children: [
                                          if (store.inventoryFirstId.value ==
                                              store.taskItemListTree[i].id) ...[
                                            Container(
                                              height: 15.pt,
                                              width: 3.pt,
                                              color: colorScheme.primary,
                                              margin:
                                                  EdgeInsets.only(right: 10.pt),
                                            ),
                                          ],
                                          WisText(
                                            store.taskItemListTree[i].label ??
                                                '',
                                            size: 13.pt,
                                            color: store.inventoryFirstId
                                                        .value ==
                                                    store.taskItemListTree[i].id
                                                ? colorScheme.primary
                                                : colorScheme.minor,
                                            overflow: TextOverflow.ellipsis,
                                          ).asExpanded(),
                                        ],
                                      ),
                                    ]
                                  ],
                                ],
                              ),
                            ),
                            Wisdom(
                              width: 1.pt,
                              color: colorScheme.divider2,
                            ),
                            SingleChildScrollView(
                              key: Key('taskStatFilterInner2'),
                              child: Wisdom.column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  for (var i = 0;
                                      i < store.taskItemSecondList.length;
                                      i++) ...[
                                    Wisdom.row(
                                      padding: EdgeInsets.fromLTRB(
                                          15.pt, 0, 15.pt, 0),
                                      height: 38.pt,
                                      onTap: () => {
                                        store.changeSecondInventoryValue(
                                            store.taskItemSecondList[i].id),
                                      },
                                      color: store.taskItemSecondList[i].id ==
                                              store.statusCode
                                          ? Color.fromRGBO(246, 247, 252, 1)
                                          : Colors.white,
                                      children: [
                                        if (store.taskItemSecondList[i].id ==
                                            store.statusCode.value) ...[
                                          Image(
                                            image: AssetList.$dxy_png_image,
                                            width: 15.pt,
                                            height: 15.pt,
                                          ),
                                        ] else ...[
                                          Image(
                                            image: AssetList.$dxH_png_image,
                                            width: 15.pt,
                                            height: 15.pt,
                                          ),
                                        ],
                                        SizedBox(width: 10.pt,),
                                        WisText(
                                          store.taskItemSecondList[i].label ??
                                              '',
                                          size: 13.pt,
                                          color:
                                              store.taskItemSecondList[i].id ==
                                                      store.statusCode.value
                                                  ? colorScheme.primary
                                                  : colorScheme.minor,
                                          overflow: TextOverflow.ellipsis,
                                        ).asExpanded(),
                                      ],
                                    ),
                                  ],
                                ],
                              ),
                            ).asExpanded(),
                          ],
                        )),
                  ],
                ),
                //    Wisdom.column(
                //   crossAxisAlignment: CrossAxisAlignment.stretch,
                //   children: [
                //     Text(
                //       '类型筛选',
                //       style: TextStyle(
                //         fontSize: 16.pt,
                //         fontWeight: FontWeight.bold,
                //       ),
                //     ).asPaddingSymmetric(vertical: 8.pt),
                //     ButtonTheme(
                //       minWidth: 100.pt,
                //       height: 39.pt,
                //       textTheme: ButtonTextTheme.accent,
                //       shape: RoundedRectangleBorder(
                //         borderRadius: BorderRadius.circular(4.pt),
                //       ),
                //       child: Obx(
                //         () => Wrap(
                //           spacing: 15.pt,
                //           runSpacing: 15.pt,
                //           alignment: WrapAlignment.start,
                //           children: [
                //             for (var item in store.taskItemList.reversed) ...[
                //               WButton(
                //                 onPressed: () =>
                //                     store.onSelectInventor(item.id!),
                //                 color: Color.fromRGBO(246, 247, 252, 1),
                //                 shape: RoundedRectangleBorder(
                //                   borderRadius: BorderRadius.circular(4.pt),
                //                   side: BorderSide(
                //                     width: 1,
                //                     color: store.statusCode.value == item.id
                //                         ? colorScheme.primary.withOpacity(0.5)
                //                         : colorScheme.background,
                //                     style: BorderStyle.solid,
                //                   ),
                //                 ),
                //                 child: Text(
                //                   item.name ?? '',
                //                   style: TextStyle(
                //                     fontSize: 14.pt,
                //                     color: store.statusCode.value == item.id
                //                         ? colorScheme.primary
                //                         : colorScheme.minor,
                //                   ),
                //                 ),
                //               )
                //             ],
                //           ],
                //         ),
                //       ),
                //     ),
                //   ],
                // ),
              ] else ...[
                Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Text(
                      '快速筛选',
                      style: TextStyle(
                        fontSize: 16.pt,
                        fontWeight: FontWeight.bold,
                      ),
                    ).asPaddingSymmetric(vertical: 8.pt),
                    ButtonTheme(
                      minWidth: 100.pt,
                      height: 39.pt,
                      textTheme: ButtonTextTheme.accent,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(4.pt),
                      ),
                      child: Obx(
                        () => Wrap(
                          spacing: 15.pt,
                          runSpacing: 15.pt,
                          alignment: WrapAlignment.start,
                          children: [
                            for (var item in store.timeList) ...[
                              WButton(
                                onPressed: () => store
                                    .changeTimeValue(item['code'] as String),
                                color: Color.fromRGBO(246, 247, 252, 1),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(4.pt),
                                  side: BorderSide(
                                    width: 1,
                                    color: store.findTypePra.value ==
                                            item['code']
                                        ? colorScheme.primary.withOpacity(0.5)
                                        : colorScheme.background,
                                    style: BorderStyle.solid,
                                  ),
                                ),
                                child: Text(
                                  item['name'] ?? '',
                                  style: TextStyle(
                                    fontSize: 14.pt,
                                    color:
                                        store.findTypePra.value == item['code']
                                            ? colorScheme.primary
                                            : colorScheme.minor,
                                  ),
                                ),
                              )
                            ],
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                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: Obx(
                        () => Wrap(
                          alignment: WrapAlignment.start,
                          children: [
                            WButton(
                              onPressed: () async {
                                final res = await WPickerUtil.year(
                                  context,
                                  initialDate: store.yearPra.isNotEmpty
                                      ? DateTime.parse('${store.yearPra}-01-01')
                                      : null,
                                );
                                if (res != null) {
                                  store.onChooseYear(res);
                                }
                              },
                              color: store.yearPra.isEmpty
                                  ? colorScheme.background
                                  : colorScheme.primary,
                              child: Text(
                                "${store.yearPra.isEmpty ? '请选择年份' : store.yearPra}",
                                style: TextStyle(
                                  fontSize: 14.pt,
                                  color: store.yearPra.isEmpty
                                      ? colorScheme.minor
                                      : Colors.white,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                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: Obx(
                        () => Wrap(
                          alignment: WrapAlignment.start,
                          children: [
                            WButton(
                              onPressed: () async {
                                final res = await WPickerUtil.month(
                                  context,
                                  initialDate: store.monthPra.isNotEmpty
                                      ? DateTime.parse(
                                          '2023-${store.monthPra}-01')
                                      : null,
                                );
                                if (res != null) {
                                  store.onChooseMonth(res);
                                }
                              },
                              color: store.monthPra.isEmpty
                                  ? colorScheme.background
                                  : colorScheme.primary,
                              child: Text(
                                "${store.monthPra.isEmpty ? '请选择月份' : store.monthPra}",
                                style: TextStyle(
                                  fontSize: 14.pt,
                                  color: store.monthPra.isEmpty
                                      ? colorScheme.minor
                                      : Colors.white,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                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: Obx(
                        () => Wrap(
                          alignment: WrapAlignment.start,
                          children: [
                            WButton(
                              onPressed: () async {
                                final res = await WPickerUtil.multiple<
                                    CommunityPickerModel>(
                                  context,
                                  title: Text('选择小区'),
                                  initialValue: _getCommunitySelectValue(),
                                  getData: () => store.getCommunityList(),
                                  isSearch: true,
                                );
                                if (res != null) {
                                  store.onChooseCommunity(res);
                                }
                              },
                              color: store.communityIdsPra.isEmpty
                                  ? colorScheme.background
                                  : colorScheme.primary,
                              child: Text(
                                "${store.communityIdsPra.isEmpty ? '请选择小区' : store.communityIName}",
                                style: TextStyle(
                                  fontSize: 14.pt,
                                  color: store.communityIdsPra.isEmpty
                                      ? colorScheme.minor
                                      : Colors.white,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                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: Obx(
                        () => Wrap(
                          alignment: WrapAlignment.start,
                          children: [
                            WButton(
                              onPressed: () async {
                                final res = await WPickerUtil.multiple<
                                    SinglePickerEntity>(
                                  context,
                                  title: Text('选择部门'),
                                  initialValue: _getSelectValue(),
                                  getData: () =>
                                      SinglePickerEntity.listMap2entity(
                                    store.organTree,
                                    'name',
                                    'id',
                                    enableValue: null,
                                  ),
                                  isSearch: true,
                                );
                                if (res != null) {
                                  store.onChooseOrganIds(
                                      res.map((e) => e.unique).toList());
                                }
                              },
                              color: store.organIdsPra.isEmpty
                                  ? colorScheme.background
                                  : colorScheme.primary,
                              child: Text(
                                "${store.organIdsPra.isEmpty ? '请选择部门' : store.organNames.value}",
                                style: TextStyle(
                                  fontSize: 14.pt,
                                  color: store.organIdsPra.isEmpty
                                      ? colorScheme.minor
                                      : Colors.white,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Text(
                      '任务类型',
                      style: TextStyle(
                        fontSize: 16.pt,
                        fontWeight: FontWeight.bold,
                      ),
                    ).asPaddingSymmetric(vertical: 10.pt),
                    Obx(() => Wisdom.row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(4.pt),
                          height: 240.pt,
                          children: [
                            SingleChildScrollView(
                              key: Key('taskStatFilterInner1'),
                              child: Wisdom.column(
                                width: (MediaQuery.of(context).size.width -
                                        100.pt) /
                                    2,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  for (var i = 0;
                                      i < store.taskItemListTree.length;
                                      i++) ...[
                                    if (store.taskItemListTree[i].children
                                            .length >
                                        0 && store.taskItemListTree[i].type == "preset") ...[
                                      Wisdom.row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        padding:
                                            EdgeInsets.fromLTRB(15.pt, 0, 0, 0),
                                        height: 38.pt,
                                        onTap: () => {
                                          store.changeFirstInventoryValue(
                                              store.taskItemListTree[i].id),
                                          store.setInventorySecondList(store
                                              .taskItemListTree[i].children),
                                          store.setInventoryFirstItem(
                                              store.taskItemListTree[i]),
                                        },
                                        color: store.inventoryFirstId.value ==
                                                store.taskItemListTree[i].id
                                            ? Color.fromRGBO(246, 247, 252, 1)
                                            : Colors.white,
                                        children: [
                                          if (store.inventoryFirstId.value ==
                                              store.taskItemListTree[i].id) ...[
                                            Container(
                                              height: 15.pt,
                                              width: 3.pt,
                                              color: colorScheme.primary,
                                              margin:
                                                  EdgeInsets.only(right: 10.pt),
                                            ),
                                          ],
                                          WisText(
                                            store.taskItemListTree[i].label ??
                                                '',
                                            size: 13.pt,
                                            color: store.inventoryFirstId
                                                        .value ==
                                                    store.taskItemListTree[i].id
                                                ? colorScheme.primary
                                                : colorScheme.minor,
                                            overflow: TextOverflow.ellipsis,
                                          ).asExpanded(),
                                        ],
                                      ),
                                    ]
                                  ],
                                ],
                              ),
                            ),
                            Wisdom(
                              width: 1.pt,
                              color: colorScheme.divider2,
                            ),
                            SingleChildScrollView(
                              key: Key('taskStatFilterInner2'),
                              child: Wisdom.column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  for (var i = 0;
                                      i < store.taskItemSecondList.length;
                                      i++) ...[
                                    Wisdom.row(
                                      padding: EdgeInsets.fromLTRB(
                                          15.pt, 0, 15.pt, 0),
                                      height: 38.pt,
                                      onTap: () => {
                                        store.changeSecondInventoryValue(
                                            store.taskItemSecondList[i].id),
                                      },
                                      color: store.taskItemSecondList[i].id ==
                                              store.statusCode
                                          ? Color.fromRGBO(246, 247, 252, 1)
                                          : Colors.white,
                                      children: [
                                        if (store.taskItemSecondList[i].id ==
                                            store.statusCode.value) ...[
                                          Image(
                                            image: AssetList.$dxy_png_image,
                                            width: 15.pt,
                                            height: 15.pt,
                                          ),
                                        ] else ...[
                                          Image(
                                            image: AssetList.$dxH_png_image,
                                            width: 15.pt,
                                            height: 15.pt,
                                          ),
                                        ],
                                        SizedBox(width: 10.pt,),
                                        WisText(
                                          store.taskItemSecondList[i].label ??
                                              '',
                                          size: 13.pt,
                                          color:
                                              store.taskItemSecondList[i].id ==
                                                      store.statusCode.value
                                                  ? colorScheme.primary
                                                  : colorScheme.minor,
                                          overflow: TextOverflow.ellipsis,
                                        ).asExpanded(),
                                      ],
                                    ),
                                  ],
                                ],
                              ),
                            ).asExpanded(),
                          ],
                        )),
                  ],
                ),
              ],
            ],
          ),
        )
      ],
    );
  }
}
