/*
 * @Author: WuWei
 * @Date: 2020-07-24 09:19:08
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-30 17:41:36
 * @Description: 咨询组件
 */
part of '../index.dart';

Map businessColor = {
  'complaint': Color(0xFFF94F48),
  'praise': Color(0xFF0FBC5E),
  'advice': Color(0xFF0091ec),
  'suggest': Color(0xFF07d1d4),
};

class _TabConsultComponent extends StatefulWidget {
  _TabConsultComponent({Key? key}) : super(key: key);

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

class __TabConsultComponentState extends State<_TabConsultComponent> {
  @override
  Widget build(BuildContext context) {
    // final style = WTheme.of(context);final colorScheme = style.colorScheme;
    PrivateReportDetailStore _store;
    _store = Provider.of<PrivateReportDetailStore>(context);
    return WisScrollView(
      key: _store.reportConsultKey,
      onInit: () => _store.getReportDialog(),
      onRefresh: () => _store.getReportDialog(),
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: Wisdom.column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              _DialogInfoCard(),
            ],
          ),
        ),
        _DialogList(),
      ],
    );
  }
}

class _DialogList extends StatelessWidget {
  const _DialogList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    PrivateReportDetailStore _store;
    _store = Provider.of<PrivateReportDetailStore>(context);
    ReportDialogModel? detail = _store.reportDialog;
    // final style = WTheme.of(context);final colorScheme = style.colorScheme;
    return WisSliverList.childBuilder(
        childCount: detail?.dialogs?.length ?? 0,
        builder: (_, i) {
          var dialogblock = detail!.dialogs![i];
          return Wisdom.column(
            mainAxisSize: MainAxisSize.min,
            padding: EdgeInsets.all(15.pt),
            crossAxisAlignment: CrossAxisAlignment.stretch,
            margin: i == (detail.dialogs?.length ?? 0) - 1
                ? EdgeInsets.only(bottom: 70.pt)
                : EdgeInsets.only(bottom: 0),
            children: <Widget>[
              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.center,
                margin: EdgeInsets.only(bottom: 20.pt),
                children: <Widget>[
                  Text(
                    '${dialogblock.createAt ?? ''}',
                    style: TextStyle(
                      color: Color(0xffb3b2b2),
                      fontSize: 13.pt,
                    ),
                  )
                ],
              ),
              for (var item in dialogblock.reportDialogsList ?? [])
                _DialogCard(item: item)
            ],
          );
        });
  }
}

class _DialogCard extends StatelessWidget {
  const _DialogCard({
    Key? key,
    required this.item,
  }) : super(key: key);
  final item;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    return Wisdom.row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment:
          item.dialogFrom == 'property' || item.dialogFrom == 'current'
              ? MainAxisAlignment.end
              : MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      margin: EdgeInsets.only(bottom: 15.pt),
      children: <Widget>[
        if (item.dialogFrom == 'resident') ...[
          WImage.network(
            'https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/660532763600990208',
            width: 42.pt,
            height: 42.pt,
            fit: BoxFit.fill,
            borderRadius: BorderRadius.circular(100.pt),
          ),
        ],
        Wisdom.column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment:
              item.dialogFrom == 'property' || item.dialogFrom == 'current'
                  ? CrossAxisAlignment.end
                  : CrossAxisAlignment.start,
          margin: item.dialogFrom == 'property' || item.dialogFrom == 'current'
              ? EdgeInsets.only(right: 8.pt)
              : EdgeInsets.only(left: 8.pt),
          children: <Widget>[
            Text(
              '${item?.userName ?? ''}',
              style: TextStyle(
                color: Color(0xff999999),
                fontSize: 11.pt,
              ),
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                if (item.dialogFrom == 'property' ||
                    item.dialogFrom == 'current') ...[
                  if (item.statusName == 'read') ...[
                    WisText('已读', color: colorScheme.grey, size: 13.pt),
                    SizedBox(width: 7.pt),
                  ] else ...[
                    WisText('未读', color: colorScheme.mainTitle, size: 13.pt),
                    SizedBox(width: 7.pt),
                  ],
                ],
                Wisdom(
                  maxWidth: 221.pt,
                  margin: EdgeInsets.only(top: 5.pt),
                  padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 15.pt, 10.pt),
                  borderRadius: BorderRadius.only(
                    topLeft: item.dialogFrom == 'property' ||
                            item.dialogFrom == 'current'
                        ? Radius.circular(15.pt)
                        : Radius.circular(0),
                    topRight: item.dialogFrom == 'property' ||
                            item.dialogFrom == 'current'
                        ? Radius.circular(0)
                        : Radius.circular(15.pt),
                    bottomLeft: Radius.circular(15.pt),
                    bottomRight: Radius.circular(15.pt),
                  ),
                  color: item.dialogFrom == 'property' ||
                          item.dialogFrom == 'current'
                      ? colorScheme.primary
                      : Colors.white,
                  child: item?.dialogType == 'text'
                      ? Text(
                          '''${item?.dialog}''',
                          softWrap: true,
                          style: TextStyle(
                              color: item.dialogFrom == 'property' ||
                                      item.dialogFrom == 'current'
                                  ? Colors.white
                                  : Color(0xff585858),
                              fontSize: 15.pt),
                        )
                      : Wisdom(
                          onTap: () => WisPhotoViewUtil.show([
                            CachedNetworkImageProvider(
                              AppParseUtil.image(item?.dialog),
                            )
                          ], 0),
                          child: WImage.network(
                            AppParseUtil.image(item?.dialog),
                            height: 60.pt,
                            width: 60.pt,
                            fit: BoxFit.fill,
                          ),
                        ),
                ),
                if (item.dialogFrom == 'resident') ...[
                  if (item.statusName == 'read') ...[
                    SizedBox(width: 7.pt),
                    WisText('已读', color: colorScheme.grey, size: 13.pt),
                  ] else ...[
                    SizedBox(width: 7.pt),
                    WisText('未读', color: colorScheme.mainTitle, size: 13.pt),
                  ],
                ],
              ],
            )
          ],
        ),
        if (item.dialogFrom == 'property' || item.dialogFrom == 'current') ...[
          WImage.network(
            UrlFunc.urlHasMatch(item.headPortrait)
                ? item?.headPortrait
                : AppParseUtil.image('${item?.headPortrait}'),
            height: 40.pt,
            width: 40.pt,
            fit: BoxFit.fill,
            borderRadius: BorderRadius.circular(4.0),
          ),
        ]
      ],
    );
  }
}

class _DialogInfoCard extends StatelessWidget {
  const _DialogInfoCard({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    PrivateReportDetailStore _store;
    _store = Provider.of<PrivateReportDetailStore>(context);
    ReportDetailModel? detail = _store.reportDetail;
    final listViewItem = ListViewItem(
      item: detail,
      picture: detail?.photos,
      isList: false,
    );
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
      child: listViewItem,
    );
  }
}
