import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/dialog/select_date_ym_dialog/select_date_ym_dialog.dart';
import 'package:zhiying_base_widget/pages/bil_detail_page/bil_detail_page_bloc.dart';
import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
import 'package:zhiying_base_widget/utils/contants.dart';
import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
import 'package:zhiying_base_widget/widgets/wallet/wallet_detail/wallet_detail_sk.dart';
import 'package:zhiying_base_widget/widgets/wallet_bil_detail/wallet_bil_sk.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/util/custom_sliver_persistent_header_delegate.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_alibc/flutter_alibc.dart';

///钱包明细
class BilDetailPage extends StatefulWidget {
  final Map<String, dynamic> data;

  const BilDetailPage(this.data, {Key key}) : super(key: key);

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

class _BilDetailPageState extends State<BilDetailPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff9f9f9),
      body: MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: MainPageNotifier()),
          ChangeNotifierProvider.value(value: MainPageBgNotifier()),
        ],
        child: BlocProvider<BilDetailPageBloc>(
          bloc: BilDetailPageBloc(),
          child: BilDetailPageContainer(data: widget.data),
        ),
      ),
    );
  }
}

class BilDetailPageContainer extends StatefulWidget {
  final Map<String, dynamic> data;

  const BilDetailPageContainer({Key key, this.data}) : super(key: key);

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

class _BilDetailPageContainerState extends State<BilDetailPageContainer> {
  BilDetailPageBloc _bloc;

  String showDate;

  @override
  void initState() {
    _bloc = BlocProvider.of<BilDetailPageBloc>(context);
    _bloc.loadData(widget.data[Constants.SkipIdentifierName]);
    showDate = DateFormat('yyyy-MM').format(DateTime.now());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: _bloc.outData,
      builder: (context, sny) {
        if (sny.data == null) {
          return WalletBilDetailSkeleton();
        }
        var model = sny.data;
        return Scaffold(
          body: Container(
              child: Column(
            children: _buildItem(model),
          )),
        );
      },
    );
  }

  ///构建子模块
  _buildItem(List<Map<String, dynamic>> model) {
    List<Widget> list = List();
    for (var item in model) {
      WidgetModel widgetModel =
          WidgetModel.fromJson(Map<String, dynamic>.from(item));
      if (widgetModel.modName == 'my_wallet_detail') {
        list.add(Expanded(
            child: WidgetFactory.create(widgetModel.modName,
                isSliver: false, model: item)[0]));
        continue;
      }
      list.addAll(WidgetFactory.create(widgetModel.modName,
          isSliver: false, model: item));
    }
    return list;
  }
}
