import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/orders_page/bloc/order_content_bloc.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_filter_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/views/order_item_widget.dart';
import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart';
import 'package:zhiying_comm/util/base_bloc.dart';

class OrderContentPage extends StatefulWidget {
  final OrderFilterModel filter;

  const OrderContentPage({Key key, this.filter}) : super(key: key);

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

class _OrderContentPageState extends State<OrderContentPage> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<OrderContentBloc>(
      bloc: OrderContentBloc(),
      child: OrderContentContainer(widget.filter),
    );
  }
}

class OrderContentContainer extends StatefulWidget {
  final OrderFilterModel filter;

  const OrderContentContainer(this.filter, {Key key}) : super(key: key);

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

class _OrderContentContainerState extends State<OrderContentContainer> {
  OrderContentBloc _bloc;

  @override
  void initState() {
    _bloc = BlocProvider.of(context);
    _bloc.loadData(widget.filter);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print(context.toString());
    return StreamBuilder<List<OrderModel>>(
        stream: _bloc.outData,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.data == null) {
            return Container();
          }
          List<OrderModel> orders = snapshot.data;
          if (orders.length == 0) {
            return Container(
              child: Center(
                child: EmptyWidget(tips: '暂无订单，快去下一单吧'),
              ),
            );
          }
          return ListView.builder(
              itemCount: orders.length,
              itemBuilder: (context, index) {
                return OrderItemWidget(orders[index]);
              });
        });
  }
}
