/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-07-23 14:47:40
 * @LastEditTime : 2023-08-18 15:13:49
 * @Description  : 任务管理 -- 扫一扫
 */

import 'package:flutter/material.dart';
import 'package:task_package/src/components/task_item.dart';
import 'package:task_package/src/view_models/scan_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:get/get.dart';

import '../../assets.gen.dart';

class ScanTaskPage extends StatelessWidget {
  const ScanTaskPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ScanPageView();
  }
}

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

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('扫一扫',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: _BuildBody(),
    );
  }
}

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

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

class __BuildBodyState extends State<_BuildBody> {
  void dispose() {
    Get.delete<ScanPageStore>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _BuildList().asExpanded(),
      ],
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final ScanPageStore store = Get.isRegistered<ScanPageStore>()
        ? Get.find<ScanPageStore>()
        : Get.put(ScanPageStore());
    final colorScheme = WTheme.of(context).colorScheme;
    return Column(
      children: [
        Wisdom(
          color: Colors.white,
          child: WSearch(
            hintText: '输入任务标题搜索',
            onSubmitted: (v) => {store.onSearch(v)},
            onCancel: () => store.onSearch(''),
            moonlight: false,
            isDark: true,
          ),
        ),
        Wisdom(
          height: 44.pt,
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
          child: Wisdom.row(
            children: [
              CustomScrollView(scrollDirection: Axis.horizontal, slivers: [
                SliverToBoxAdapter(child: Obx(() {
                  return Wisdom.row(
                    children: [
                      Obx(() {
                        return Wisdom(
                          borderRadius: BorderRadius.circular(4.pt),
                          color: Color(0xFFF6F7FC),
                          padding: EdgeInsets.symmetric(
                            horizontal: 8.5.pt,
                            vertical: 5.pt,
                          ),
                          onTap: () => store.chooseType(''),
                          margin: EdgeInsets.only(right: 12.pt),
                          child: WisText(
                            '全部',
                            size: 14.pt,
                            color: store.type.value == ''
                                ? colorScheme.primary
                                : colorScheme.minor,
                          ),
                        );
                      }),
                      for (var i = 0; i < store.taskTypeList.length; i++)
                        Obx(() {
                          return Wisdom(
                            borderRadius: BorderRadius.circular(4.pt),
                            color: Color(0xFFF6F7FC),
                            padding: EdgeInsets.symmetric(
                              horizontal: 8.5.pt,
                              vertical: 5.pt,
                            ),
                            onTap: () =>
                                store.chooseType(store.taskTypeList[i].id),
                            margin: EdgeInsets.only(right: 12.pt),
                            child: WisText(
                              store.taskTypeList[i].name,
                              size: 14.pt,
                              color:
                                  store.type.value == store.taskTypeList[i].id
                                      ? colorScheme.primary
                                      : colorScheme.minor,
                            ),
                          );
                        })
                    ],
                  );
                })),
              ]).asExpanded(),
              Wisdom.row(
                color: Colors.white,
                padding: EdgeInsets.symmetric(vertical: 5.pt),
                width: 75.pt,
                onTap: () => store.toFilterPage(),
                alignment: Alignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('筛选',
                      style: TextStyle(
                          fontSize: 13.pt, color: colorScheme.primary)),
                  Image(
                    image: AssetList.$filter_blue_png_image,
                    height: 20.pt,
                  ),
                ],
              ),
            ],
          ),
        ),
        SizedBox(height: 10.pt),
        WisScrollView(
          key: store.scrollViewKey,
          controller: store.controller,
          onRefresh: () => store.onGetScanTaskList(),
          onInit: () => store.init(),
          onLoad: () => store.onGetScanTaskList(),
          slivers: <Widget>[
            Obx(() {
              return WisSliverList.childBuilder(
                childCount: store.dataList.length,
                interval: 10.pt,
                builder: (context, index) {
                  final item = store.dataList[index];
                  return Container(
                    margin: EdgeInsets.symmetric(horizontal: 15.pt),
                    child: TaskItem(
                      item: item,
                      onClick: (item) => store.route2Detail(item),
                    ),
                  );
                },
              );
            }),
          ],
        ).asExpanded(),
      ],
    );
  }
}
