/*
 * @Author: ChenYaJin
 * @Date: 2023-04-13 11:02:42
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-25 10:24:38
 * @Description: 作业统计-任务列表
 */
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter/material.dart';
import 'package:task_package/src/components/task_item.dart';
import 'package:task_package/src/view_models/index_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class StatisticTaskListPage extends StatelessWidget {
  const StatisticTaskListPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => IndexStore(context)),
      ],
      child: IndexView(),
    );
  }
}

class IndexView extends StatelessWidget {
  const IndexView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // IndexStore store = Provider.of<IndexStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText('任务列表',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: _BuildBody(),
      ),
    );
  }
}

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

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

class __BuildBodyState extends State<_BuildBody>
    with SingleTickerProviderStateMixin {
  IndexStore get store => Provider.of(context, listen: false);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<IndexStore>(context, listen: true);

    return Column(
      children: <Widget>[
        Wisdom.row(
          color: Colors.white,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          margin: EdgeInsets.only(bottom: 10.pt),
          children: <Widget>[
            /// 输入框
            Expanded(
              child: WSearch(
                hintText: '输入任务标题搜索',
                onSubmitted: (v) => store.onSearchStatisticTaskTitles(v),
                onCancel: () => store.onSearchStatisticTaskTitles(''),
                moonlight: false,
                padding: EdgeInsets.symmetric(vertical: 10.pt),
                isDark: true,
              ),
            ),
          ],
        ),
        WisScrollView(
          key: store.statisticScrollViewListKey,
          onInit: () => store.initStatisticSourceList(isLoad: false),
          onRefresh: () => store.initStatisticSourceList(isLoad: false),
          onLoad: () => store.initStatisticSourceList(isLoad: true),
          emptyWidget: SliverToBoxAdapter(
            child: Wisdom.column(
              mainAxisAlignment: MainAxisAlignment.center,
              margin: EdgeInsets.only(bottom: 10.pt),
              padding: EdgeInsets.symmetric(vertical: 50.pt),
              children: [
                WEmptyWidget(
                  text: Text('空空如也~~'),
                ),
              ],
            ),
          ),
          slivers: <Widget>[
            SliverToBoxAdapter(
              child: Observer(
                builder: (_) {
                  return Wisdom.column(
                    mainAxisSize: MainAxisSize.min,
                    margin: EdgeInsets.symmetric(horizontal: 15.pt),
                    children: [
                      for (var task in store.statisticTaskList)
                        Wisdom(
                          margin: EdgeInsets.only(
                            bottom: 10.pt,
                          ),
                          child: TaskItem(
                            onClick: (item) =>
                                store.toTaskDetail(item.id, item.isChild),
                            item: task,
                            isMine: true,
                          ),
                        ),
                    ],
                  );
                },
              ),
            ),
          ],
        ).asExpanded(),
      ],
    );
  }
}
