import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appFontsize.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/mh_controller/message_controller.dart';
import 'package:vbvs_app/controller/mh_controller/mhdevice_share_controller.dart';
import 'package:vbvs_app/model/api_response.dart';

class MessageDetailPage extends StatefulWidget {
  final data;
  const MessageDetailPage({super.key, required this.data});

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

class _MessageDetailPageState extends State<MessageDetailPage> {
  MHDeviceShareController controller = Get.find();
  MhMessageController messageController = Get.find();
  @override
  Widget build(BuildContext context) {
    var messageInfo = widget.data;
    return LayoutBuilder(
        builder: (context, boxConstraints) => GestureDetector(
              onTap: () => FocusScope.of(context).unfocus(),
              child: Container(
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image:
                        AssetImage('assets/images/new_background.png'), // 本地图片
                    fit: BoxFit.fill, // 填满整个 Container
                  ),
                ),
                child: Scaffold(
                  appBar: AppBar(
                    backgroundColor: Colors.transparent,
                    automaticallyImplyLeading: false,
                    iconTheme:
                        IconThemeData(color: themeController.currentColor.sc3),
                    titleSpacing: 0,
                    title: SizedBox(
                      width: double.infinity,
                      height: 180.rpx,
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          // 中间居中的标题
                          Text(
                            '消息详情',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 30.rpx,
                            ),
                          ),
                          // 左侧图标
                          Positioned(
                            left: 0.rpx,
                            child: returnIconButtomNew,
                          ),
                        ],
                      ),
                    ),
                    actions: const [],
                    centerTitle: false,
                  ),
                  backgroundColor: Colors.transparent,
                  body: SafeArea(
                    top: true,
                    child: Padding(
                        padding: EdgeInsets.only(top: 30.rpx),
                        child: Column(
                          children: [
                            Padding(
                              padding:
                                  EdgeInsets.only(left: 30.rpx, right: 30.rpx),
                              child: Container(
                                  decoration: BoxDecoration(
                                    color: Color(0XFF003058),
                                    borderRadius: BorderRadius.circular(16.rpx),
                                  ),
                                  child: Padding(
                                      padding: EdgeInsets.only(
                                          left: 30.rpx,
                                          top: 20.rpx,
                                          bottom: 20.rpx),
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Container(
                                            width: double.infinity,
                                            constraints: BoxConstraints(
                                                minHeight: 66.rpx),
                                            child: Align(
                                              alignment:
                                                  AlignmentDirectional(-1, 0),
                                              child: Text(
                                                "${messageInfo['data']['title']}",
                                                style: TextStyle(
                                                  fontFamily: 'Inter',
                                                  fontSize: 30.rpx,
                                                  letterSpacing: 0.0,
                                                  color: themeController
                                                      .currentColor.sc3,
                                                ),
                                              ),
                                            ),
                                          ),
                                          Row(
                                            mainAxisSize: MainAxisSize.max,
                                            children: [
                                              Container(
                                                constraints: BoxConstraints(
                                                    minWidth: 30.rpx,
                                                    maxWidth: 140.rpx),
                                                child: Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: messageInfo['data']
                                                          ['val']
                                                      .map<Widget>((mapItem) =>
                                                          _buildInfoItem(
                                                              context,
                                                              mapItem['k'] ??
                                                                  ''))
                                                      .toList(),
                                                ),
                                              ),
                                              Container(
                                                constraints: BoxConstraints(
                                                    minWidth: 30.rpx),
                                                child: Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: messageInfo['data']
                                                          ['val']
                                                      .map<Widget>((mapItem) =>
                                                          _buildValueItem(
                                                              context,
                                                              mapItem['v'] ??
                                                                  ''))
                                                      .toList(),
                                                ),
                                              ),
                                            ].divide(SizedBox(width: 30.rpx)),
                                          ),
                                        ],
                                      ))),
                            ),
                            const Spacer(),
                            bottomIcon(context)
                          ],
                        )),
                  ),
                ),
              ),
            ));
  }

  Container bottomIcon(BuildContext context) {
    return Container(
      height: 120.rpx,
      decoration: BoxDecoration(
        color: Color(0xFF003058),
      ),
      width: double.infinity,
      child: TextButton(
        onPressed: () async {
          if (widget.data['status'] == 1) {
            ApiResponse apiResponse =
                await controller.confirmShare(widget.data['data']['shareCode']);
            if (apiResponse.code == HttpStatusCodes.ok) {
              TopSlideNotification.show(context,
                  text: apiResponse.msg!, textColor: Color(0xFF00C1AA));
              messageController.getMessageList();
              messageController.updateAll();
            } else {
              TopSlideNotification.show(context,
                  text: apiResponse.msg!, textColor: Color(0XFFFF7159));
              messageController.getMessageList();
              messageController.updateAll();
            }
          }
        },
        child: Text(
          getStatusText(widget.data['status']),
          style: TextStyle(
            fontFamily: 'Readex Pro',
            color: widget.data['data']['status'] == 1
                ? Colors.white
                : Color(0xFF929699),
            fontSize: 26.rpx,
            letterSpacing: 0,
          ),
        ),
      ),
    );
  }

  Widget _buildInfoItem(BuildContext context, String label) {
    return Container(
      constraints: BoxConstraints(
        minHeight: 62.rpx,
      ),
      child: Align(
        alignment: AlignmentDirectional(-1, 0),
        child: Text(
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          label.tr,
          style: TextStyle(
            fontFamily: 'Inter',
            fontSize: 26.rpx,
            letterSpacing: 0.0,
            color: themeController.currentColor.sc4,
          ),
        ),
      ),
    );
  }

  Widget _buildValueItem(BuildContext context, value) {
    return Container(
      constraints: BoxConstraints(
        minHeight: 62.rpx,
      ),
      child: Align(
        alignment: AlignmentDirectional(-1, 0),
        child: Text(
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          "${value}",
          style: TextStyle(
            fontFamily: 'Inter',
            fontSize: 26.rpx,
            letterSpacing: 0.0,
            color: themeController.currentColor.sc3,
          ),
        ),
      ),
    );
  }

  String getStatusText(int status) {
    switch (status) {
      case 1:
        return '同意';
      case 2:
        return '已同意';
      case 3:
        return '已过期';
      default:
        return '未知状态';
    }
  }
}
