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_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);
    showDate = DateFormat('yyyy-MM').format(DateTime.now());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return StreamBuilder(
      builder: (context, sny) {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("明细"),
          ),
          body: Container(
            child: ListView(
              shrinkWrap: true,
              children: <Widget>[_buildType(), _buildBottomItem()],
            ),
          ),
        );
      },
    );
  }

  _buildType() {
    return Row(
      children: <Widget>[
        SizedBox(width: 12.5),
        InkWell(
          onTap: () {
            ///显示日期选择弹窗
            _selectDate();
          },
          child: Padding(
            padding: const EdgeInsets.only(top: 8, bottom: 8),
            child: Text(showDate ?? ""),
          ),
        ),
        Icon(Icons.arrow_drop_down),
        Expanded(
            child: Container(
          height: 50,
          child: ListView.builder(
              scrollDirection: Axis.horizontal,
              shrinkWrap: true,
              itemCount: 3,
              itemBuilder: (context, index) {
                return InkWell(child: Container(
                  margin:
                  EdgeInsets.only(top: 10, left: 8, right: 8, bottom: 10),
                  decoration: BoxDecoration(
                      color: Colors.grey[200],
                      borderRadius: BorderRadius.circular(50)),
                  child: Padding(
                    padding: const EdgeInsets.only(left: 16, right: 16),
                    child: Center(
                        child: Text(
                          "推广",
                          style: TextStyle(color: Colors.grey),
                        )),
                  ),
                ),onTap: () async {
                  print("ok");
                  await FlutterAlibc.loginTaoBao();
                },);
              }),
        ))
      ],
    );
  }

  void _selectDate() async {
    showDialog(context: context, builder: (context) => SelectDateYMDialog());
  }

  _buildBottomItem() {
    return ListView.builder(
        shrinkWrap: true, itemCount: 10, itemBuilder: _buildItem);
  }

  Widget _buildItem(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.only(left: 12.5, right: 12.5, bottom: 4, top: 4),
      padding:
          EdgeInsets.only(left: 25.w, right: 25.w, top: 20.h, bottom: 20.h),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4),
                        color: Colors.red[100],
                        border: Border.all(color: Colors.red)),
                    child: Padding(
                      padding: const EdgeInsets.only(
                          left: 4, right: 4, top: 2, bottom: 2),
                      child: Center(
                        child: Text(
                          "自购",
                          style: TextStyle(color: Colors.red, fontSize: 20.sp),
                        ),
                      ),
                    ),
                  ),
                  Container(
                      width: 443.w,
                      margin: EdgeInsets.only(left: 15.w),
                      child: Text(
                        "Segway Ninebot mini Pro智能代的卅饭店发生的范德萨地方撒发生",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(fontSize: 28.sp),
                      ))
                ],
              ),
              Row(
                children: <Widget>[
                  Text(
                    "+",
                    style: TextStyle(color: Colors.red),
                  ),
                  Text(
                    "¥8.00",
                    style: TextStyle(color: Colors.red, fontSize: 30.sp),
                  )
                ],
              )
            ],
          ),
          SizedBox(
            height: 4,
          ),
          Row(
            children: <Widget>[
              Text(
                "订单编号：",
                style: TextStyle(
                    color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
              ),
              Text(
                "1233333333",
                style: TextStyle(
                    color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
              ),
              SizedBox(
                width: 28.w,
              ),
              Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4),
                    color: HexColor.fromHex("#FFF5F5F5"),
                    border: Border.all(color: HexColor.fromHex("#FFD6D6D6"))),
                child: Padding(
                  padding:
                      EdgeInsets.only(left: 16, right: 16, top: 1, bottom: 1),
                  child: Text(
                    "复制",
                    style: TextStyle(
                        color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
                  ),
                ),
              )
            ],
          ),
          Row(
            children: <Widget>[
              Text(
                "订单编号：",
                style: TextStyle(
                    color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
              ),
              Text(
                "1233333333",
                style: TextStyle(
                    color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Text(
                "订单编号：",
                style: TextStyle(
                    color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
              ),
              Text(
                "1233333333",
                style: TextStyle(
                    color: HexColor.fromHex("#FF999999"), fontSize: 22.sp),
              ),

            ],
          )
        ],
      ),
    );
  }
}
