/*
 * @Author: ChenYaJin
 * @Date: 2023-01-13 14:38:49
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-01-16 15:01:01
 * @Description: 任务筛选页
 */
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:task_package/src/view_models/filter_store.dart';
import 'package:task_package/src/assets.gen.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 FilterStore store = Get.find<FilterStore>();

    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: [
          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 FilterStore store = Get.find<FilterStore>();

  @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: [
              Wisdom.column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Text(
                    '时间排序',
                    style: TextStyle(
                      fontSize: 16.pt,
                      fontWeight: FontWeight.bold,
                    ),
                  ).asPaddingSymmetric(vertical: 5.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 i = 0; i < store.timeList.length; i++) ...[
                            WButton(
                              onPressed: () => store
                                  .changeSortValue(store.timeList[i]['code']!),
                              color: Color.fromRGBO(246, 247, 252, 1),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(4.pt),
                                side: BorderSide(
                                  width: 1,
                                  color: store.sortCreatedAt.value ==
                                          store.timeList[i]['code']
                                      ? colorScheme.primary.withOpacity(0.5)
                                      : colorScheme.background,
                                  style: BorderStyle.solid,
                                ),
                              ),
                              child: Text(
                                store.timeList[i]['name'] ?? '',
                                style: TextStyle(
                                  fontSize: 13.pt,
                                  color: store.sortCreatedAt.value ==
                                          store.timeList[i]['code']
                                      ? colorScheme.primary
                                      : colorScheme.minor,
                                ),
                              ),
                            )
                          ],
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              Wisdom.column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Text(
                    '任务分类',
                    style: TextStyle(
                      fontSize: 16.pt,
                      fontWeight: FontWeight.bold,
                    ),
                  ).asPaddingSymmetric(vertical: 10.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 i = 0;
                              i < store.taskTypeList.length;
                              i++) ...[
                            WButton(
                              onPressed: () => store.changeTypeValue(
                                  store.taskTypeList[i]['code']!),
                              color: Color.fromRGBO(246, 247, 252, 1),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(4.pt),
                                side: BorderSide(
                                  width: 1,
                                  color: store.type.value ==
                                          store.taskTypeList[i]['code']
                                      ? colorScheme.primary.withOpacity(0.5)
                                      : colorScheme.background,
                                  style: BorderStyle.solid,
                                ),
                              ),
                              child: Text(
                                store.taskTypeList[i]['name'] ?? '',
                                style: TextStyle(
                                  fontSize: 13.pt,
                                  color: store.type.value ==
                                          store.taskTypeList[i]['code']
                                      ? colorScheme.primary
                                      : colorScheme.minor,
                                ),
                              ),
                            )
                          ],
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              Wisdom.column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Text(
                    '任务状态',
                    style: TextStyle(
                      fontSize: 16.pt,
                      fontWeight: FontWeight.bold,
                    ),
                  ).asPaddingSymmetric(vertical: 10.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 i = 0;
                                i < store.statusList.length;
                                i++) ...[
                              WButton(
                                onPressed: () => store.changeStateValue(
                                    store.statusList[i]['code']!),
                                color: Color.fromRGBO(246, 247, 252, 1),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(4.pt),
                                  side: BorderSide(
                                    width: 1,
                                    color: store.filterStatusCode.value ==
                                            store.statusList[i]['code']
                                        ? colorScheme.primary.withOpacity(0.5)
                                        : colorScheme.background,
                                    style: BorderStyle.solid,
                                  ),
                                ),
                                child: Text(
                                  store.statusList[i]['name'] ?? '',
                                  style: TextStyle(
                                    fontSize: 13.pt,
                                    color: store.filterStatusCode.value ==
                                            store.statusList[i]['code']
                                        ? colorScheme.primary
                                        : colorScheme.minor,
                                  ),
                                ),
                              )
                            ],
                          ],
                        )),
                  ),
                ],
              ),
              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('taskFilterInner1'),
                            child: Wisdom.column(
                              width:
                                  (MediaQuery.of(context).size.width - 100.pt) /
                                      2,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Wisdom.row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  padding: EdgeInsets.fromLTRB(15.pt, 0, 0, 0),
                                  height: 38.pt,
                                  onTap: () => {
                                    store.changeFirstInventoryValue(''),
                                    store.setInventorySecondList([]),
                                    store.setInventoryFirstItem(null),
                                  },
                                  color: store.inventoryFirstId.value == ''
                                      ? Color.fromRGBO(246, 247, 252, 1)
                                      : Colors.white,
                                  children: [
                                    if (store.inventoryFirstId.value == '') ...[
                                      Container(
                                        height: 15.pt,
                                        width: 3.pt,
                                        color: colorScheme.primary,
                                        margin: EdgeInsets.only(right: 10.pt),
                                      ),
                                    ],
                                    WisText(
                                      '全部',
                                      size: 13.pt,
                                      color: store.inventoryFirstId.value == ''
                                          ? colorScheme.primary
                                          : colorScheme.minor,
                                      overflow: TextOverflow.ellipsis,
                                    ).asExpanded(),
                                  ],
                                ),
                                for (var i = 0;
                                    i < store.taskItemList.length;
                                    i++) ...[
                                  if (store.taskItemList[i].children.length >
                                      0) ...[
                                    Wisdom.row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      padding:
                                          EdgeInsets.fromLTRB(15.pt, 0, 0, 0),
                                      height: 38.pt,
                                      onTap: () => {
                                        store.changeFirstInventoryValue(
                                            store.taskItemList[i].id),
                                        store.setInventorySecondList(
                                            store.taskItemList[i].children),
                                        store.setInventoryFirstItem(
                                            store.taskItemList[i]),
                                      },
                                      color: store.inventoryFirstId.value ==
                                              store.taskItemList[i].id
                                          ? Color.fromRGBO(246, 247, 252, 1)
                                          : Colors.white,
                                      children: [
                                        if (store.inventoryFirstId.value ==
                                              store.taskItemList[i].id) ...[
                                          Container(
                                             height: 15.pt,
                                            width: 3.pt,
                                            color: colorScheme.primary,
                                            margin:
                                                EdgeInsets.only(right: 10.pt),
                                          ),
                                        ],
                                        WisText(
                                          store.taskItemList[i].label ?? '',
                                          size: 13.pt,
                                          color: store.inventoryFirstId.value ==
                                                  store.taskItemList[i].id
                                              ? colorScheme.primary
                                              : colorScheme.minor,
                                          overflow: TextOverflow.ellipsis,
                                        ).asExpanded(),
                                      ],
                                    ),
                                  ]
                                ],
                              ],
                            ),
                          ),
                          Wisdom(
                            width: 1.pt,
                            color: colorScheme.divider2,
                          ),
                          SingleChildScrollView(
                            key: Key('taskFilterInner2'),
                            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.inventoryIds.length > 0 &&
                                            store.inventoryIds.contains(
                                                store.taskItemSecondList[i].id)
                                        ? Color.fromRGBO(246, 247, 252, 1)
                                        : Colors.white,
                                    children: [
                                      WisText(
                                        store.taskItemSecondList[i].label ?? '',
                                        size: 13.pt,
                                        color: store.inventoryIds.length > 0 &&
                                                store.inventoryIds.contains(
                                                    store.taskItemSecondList[i]
                                                        .id)
                                            ? colorScheme.primary
                                            : colorScheme.minor,
                                        overflow: TextOverflow.ellipsis,
                                      ).asExpanded(),
                                      if (store.inventoryIds.length > 0 &&
                                            store.inventoryIds.contains(
                                                store.taskItemSecondList[i].id)) ...[
                                                  Image(
                                        image: AssetList.$xzh_png_image,
                                        width: 10.pt,
                                        height: 10.pt,
                                      ),
                                                ],
                                      
                                    ],
                                  ),
                                ],
                              ],
                            ),
                          ).asExpanded(),
                        ],
                      )),
                ],
              ),
            ],
          ),
        )
      ],
    );
  }
}
