/*
 * @Author: WuWei
 * @Date: 2020-05-28 17:28:00
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-06 10:39:53
 * @Description:
 */

import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../components/block_clipper.dart';
import '../../components/info_empty.dart';
import '../../view_models/resident_detail_store.dart';
import 'component/info_item.dart';
part 'component/wechat_item.dart';
part 'component/topinfo_card.dart';
part 'component/house_item.dart';
part 'component/car_item.dart';
part 'component/park_item.dart';

class ResidentDetailPage extends StatelessWidget {
  const ResidentDetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => SearchResidentDetailStore(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: 4, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SearchResidentDetailStore>(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('住户信息'),
        actions: <Widget>[
          Wisdom(
            onTap: () => store.toEdit(),
            padding: EdgeInsets.symmetric(horizontal: 10.pt),
            child: Image.asset(
              'lib/assets/bj.png',
              width: 17.pt,
              height: 17.pt,
              package: 'search_package',
            ),
          ),
        ],
      ),
      body: WisScrollView(
        key: store.scorllViewKey,
        onInit: () => store.getResidentDetail(
          id: store.args['id'],
        ),
        slivers: <Widget>[
          _TopInfoCard(),
          SliverPersistentHeader(
            pinned: true,
            floating: true,
            delegate: StickyTabBarDelegate(
              child: Wisdom(
                color: Color(0xFFF8F8F8),
                child: TabBar(
                  dividerHeight: 0,
                  tabAlignment: TabAlignment.start,
                  controller: this.tabController,
                  labelColor: colorScheme.blue,
                  indicatorColor: colorScheme.primary,
                  isScrollable: true,
                  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: '相关车位'),
                  ],
                ),
              ),
            ),
          ),
          SliverFillRemaining(
            // hasScrollBody: false,
            // fillOverscroll: false,
            child: TabBarView(
              controller: this.tabController,
              children: <Widget>[
                _WechatInfo(
                    onInit: () => store.getResidentWechat(
                          id: store.args['id'],
                        )),
                _HouseInfo(
                    onHouseInit: () => store.getResidentHouse(
                          id: store.args['id'],
                        )),
                _Carinfo(
                    onCarInit: () => store.getResidentCar(
                          id: store.args['id'],
                        )),
                _Parkinfo(
                    onInit: () => store.getResidentPark(
                          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;
  }
}
