/*
 * @Author: WuWei
 * @Date: 2020-07-02 09:15:01
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-05-25 15:31:38
 * @Description:
 */
import 'package:get/get.dart';
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:overdue_package/src/view_models/overdue_index_model.dart';

Map statusType = {
  'hangUp': '已挂起',
  'waitAccept': '待受理',
  'accept': '已受理',
  'solve': '已解决',
  'evaluate': '已评价',
  'reject': '已驳回',
  'end': '已结束',
  'cancel': '已取消'
};

final Color redColor = Color(0xFFF94F48);
final Color greenColor = Color(0xFF00BC84);
final Color yellowColor = Color(0xFFFD9025);
final Color blueColor = Color(0xFF4FA3FB);
final Color blueGreenColor = Color(0xFF1BBDDF);
final Color greyColor = Color(0xFF666666);

Map statusColor = {
  'waitAccept': redColor,
  'accept': yellowColor,
  'solve': greenColor,
  'evaluate': blueGreenColor,
  'hangUp': redColor,
  'cancel': greyColor,
  'reject': greyColor
};

Map businessColor = {
  'complaint': redColor,
  'praise': greenColor,
  'advice': blueColor,
  'suggest': blueGreenColor,
  'reject': greyColor,
};

class ListViewItem extends StatefulWidget {
  const ListViewItem({
    Key? key,
    required this.item,
    this.picture,
    this.isList = true,
  }) : super(key: key);

  final item;
  final picture;
  final bool isList;
  @override
  _ListViewItemState createState() => _ListViewItemState();
}

