import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/pages/orders_page/bloc/order_search_bloc.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_page_style_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';
import 'package:zhiying_comm/zhiying_comm.dart';

// 订单搜索页面
class OrderSearchPage extends StatefulWidget {
  final OrderPageStyleModel style;

  const OrderSearchPage(this.style, {Key key}) : super(key: key);

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

class _OrderSearchPageState extends State<OrderSearchPage> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<OrderSearchBloc>(
      bloc: OrderSearchBloc(),
      child: _OrderSearchContainer(widget.style),
    );
  }
}

class _OrderSearchContainer extends StatefulWidget {
  final OrderPageStyleModel style;

  const _OrderSearchContainer(this.style, {Key key}) : super(key: key);

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

class _OrderSearchContainerState extends State<_OrderSearchContainer> {
  final TextEditingController _keywordController = TextEditingController();
  OrderSearchBloc _bloc;
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  void initState() {
    _bloc = BlocProvider.of(context);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _createNav(),
      body: SafeArea(
        child: StreamBuilder<List<OrderModel>>(
            stream: _bloc.outData,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.data == null) {
                return Container();
              }
              _refreshController.refreshCompleted();
              List<OrderModel> orders = snapshot.data;
              if (orders.length == 0) {
                return Container(
                  child: Center(
                    child: EmptyWidget(),
                  ),
                );
              }
              return SmartRefresher(
                enablePullDown: false,
                enablePullUp: true,
                controller: _refreshController,
                onLoading: () {
                  _bloc.loadMore();
                },
                child: ListView.builder(
                    itemCount: orders.length,
                    itemBuilder: (context, index) {
                      return OrderItemWidget(
                        orders[index],
                        widget.style,
                      );
                    }),
              );
            }),
      ),
    );
  }

  // 导航栏
  Widget _createNav() {
    return CupertinoNavigationBar(
      border: Border(
        bottom: BorderSide(
          width: 0.0, // One physical pixel.
          style: BorderStyle.none,
        ),
      ),
      backgroundColor: Colors.white,
      leading: Navigator.canPop(context)
          ? GestureDetector(
              child: Container(
                padding: EdgeInsets.zero,
                child: Icon(
                  Icons.arrow_back_ios,
                  size: 20,
                ),
              ),
              onTap: () {
                if (Navigator.canPop(context)) {
                  Navigator.pop(context);
                }
              },
            )
          : Container(),
      middle: Container(
        height: 32,
        child: CupertinoTextField(
          autofocus: true,
          padding: EdgeInsets.only(left: 12, right: 12),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(16),
              color: Color(0xfff9f9f9)),
          placeholder: '搜索商品标题/粉丝姓名/订单号',
          placeholderStyle: TextStyle(color: Color(0xff999999), fontSize: 14),
          controller: _keywordController,
          style: TextStyle(fontSize: 14, color: Color(0xff333333)),
          textInputAction: TextInputAction.search,
          onSubmitted: _search,
        ),
      ),
      trailing: Container(
        padding: EdgeInsets.only(left: 10),
        child: CupertinoButton(
          padding: EdgeInsets.zero,
          child: Text(
            '搜索',
            style: TextStyle(
              fontSize: 14,
              color: Colors.redAccent,
            ),
          ),
          onPressed: () {
            Logger.debug(_keywordController.text);
            _search(_keywordController.text);
          },
        ),
      ),
    );
  }

  void _search(String keyword) {
    FocusScope.of(context).requestFocus(FocusNode());
    if (_keywordController.text == null || _keywordController.text == '') {
      Fluttertoast.showToast(msg: '请输入搜索关键词');
      return;
    }
    _bloc.refresh(_keywordController.text);
  }
}
