/*
 * @Author: ChenYaJin
 * @Date: 2023-04-13 16:12:09
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-14 15:48:00
 * @Description: 点位/区域 异常列表
 */
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../components/abnormal_item.dart';
import 'package:task_package/src/view_models/work_statistic_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class RecordListPage extends StatelessWidget {
  const RecordListPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    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 {
  @override
  Widget build(BuildContext context) {
    final WorkStatisticStore store = Get.find<WorkStatisticStore>();

    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.onSearchAbnormalList(v),
                onCancel: () => store.onSearchAbnormalList(''),
                moonlight: false,
                padding: EdgeInsets.symmetric(vertical: 10.pt),
                isDark: true,
              ),
            ),
          ],
        ),
        WisScrollView(
          key: store.abnormalScrollViewListKey,
          onInit: () => store.getAbnormalList(isLoad: false),
          onRefresh: () => store.getAbnormalList(isLoad: false),
          onLoad: () => store.getAbnormalList(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>[
            Obx(
              () {
                return SliverToBoxAdapter(
                  child: Wisdom.column(
                    mainAxisSize: MainAxisSize.min,
                    margin: EdgeInsets.symmetric(horizontal: 15.pt),
                    children: [
                      for (var item in store.abnormalList)
                        Wisdom(
                          margin: EdgeInsets.only(
                            bottom: 10.pt,
                          ),
                          child: AbnormalItem(
                            onClick: (record) =>
                                store.onToAbnormalDetail(record),
                            item: item,
                          ),
                        ),
                    ],
                  ),
                );
              },
            ),
          ],
        ).asExpanded(),
      ],
    );
  }
}