class _ListViewItemState extends State<ListViewItem> {
  final OverdueIndexStore store = Get.find<OverdueIndexStore>();
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    String phoneNumber = widget.item.mobile.toString();
    String replaceMoblie = widget.item?.mobile != null
        ? phoneNumber.length > 10
            ? phoneNumber.replaceRange(3, 7, '****')
            : phoneNumber.length > 5
                ? widget.item.mobile
                    .toString()
                    .replaceRange(3, phoneNumber.length - 3, '**')
                : phoneNumber
        : '';
    return WisCard(
      color: Colors.white,
      margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
      elevation: 0,
      child: Wisdom.column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Wisdom.column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 10.pt),
            mainAxisSize: MainAxisSize.min,
            children: [
              Text.rich(
                TextSpan(
                  children: [
                    WidgetSpan(
                        child: WTag(
                      child: Text(
                        '${widget.item?.statusName ?? ''}',
                        style:
                            TextStyle(color: statusColor[widget.item?.status]),
                      ),
                      color: statusColor[widget.item?.status],
                      margin: EdgeInsets.only(right: 10.pt),
                    )),
                    if (widget.item?.paidService != null
                        ? widget.item?.paidService
                        : false)
                      WidgetSpan(
                        child: WTag(
                          child: Text(
                            '有偿',
                            style: TextStyle(color: redColor),
                          ),
                          color: redColor,
                          margin: EdgeInsets.only(right: 10.pt),
                        ),
                      ),
                    if (widget.item?.tag == 'BLUE')
                      WidgetSpan(
                        child: WTag(
                          child: Text(
                            '${widget.item?.tagMsg}',
                            style: TextStyle(color: colorScheme.primary),
                          ),
                          color: colorScheme.primary,
                          margin: EdgeInsets.only(right: 10.pt),
                        ),
                      ),
                    if (widget.item?.tag == 'ORANGE')
                      WidgetSpan(
                        child: WTag(
                          child: Text(
                            '${widget.item?.tagMsg}',
                            style: TextStyle(
                              color: Colors.orange,
                            ),
                          ),
                          color: Colors.orange,
                          margin: EdgeInsets.only(right: 10.pt),
                        ),
                      ),
                    if (widget.item?.tag == 'RED')
                      WidgetSpan(
                        child: WTag(
                          child: Text(
                            '${widget.item?.tagMsg}',
                            style: TextStyle(color: colorScheme.red),
                          ),
                          color: colorScheme.red,
                          margin: EdgeInsets.only(right: 10.pt),
                        ),
                      ),
                    if (widget.item?.tag == 'YELLOW')
                      WidgetSpan(
                        child: WTag(
                          child: Text(
                            '${widget.item?.tagMsg}',
                            style: TextStyle(color: Colors.yellow[600]),
                          ),
                          color: Colors.yellow[600],
                          margin: EdgeInsets.only(right: 10.pt),
                        ),
                      ),
                    TextSpan(
                      text: '${widget.item?.description ?? ''}',
                      style: TextStyle(
                        fontSize: 16.pt,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
                maxLines: widget.isList == true ? 2 : 100,
                overflow: widget.isList == true ? TextOverflow.ellipsis : null,
              ),
            ],
          ),
          if (widget.picture != null)
            Wisdom.wrap(
              margin: EdgeInsets.symmetric(horizontal: 15.pt),
              alignment: Alignment.centerLeft,
              wrapAlignment: WrapAlignment.start,
              spacing: 15.pt,
              runSpacing: 15.pt,
              children: <Widget>[
                for (var item in widget.picture ?? [])
                  Wisdom.stack(
                    width: (MediaQuery.of(context).size.width - 95) / 3,
                    height: 65.pt,
                    border: Border.all(color: colorScheme.divider),
                    color: Colors.black,
                    fit: StackFit.expand,
                    clipBehavior: Clip.none,
                    children: <Widget>[
                      Wisdom(
                        onTap: () => WisPhotoViewUtil.show([
                          CachedNetworkImageProvider(
                            AppParseUtil.image(item),
                          )
                        ], 0),
                        child: WImage.network(
                          AppParseUtil.image(item),
                          width: ((MediaQuery.of(context).size.width - 95) / 3),
                          height: 61.pt,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ],
                  ),
              ],
            ),
          Wisdom(
            padding: EdgeInsets.symmetric(horizontal: 15.pt),
            child: Divider(
              color: Color(0xffECECEC),
              height: 20.pt,
            ),
          ),
          Wisdom.row(
            padding: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 10.pt),
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Wisdom.column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  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(
                  //   margin: EdgeInsets.symmetric(vertical: 5.pt),
                  //   child: Text(
                  //     '${widget?.item?.name ?? '-'}',
                  //     overflow: TextOverflow.ellipsis,
                  //     style: TextStyle(
                  //       color: Color(0xff222222),
                  //       fontSize: 14.pt,
                  //       fontWeight: FontWeight.w300,
                  //     ),
                  //   ),
                  // ),
                ],
              ),
              Wisdom.column(
                margin: EdgeInsets.only(left: 15.pt),
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      WTag(
                        child: Text(
                          '工单用时: '
                          '${store.getTimeConsuming(widget.item?.backTrackingAt ?? '')}',
                          style: TextStyle(
                            color: Colors.orange,
                          ),
                        ),
                        color: Colors.orange,
                        margin: EdgeInsets.only(right: 10.pt),
                      ),
                      Wisdom(
                        alignment: Alignment.centerRight,
                        margin: EdgeInsets.only(left: 19.pt),
                        onTap: () => launch('tel:${widget.item?.mobile ?? ''}'),
                        child: Text(
                          replaceMoblie,
                          style: TextStyle(
                            fontSize: 14.pt,
                            color: colorScheme.primary,
                          ),
                        ),
                      ),
                    ],
                  ),
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    margin: EdgeInsets.symmetric(vertical: 5.pt),
                    children: <Widget>[
                      Wisdom.row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Wisdom(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              '小区: ${widget.item?.communityName ?? '-'}',
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                color: Color(0xff222222),
                                fontSize: 14.pt,
                                fontWeight: FontWeight.w300,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Wisdom.row(
                        children: [
                          WTag(
                            plain: true,
                            child: Text(
                              '${widget.item?.businessTypeName ?? ''}',
                              style: TextStyle(
                                color: businessColor[widget.item?.businessType],
                              ),
                            ),
                            margin: EdgeInsets.only(right: 5.pt),
                            color: businessColor[widget.item?.businessType],
                          ),
                          if (widget.item?.sortName != null)
                            Text(
                              '${widget.item?.sortName ?? ''}',
                              style: TextStyle(
                                color: colorScheme.grey,
                                fontSize: 12.pt,
                              ),
                            ),
                        ],
                      ),
                    ],
                  ),
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Wisdom(
                        child: Text(
                          '房号: ${widget.item?.houseName ?? '-'}',
                          style: TextStyle(
                            color: Color(0xFFFF7531),
                            fontSize: 14.pt,
                            fontWeight: FontWeight.w300,
                          ),
                        ),
                      ),
                      Wisdom(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          "处理人: ${widget.item?.dealPerson != null && widget.item?.dealPerson != '' ? widget.item?.dealPerson : '  -'}",
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Color(0xff222222),
                            fontSize: 14.pt,
                            fontWeight: FontWeight.w300,
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ).asExpanded()
            ],
          ),
          if (widget.isList == true)
            Wisdom.row(
              padding: EdgeInsets.fromLTRB(15.pt, 10.pt, 15.pt, 10.pt),
              border: Border(
                top: BorderSide(
                  color: colorScheme.divider,
                  width: 0.5.pt,
                ),
              ),
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "内部分类: ${widget.item?.internalSortName != null && widget.item?.internalSortName != '' ? widget.item?.internalSortName : '  -'}",
                  style: TextStyle(
                    fontWeight: FontWeight.w300,
                    fontSize: 13.pt,
                    color: colorScheme.time,
                  ),
                ).nestExpanded(),
                Text(
                  '提报时间: ${widget.item?.backTrackingAt ?? '-'}',
                  style: TextStyle(
                    fontWeight: FontWeight.w300,
                    fontSize: 13.pt,
                    color: colorScheme.time,
                  ),
                )
              ],
            ),
        ],
      ),
    );
  }
}
