/*
 * @Author: WuXiangNan
 * @Date: 2021-04-20 16:15:22
 * @LastEditors: WuXiangNan
 * @LastEditTime: 2021-08-16 15:04:39
 * @FilePath: /wisdom-huiguanjia-app/business_package/lib/src/pages/index.dart
 * @Description: 
 */

import 'package:business_package/src/view_models/business_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../assets.gen.dart';
// import 'package:business_package/src/components/area_line_chart.dart';
// import 'package:business_package/src/components/bar_charts.dart';
// import 'package:business_package/src/components/pie_charts.dart';
import 'package:intl/intl.dart';

import 'data/filter_checkbox.dart';
part 'data/data.dart';

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

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => BusinessStore(context),
      child: _PageView(),
    );
  }
}

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

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

class __PageViewState extends State<_PageView>
    with SingleTickerProviderStateMixin {
  TabController? tabController;
  BusinessStore get businessStore =>
      Provider.of<BusinessStore>(context, listen: false);

  void initState() {
    businessStore.getReady();
    super.initState();
    this.tabController = TabController(length: 3, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    BusinessStore businessStore = Provider.of<BusinessStore>(context);
    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),

        /// 顶部按钮
        actions: <Widget>[
          Wisdom.row(
            children: <Widget>[
              Wisdom(
                // onTap: () => businessStore.onSelectProject(),
                onTap: () async {
                  var value = await WisCheckBox.show(
                      businessStore.selectedCommunities,
                      businessStore.communityList.length);
                  if (value != null) {
                    businessStore.selectedCommunities = value;
                    var list = [];
                    value.forEach((key, value) {
                      list = list + value;
                    });
                    if (list.length > 0) {
                      businessStore.communityIds = list.join(',');
                      businessStore.getData();
                    }
                  }
                },
                margin: EdgeInsets.only(right: 25.pt),
                child: Image(
                  image: AssetList.$sx_png_image,
                  height: 17.pt,
                ),
              ),
            ],
          )
        ],
      ),
      body: NestedScrollView(
        headerSliverBuilder: (context, innerBoxIsScrolled) {
          return <Widget>[
            // 项目数据
            SliverToBoxAdapter(
              child: Wisdom.column(
                // height: 200.pt,
                color: Colors.white,
                padding: EdgeInsets.all(15.pt),
                margin: EdgeInsets.only(bottom: 10.pt),
                children: [
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      WisText(
                        '项目数据',
                        bold: true,
                        size: 15.pt,
                      ),
                      Wisdom.row(
                        onTap: () => businessStore.showProject(),
                        children: [
                          WisText(
                            '查看清单 ',
                            color: colorScheme.primary,
                          ),
                          Icon(
                            Icons.chevron_right,
                            color: colorScheme.primary,
                          ),
                        ],
                      )
                    ],
                  ),
                  Wisdom.wrap(children: [
                    Divider(
                      height: 20.pt,
                    ),
                    Wisdom.row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      margin: EdgeInsets.symmetric(vertical: 5.pt),
                      children: [
                        WisText(
                          '项目数量',
                        ),
                        WisText(
                          '${businessStore.communityList.length}个',
                        )
                      ],
                    ),
                    Wisdom.row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      margin: EdgeInsets.symmetric(vertical: 5.pt),
                      children: [
                        WisText(
                          '在管物业面积',
                        ),
                        WisText(
                          '${businessStore.projectData.area}㎡',
                        )
                      ],
                    ),
                    if (businessStore.isShowList) _buildProjectDetail()
                  ])
                ],
              ),
            ),
            SliverPersistentHeader(
              pinned: true,
              delegate: StickyTabBarDelegate(
                child: Wisdom(
                  color: Colors.white,
                  child: TabBar(
                    dividerHeight: 0,
                    controller: this.tabController,
                    labelColor: colorScheme.mainTitle,
                    unselectedLabelColor: Color(0xFF666666),
                    indicator: RoundUnderlineTabIndicator(
                      width: 25.pt,
                    ),
                    labelPadding: EdgeInsets.symmetric(
                        vertical: 10.pt, horizontal: 17.pt),
                    labelStyle: TextStyle(
                      fontWeight: FontWeight.w600,
                      fontSize: 16.pt,
                    ),
                    unselectedLabelStyle: TextStyle(
                      fontWeight: FontWeight.normal,
                      fontSize: 15.pt,
                    ),
                    tabs: [
                      Tab(text: '日数据'),
                      Tab(text: '月数据'),
                      Tab(text: '年数据'),
                    ],
                    onTap: (index) => businessStore.onChangeTab(index),
                  ),
                ),
              ),
            )
          ];
        },
        body: TabBarView(
          controller: this.tabController,
          children: <Widget>[
            _DataPageView(),
            _DataPageView(),
            _DataPageView(),
          ],
        ),
      ),
    );
  }

  _buildProjectDetail() {
    final colorScheme = WTheme.of(context).colorScheme;
    BusinessStore businessStore = Provider.of<BusinessStore>(context);
    return Wisdom.wrap(
      children: [
        Divider(
          height: 20.pt,
        ),
        Wisdom.row(
          margin: EdgeInsets.only(bottom: 10.pt),
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('楼栋数'),
                WisText(
                  '${businessStore.projectData.buildingCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
            VerticalDivider(
              width: 25.pt,
              color: Colors.black,
            ),
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('房屋数'),
                WisText(
                  '${businessStore.projectData.houseCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
            VerticalDivider(
              width: 25.pt,
              color: Colors.black,
            ),
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('住户数'),
                WisText(
                  '${businessStore.projectData.householdCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
            VerticalDivider(
              width: 25.pt,
              color: Colors.black,
            ),
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('车辆数'),
                WisText(
                  '${businessStore.projectData.carCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
          ],
        ),
        Wisdom.row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('车位数'),
                WisText(
                  '${businessStore.projectData.parkCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
            VerticalDivider(
              width: 25.pt,
              color: Colors.black,
            ),
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('电表数'),
                WisText(
                  '${businessStore.projectData.electricMeterCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
            VerticalDivider(
              width: 25.pt,
              color: Colors.black,
            ),
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('水表数'),
                WisText(
                  '${businessStore.projectData.waterMeterCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
            VerticalDivider(
              width: 25.pt,
              color: Colors.black,
            ),
            Wisdom.column(
              width: (MediaQuery.of(context).size.width - 105.pt) / 4,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText('公共表数'),
                WisText(
                  '${businessStore.projectData.publicMeterCount}',
                  color: colorScheme.primary,
                )
              ],
            ),
          ],
        )
      ],
    );
  }
}

/// 滑动吸顶的tabbar
class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final child;

  StickyTabBarDelegate({required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return this.child;
  }

  @override
  double get maxExtent => this.child.child.preferredSize.height;

  @override
  double get minExtent => this.child.child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
