import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/feedback_page/model/feedback_record_data_model.dart';
import 'package:zhiying_base_widget/pages/feedback_page/model/feedback_record_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class FeedbackRecordItem extends StatelessWidget {
  FeedbackRecordStyleModel styleModel;
  FeedbackRecordDataItemModel dataModel;

  FeedbackRecordItem(this.styleModel, this.dataModel);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 12.5, right: 12.5, top: 4, bottom: 4),
      padding: EdgeInsets.all(15),
      width: double.infinity,
      decoration: BoxDecoration(
        color: HexColor.fromHex(styleModel?.itemStyle?.bgColor ?? '#FFFFFF'),
        borderRadius: BorderRadius.circular(7.5),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                _createTitle(),
                _createDesc(),
                _createImage(),
                _createTime(),
              ],
            ),
          ),
          Container(
            width: 60,
            height: 60,
            decoration: BoxDecoration(
              // color: Colors.green,
              borderRadius: BorderRadius.circular(30),
            ),
            child: CachedNetworkImage(
              imageUrl: getStateImageUrl(),
            ),
          )
        ],
      ),
    );
  }

  String getStateImageUrl(){
    String result = '';
    int length = styleModel?.stateImages?.length ?? 0;
    if(length > 0){
      styleModel.stateImages.forEach((element) {
        if(element.stateId == dataModel.state){
          result  = element.img;
        }
      });
    }
    return result;
  }

  Widget _createTitle() {
    List<InlineSpan> list = List();

    list.add(WidgetSpan(
      child: Container(
        padding: EdgeInsets.only(left: 4.5, right: 4.5, top: 3, bottom: 3),
        margin: EdgeInsets.only(right: 4),
        child: Text(
          dataModel?.typeText ?? '其它',
          style: TextStyle(
            fontSize: 9,
            height: 1,
            color: HexColor.fromHex(styleModel?.itemStyle?.typeTextColor ?? '#FFFFFF'),
          ),
        ),
        decoration: BoxDecoration(color: HexColor.fromHex(styleModel?.itemStyle?.typeBgColor ?? '#FF4242'), borderRadius: BorderRadius.circular(2.5)),
      ),
    ));

    list.add(TextSpan(
      text: dataModel?.title ?? '',
      style: TextStyle(fontSize: 14, color: HexColor.fromHex(styleModel?.itemStyle?.titleTextColor ?? '#333333'), fontWeight: FontWeight.bold),
    ));
    return RichText(
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(children: list),
    );
  }

  Widget _createDesc() {
    return Container(
      child: Text(
        dataModel?.content ?? '',
        style: TextStyle(fontSize: 11, color: HexColor.fromHex(styleModel?.itemStyle?.contentTextColor ?? '#999999')),
      ),
    );
  }

  Widget _createImage() {
    List<Widget> images = [];
    int length = dataModel?.imageList?.length ?? 0;
    if (length > 0) {
      for (int i = 0; i < length; i++) {
        images.add(
          Container(
            width: 60,
            height: 60,
            child: CachedNetworkImage(
              imageUrl: dataModel?.imageList[i] ?? '',
            ),
          ),
        );
      }

      return Container(
        margin: EdgeInsets.only(top: 4, bottom: 4),
        child: Wrap(
          children: images,
        ),
      );
    } else {
      return Container();
    }
  }

  Widget _createTime() {
    return Container(
      child: Text(
        dataModel?.createTime ?? '',
        style: TextStyle(fontSize: 11, color: HexColor.fromHex(styleModel?.itemStyle?.timeTextColor ?? '#D8D8D8')),
      ),
    );
  }
}
