/*
 * @Author: WuWei
 * @Date: 2020-05-28 17:28:00
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-31 10:45:12
 * @Description:
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../components/block_clipper.dart';
import '../../view_models/house_detail_store.dart';
import 'component/info_item.dart';
import '../../components/info_empty.dart';
part 'component/resident_item.dart';
part 'component/topinfo_card.dart';
part 'component/car_item.dart';
part 'component/park_item.dart';
part 'component/bill_item.dart';
part 'component/meter_item.dart';

class HouseDetailPage extends StatelessWidget {
  const HouseDetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => SearchHouseDetailStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

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

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

class _PageViewState extends State<_PageView>
    with SingleTickerProviderStateMixin {
  TabController? tabController;

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SearchHouseDetailStore>(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.dark,
        iconTheme: IconThemeData(
          color: Colors.white,
          size: 19.pt,
        ),
        title: Text('房屋信息'),
      ),
      body: WisScrollView(
        key: store.scorllViewKey,
        onInit: () => store.getHouseDetail(
          id: store.args['id'],
        ),
        slivers: <Widget>[
          _TopInfoCard(),
          SliverPersistentHeader(
            pinned: true,
            floating: true,
            delegate: StickyTabBarDelegate(
              child: Wisdom(
                color: Color(0xFFF8F8F8),
                child: TabBar(
                  tabAlignment: TabAlignment.start,
                  dividerHeight: 0,
                  isScrollable: true,
                  controller: this.tabController,
                  labelColor: colorScheme.blue,
                  indicatorColor: colorScheme.primary,
                  unselectedLabelColor: Color(0xFF666666),
                  indicatorSize: TabBarIndicatorSize.label,
                  labelStyle: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 16.pt,
                  ),
                  unselectedLabelStyle: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 15.pt,
                  ),
                  tabs: <Widget>[
                    Tab(text: '住户信息'),
                    Tab(text: '相关车辆'),
                    Tab(text: '相关车位'),
                    Tab(text: '欠费信息'),
                    Tab(text: '抄表情况'),
                  ],
                ),
              ),
            ),
          ),
          SliverFillRemaining(
            // hasScrollBody: false,
            // fillOverscroll: false,
            child: TabBarView(
              controller: this.tabController,
              children: <Widget>[
                _ResidentInfo(
                    onInit: () => store.getHouseResident(
                          id: store.args['id'],
                        )),
                _Carinfo(
                    onCarInit: () => store.getHouseCar(
                          id: store.args['id'],
                        )),
                _Parkinfo(
                    onInit: () => store.getHousePark(
                          id: store.args['id'],
                        )),
                _BillInfo(
                  onInit: () => store.getHouseBill(
                    id: store.args['id'],
                  ),
                ),
                _MeterInfo(
                    onInit: () => store.getHouseMeter(
                          id: store.args['id'],
                        )),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

/// 滑动吸顶的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;
  }
}
