/*
 * @Author: WuWei
 * @Date: 2020-07-21 10:20:28
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-03 17:32:17
 * @Description:
 */
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../../assets.gen.dart';
import 'package:service_center_package/src/models/inner_sort_list.dart';
import 'package:service_center_package/src/view_models/service_center_report.dart';
import 'package:service_center_package/src/view_models/service_center_repair.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:service_center_package/src/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';
part 'components/report_page.dart';
part 'components/repair_page.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 ServiceCenterReportStore store =
      Get.isRegistered<ServiceCenterReportStore>()
          ? Get.find<ServiceCenterReportStore>()
          : Get.put(ServiceCenterReportStore());
  final ServiceCenterRepairStore repairStore =
      Get.isRegistered<ServiceCenterRepairStore>()
          ? Get.find<ServiceCenterRepairStore>()
          : Get.put(ServiceCenterRepairStore());

  @override
  void dispose() {
    Get.delete<ServiceCenterReportStore>();
    Get.delete<ServiceCenterRepairStore>();
    super.dispose();
  }

  @override
  void initState() {
    store.onInitData();
    repairStore.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);
        repairStore.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>[
          SliverPersistentHeader(
            pinned: true,
            floating: true,
            delegate: StickyTabBarDelegate(
              child: Wisdom(
                key: store.scrollKey,
                color: Colors.white,
                padding: EdgeInsets.only(bottom: 4.pt),
                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: [
          _ReportPage(),
          _RepairPage(),
        ],
      ),
    );
  }
}
