/*
 * @Author: WuWei
 * @Date: 2020-06-29 11:25:32
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-27 14:34:11
 * @Description:
 */
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:repair_package/src/assets.gen.dart';
import 'package:app_package/src/models/community_picker.dart';
import 'package:repair_package/src/models/inner_sort_list.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/repair_index_model.dart';
import '../../components/list_item.dart';
import 'components/sticky_tab_bar.dart';
import 'components/last.dart';
part 'components/search.dart';
part 'components/all.dart';
part 'components/copy.dart';

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

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

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

class _PageViewState extends State<_PageView>
    with SingleTickerProviderStateMixin {
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      backgroundColor: Color(0xFFF8F8F8),
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText(
            '${Get.arguments != null ? Get.arguments['appBarTitle'] ?? '报修管理' : '报修管理'}',
            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>
    with SingleTickerProviderStateMixin {
  final RepairIndexStore store = Get.isRegistered<RepairIndexStore>()
      ? Get.find<RepairIndexStore>()
      : Get.put(RepairIndexStore());
  @override
  void dispose() {
    Get.delete<RepairIndexStore>();
    super.dispose();
  }

  @override
  void initState() {
    store.onInitData();
    store.tabController = TabController(
      vsync: this,
      length: 2,
      initialIndex: store.tabIndex.value,
    );
    store.tabController!.addListener(() {
      if (store.tabController!.index != store.tabIndex.value) {
        store.onSetTabIndex(store.tabController!.index);
      }
    });
    super.initState();
  }

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

    return NestedScrollView(
      controller: store.outerController,
      headerSliverBuilder: (context, innerBoxIsScrolled) {
        return <Widget>[
          Wisdom(
            child: Obx(() => new _BuildSearchInput(
                  title: store.searchKeyWord.value,
                  onInput: (v) => store.onSearchName(v),
                  onCancel: () => store.onSearchName(''),
                )),
          ).asSliver(),
          Wisdom.row(
            padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
            color: Colors.white,
            children: [
              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                padding: EdgeInsets.all(8.pt),
                margin: EdgeInsets.only(right: 10.pt),
                borderRadius: BorderRadius.circular(4.pt),
                color: colorScheme.background4,
                onTap: () async {
                  final res = await WPickerUtil.single<CommunityPickerModel>(
                    context,
                    title: Text('选择小区'),
                    initialValue: store.community.value,
                    getData: () => store.getCommunityList(),
                    isSearch: true,
                  );
                  if (res != null) {
                    store.onChooseCommunity(res);
                  }
                },
                children: [
                  Wisdom(
                    margin: EdgeInsets.only(
                      right: 10.pt,
                    ),
                    child: Obx(() => WisText(
                          store.communityIName.isNotEmpty
                              ? store.communityIName.value
                              : '小区选择',
                          size: 14.pt,
                          weight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                          color: store.communityIName.isNotEmpty
                              ? colorScheme.primary
                              : colorScheme.mainTitle,
                        )),
                  ).asExpanded(),
                  Image(
                    image: AssetList.$tzhxq_png_image,
                    height: 10.pt,
                  ),
                ],
              ).asExpanded(),
              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                padding: EdgeInsets.all(8.pt),
                margin: EdgeInsets.only(right: 10.pt),
                borderRadius: BorderRadius.circular(4.pt),
                color: colorScheme.background4,
                onTap: () async {
                  final res = await WPickerUtil.single<InnerSortListModel>(
                    context,
                    title: Text('选择业主分类'),
                    initialValue: store.innerSort.value,
                    getData: () => store.getInnerSortList(),
                    isSearch: false,
                  );
                  if (res != null) {
                    store.onChooseInnerSort(res);
                  }
                },
                children: [
                  Wisdom(
                      margin: EdgeInsets.only(
                        right: 10.pt,
                      ),
                      child: Obx(
                        () => WisText(
                          store.innerSort.value.name ?? '内部分类',
                          size: 14.pt,
                          weight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                          color: store.innerSort.value.name != null
                              ? colorScheme.primary
                              : colorScheme.mainTitle,
                        ),
                      )).asExpanded(),
                  Image(
                    image: AssetList.$tzhxq_png_image,
                    height: 10.pt,
                  ),
                ],
              ).asExpanded(),
              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                padding: EdgeInsets.all(8.pt),
                margin: EdgeInsets.only(right: 10.pt),
                borderRadius: BorderRadius.circular(4.pt),
                color: colorScheme.background4,
                onTap: () async {
                  final res = await WPickerUtil.date(
                    context,
                    title: WisText('选择日期'),
                    initialDate: store.selectDate.value.isNotEmpty
                        ? DateTime.parse(store.selectDate.value)
                        : DateTime.now(),
                  );
                  store.setDate(res);
                },
                children: [
                  Wisdom(
                    margin: EdgeInsets.only(
                      right: 10.pt,
                    ),
                    child: Obx(() => WisText(
                          store.selectDate.value.isNotEmpty
                              ? store.selectDate.value
                              : '日期筛选',
                          size: 14.pt,
                          weight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                          color: store.selectDate.value.isNotEmpty
                              ? colorScheme.primary
                              : colorScheme.mainTitle,
                        )),
                  ).asExpanded(),
                  Image(
                    image: AssetList.$tzhxq_png_image,
                    height: 10.pt,
                  ),
                ],
              ).asExpanded(),
              Wisdom.row(
                children: [
                  Wisdom(
                    onTap: () => store.onReset(),
                    padding:
                        EdgeInsets.symmetric(horizontal: 8.pt, vertical: 5.pt),
                    // margin: EdgeInsets.only(right: 15.pt),
                    color: colorScheme.primary,
                    borderRadius: BorderRadius.circular(4.pt),
                    child: WisText(
                      '重置',
                      size: 13.pt,
                      weight: FontWeight.w400,
                      color: Colors.white,
                    ),
                  )
                ],
              )
            ],
          ).asSliver(),
          SliverToBoxAdapter(
            child: Obx(
              () => Wisdom(
                height: 44.pt,
                color: Colors.white,
                padding:
                    EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
                child: Wisdom.row(
                  color: Colors.white,
                  children: [
                    CustomScrollView(
                      scrollDirection: Axis.horizontal,
                      slivers: [
                        SliverToBoxAdapter(
                          child: Wisdom.row(
                            children: [
                              for (var item in store.statisticStatus)
                                Wisdom(
                                  borderRadius: BorderRadius.circular(4.pt),
                                  color: Color(0xFFF6F7FC),
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 8.5.pt,
                                    vertical: 5.pt,
                                  ),
                                  margin: EdgeInsets.only(right: 12.pt),
                                  onTap: () => store
                                      .filterStatus(item['code'] as String),
                                  child: Wisdom.rich(
                                    overflow: TextOverflow.ellipsis,
                                    children: [
                                      TextSpan(
                                        text: item['name'] as String,
                                        style: TextStyle(
                                            fontSize: 14.pt,
                                            fontWeight: FontWeight.w400,
                                            color: store.statusCode.value ==
                                                    item['code']
                                                ? colorScheme.primary
                                                : Color(0xFF616178)),
                                      ),
                                      if (item['show'] as bool == true)
                                        TextSpan(
                                          text: '(${item['quantity'] ?? 0})',
                                          style: TextStyle(
                                              fontSize: 14.pt,
                                              fontWeight: FontWeight.w400,
                                              color: store.statusCode.value ==
                                                      item['code']
                                                  ? colorScheme.primary
                                                  : Color(0xFF616178)),
                                        ),
                                    ],
                                  ),
                                ),
                            ],
                          ),
                        ),
                      ],
                    ).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: store.isFilterStatus
                                  ? colorScheme.primary
                                  : colorScheme.mainTitle,
                            )),
                        Image(
                          image: store.isFilterStatus
                              ? AssetList.$filter_blue_png_image
                              : AssetList.$filter_png_image,
                          height: 20.pt,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
          SliverPersistentHeader(
            pinned: true,
            floating: true,
            delegate: StickyTabBarDelegate(
              child: Wisdom(
                key: store.scrollKey,
                color: Colors.white,
                child: TabBar(
                  controller: store.tabController,
                  dividerHeight: 0,
                  labelColor: colorScheme.text,
                  indicatorColor: colorScheme.primary,
                  unselectedLabelColor: colorScheme.minor,
                  indicatorSize: TabBarIndicatorSize.label,
                  indicatorPadding: EdgeInsets.zero,
                  indicatorWeight: 2.0,
                  labelPadding: EdgeInsets.symmetric(vertical: 10.pt),
                  indicator: RoundUnderlineTabIndicator(
                    width: 25.pt,
                  ),
                  labelStyle: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 18.pt,
                  ),
                  unselectedLabelStyle: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 16.pt,
                  ),
                  tabs: [
                    Wisdom(
                      padding: EdgeInsets.symmetric(horizontal: 10.pt),
                      child: WisText('全部'),
                    ),
                    Wisdom(
                      padding: EdgeInsets.symmetric(horizontal: 10.pt),
                      child: WisText('抄送我的'),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ];
      },
      body: TabBarView(
        controller: store.tabController,
        children: [
          _ListAllView(),
          _ListCopyView(),
        ],
      ),
    );
  }
}
