//import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hyzp_ybqx/components/commonFun.dart';
import 'package:hyzp_ybqx/components/hyxx_data_handle.dart';
import 'package:hyzp_ybqx/pages/Works/TJXX/tj_data.dart';
import 'package:hyzp_ybqx/pages/Works/TJXX/zptj_bar_echarts_trinity_new_getdata.dart';
import 'package:hyzp_ybqx/widget/table_scrollable/table_scrollable.dart';

import '../../../services/EventBus.dart';

class ZptjBarEchartsTrinityNew extends StatefulWidget {
  ZptjBarEchartsTrinityNew({this.statisType, Key key}) : super(key: key);
  String statisType; //统计类型

  @override
  State<StatefulWidget> createState() => ZptjBarEchartsTrinityNewState();
}

class ZptjBarEchartsTrinityNewState extends State<ZptjBarEchartsTrinityNew> {
  //try_setState();   //避免异常报错
  try_setState() {
    try {
      setState(() {});
    } catch (e) {
      print('setState(() {})异常：${e}');
    }
  }

  @override
  void initState() {
    super.initState();

    print('cllRStatisDataOk = ${cllRStatisDataOk}');

    ///获取点位信息数据
    if (!cllRStatisDataOk) {
      ///车流量日统计数据类
      trinityData = ZptjBarEchartsTrinityNewGetData(); //提取获取车流量日统计数据
    } else {
      updateEchartsAndTable(); //刷新页面数据
    }

    //监听 选择LED点位 更新事件
    eventBus.on<SelectLedDwUpdateEvent>().listen((event) async {
      print(event.str);

      //获取用户选择项对应的id号
      int id = trinityData.listItems.indexOf(event.selectedValue);
      print('SelectLedDwUpdateEvent: id = ${id}, nDwbh = ${trinityData.nDwbh}');
      if (id != trinityData.nDwbh) {
        trinityData.nDwbh = id;
        updateEchartsAndTable(bFirst: false); //刷新页面数据
      }
    });
  }

  Future updateEchartsAndTable({bool bFirst = true}) {
    trinityData.replaceEchartsOptionData(bFirst: bFirst).then((value) {
      cllRStatisDataOk = true;
      try_setState();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(ScreenUtil().setHeight(173)), // 设置appBar高度
        child: AppBar(
          automaticallyImplyLeading: false,
          centerTitle: true,
          titleSpacing: 0.0,
          flexibleSpace: Container(
            padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight), //留出顶部状态栏高度
            child: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [
                    Color.fromRGBO(12, 186, 156, 1),
                    Color.fromRGBO(39, 127, 235, 1),
                  ],
                ),
              ),
            ),
          ),
          title: Padding(
            padding: EdgeInsets.only(left: 0, right: 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                getIconAndTextButton(
                  iconColor: Colors.white,
                  iconData: Icons.chevron_left_outlined,
                  onPress: () {
                    Navigator.pop(context);
                  },
                ),
                Expanded(
                    child: RichText(
                  textAlign: TextAlign.center,
                  overflow: TextOverflow.ellipsis,
                  text: TextSpan(children: [
                    TextSpan(
                      text: mapStatisType[widget.statisType]['text'],
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                    TextSpan(
                        text: '\n' + mapStatisType[widget.statisType]['textTerm'],
                        style: TextStyle(fontSize: 14.0)),
                  ]),
                )),
                SizedBox(width: 50),
              ],
            ),
          ),
        ),
      ),
      body: (listCllrtjStatis.isEmpty ||
              trinityData.strEchartsOptionNew.isEmpty ||
              trinityData.mapAllDw.length < 31)
          ? getMoreWidget(
              color: Colors.black38,
              text: '正在下载大量数据，\n请耐心等待...',
              size: 25,
              textAlign: TextAlign.center)
          : Column(
              children: [
                SizedBox(height: ScreenUtil().setHeight(20)),
                //2、得到统计图表组件
                trinityData.getBarEcharts(trinityData.strEchartsOptionNew),
                //SizedBox(height: ScreenUtil().setHeight(0)),
                //3、得到数据表组件
                TableScrollable(mapTableData: trinityData.mapAllDw),
              ],
            ),
    );
  }
}
