import 'package:zhiying_base_widget/pages/team_details_page/model/team_details_data_model.dart';
import 'package:zhiying_base_widget/pages/team_details_page/model/team_details_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter/material.dart';

class TeamDetailsMonthDataWidget extends StatelessWidget {
  TeamDetailsStyleModelDashbordItem styleModel;
  TeamDetailsDashBoardItem dataModel;

  TeamDetailsMonthDataWidget(this.styleModel, this.dataModel);

  @override
  Widget build(BuildContext context) {
    Logger.log('dataModel = ${dataModel?.toJson()?.toString()}');
    return _getContentWidget();
  }

  /// content
  Widget _getContentWidget() {
    return Container(
      decoration: BoxDecoration(
        color: HexColor.fromHex(styleModel?.bg_color ?? '#FFFFFF'),
        borderRadius: BorderRadius.circular(10),
      ),
      margin: const EdgeInsets.only(left: 12.5, right: 12.5, top: 12.5),
      padding: const EdgeInsets.only(bottom: 10),
      child: Column(children: <Widget>[
        Transform.translate(
          offset: Offset(0, -5),
          child: Container(
            alignment: Alignment.center,
            height: 30,
            width: 82,
            padding: const EdgeInsets.only(bottom: 8),
            decoration: BoxDecoration(image: DecorationImage(image: CachedNetworkImageProvider(styleModel?.title_bg_img ?? ''))),
            child: Text(
              styleModel?.title ?? '',
              style: TextStyle(color: HexColor.fromHex(styleModel?.title_color ?? '#FFFFFF'), fontSize: 13),
            ),
          ),
        ),
        const SizedBox(height: 6),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: _buildDataWidgets(),
        ),
      ]),
    );
  }

  /// 分割线
  Widget _buildVerticalDividerWidget() {
    return Container(width: 0.5, height: 18.5, child: VerticalDivider(width: 0.5, thickness: 0.5, color: HexColor.fromHex('#F2F2F2')));
  }

  /// 构建数据
  List<Widget> _buildDataWidgets() {
    List<Widget> widgets = [];
    int length = styleModel?.texts?.length ?? 0;
    int dataLength = dataModel?.data_list?.length ?? 0;
    if (length > 0) {
      for (int i = 0; i < length; i++) {
        String element = styleModel.texts[i];
        String value = (dataLength > 0 && dataLength == length) ? dataModel.data_list[i] : '0.0';
        widgets.add(_getValueWidget(element, value));
        widgets.add(_buildVerticalDividerWidget());
      }
      // styleModel.texts.forEach((element) {
      //   widgets.add(_getValueWidget(element, '0.0'));
      //   widgets.add(_buildVerticalDividerWidget());
      // });
      widgets.removeLast(); //去除最后一个分割线
    }
    return widgets;
  }

  /// 数据
  Widget _getValueWidget(String text, String value) {
    return _getCustomWidget(
      text: text ?? '',
      textColor: styleModel?.text_color ?? '#999999',
      textSize: 11,
      number: value ?? '0.0',
      numberColor: styleModel?.value_color ?? '#333333',
      numberSize: 17,
      icon: styleModel?.text_icon,
    );
  }

  /// 自定义Widget
  Widget _getCustomWidget({String text, String textColor, double textSize, String number, String numberColor, double numberSize, String icon}) {
    return Column(
      children: <Widget>[
        /// number
        Text(
          number,
          style: TextStyle(color: HexColor.fromHex(numberColor), fontSize: numberSize, fontWeight: FontWeight.bold, fontFamily: 'Din', package: 'zhiying_comm'),
        ),

        /// text
        Row(
          children: <Widget>[
            Text(text, style: TextStyle(color: HexColor.fromHex(textColor), fontSize: textSize)),

            /// icon
            Visibility(
                visible: !EmptyUtil.isEmpty(icon),
                child: Container(
                  margin: const EdgeInsets.only(left: 3),
                  width: 11,
                  height: 11,
                  child: CachedNetworkImage(
                    imageUrl: icon ?? '',
                  ),
                ))
          ],
        )
      ],
    );
  }
}
