import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../cubit/inspection_history_state.dart';

class InspectionRecordItem extends StatelessWidget {
  final InspectionRecord record;

  const InspectionRecordItem({
    super.key,
    required this.record,
  });

  @override
  Widget build(BuildContext context) {
    final resultColor = _getResultColor(record.result);

    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.r),
      ),
      padding: EdgeInsets.symmetric(horizontal: 28.w, vertical: 28.h),
      margin: EdgeInsets.only(bottom: 20.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                width: 80.w,
                height: 64.h,
                decoration: BoxDecoration(
                  color: const Color.fromRGBO(229, 241, 255, 1.0),
                  borderRadius: BorderRadius.circular(8.r),
                ),
                child: Icon(
                  record.icon,
                  color: const Color.fromRGBO(73, 149, 234, 1.0),
                  size: 32.sp,
                ),
              ),
              SizedBox(width: 20.w),
              Expanded(
                  child: Column(
                    spacing: 10.h,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            record.deviceName,
                            style: TextStyle(
                              fontSize: 26.sp,
                              fontWeight: FontWeight.bold,
                              color: const Color.fromRGBO(10, 13, 20, 1.0),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
                            decoration: BoxDecoration(
                              color: resultColor.withValues(alpha: 0.1),
                              borderRadius: BorderRadius.circular(40.r),
                            ),
                            child: Row(
                              children: [
                                Icon(
                                  _getResultIcon(record.result),
                                  color: resultColor,
                                  size: 24.sp,
                                ),
                                SizedBox(width: 4.w),
                                Text(
                                  record.result,
                                  style: TextStyle(
                                    fontSize: 24.sp,
                                    color: resultColor,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            spacing: 4.w,
                            children: [
                              Container(
                                width: 24.w,
                                height: 24.h,
                                decoration: const BoxDecoration(
                                  color: Color.fromRGBO(73, 149, 234, 1),
                                  shape: BoxShape.circle,
                                ),
                                child: Icon(
                                  Icons.person,
                                  color: Colors.white,
                                  size: 22.sp,
                                ),
                              ),
                              Text(
                                record.inspectorName,
                                style: TextStyle(
                                  fontSize: 24.sp,
                                  color: const Color.fromRGBO(100, 116, 139, 1.0),
                                ),
                              ),
                            ],
                          ),
                          Row(
                            children: [
                              Icon(
                                Icons.access_time,
                                color: const Color.fromRGBO(100, 116, 139, 1.0),
                                size: 24.sp,
                              ),
                              SizedBox(width: 4.w),
                              Text(
                                record.inspectionTime,
                                style: TextStyle(
                                  fontSize: 24.sp,
                                  color: const Color.fromRGBO(100, 116, 139, 1.0),
                                ),
                              ),
                            ],
                          ),
                        ],
                      )
                    ],
                  )
              ),
            ],
          ),
          SizedBox(height: 20.h),
          Container(
            width: double.infinity,
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
            decoration: BoxDecoration(
              color: const Color.fromRGBO(242, 243, 245, 1.0),
              borderRadius: BorderRadius.circular(12.r),
            ),
            child: Text(
              record.remark,
              style: TextStyle(
                fontSize: 24.sp,
                color: const Color.fromRGBO(100, 116, 139, 1.0),
              ),
            ),
          ),
          SizedBox(height: 20.h),
          Divider(
            height: 1.h,
            thickness: 0.5,
            color: const Color.fromRGBO(100, 116, 139, 0.2),
          ),
          SizedBox(height: 20.h),
          Text(
            '巡检项目',
            style: TextStyle(
              fontSize: 24.sp,
              fontWeight: FontWeight.bold,
              color: const Color.fromRGBO(100, 116, 139, 1.0),
            ),
          ),
          SizedBox(height: 12.h),
          Wrap(
            spacing: 8.w,
            runSpacing: 8.h,
            children: [
              ...record.items.take(3).map((item) => _buildItemTag(item)),
              if (record.items.length > 3)
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.h),
                  decoration: BoxDecoration(
                    color: const Color.fromRGBO(242, 245, 249, 1.0),
                    borderRadius: BorderRadius.circular(18.r),
                  ),
                  child: Text(
                    '+${record.items.length - 3}',
                    style: TextStyle(
                      fontSize: 22.sp,
                      color: const Color.fromRGBO(100, 116, 139, 1.0),
                    ),
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildItemTag(InspectionItemRecord item) {
    final resultColor = _getResultColor(record.result);

    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: resultColor.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(40.r),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        spacing: 6.w,
        children: [
          Icon(
            _getResultIcon(record.result),
            color: resultColor,
            size: 24.sp,
          ),
          Text(
            item.name,
            style: TextStyle(
              fontSize: 24.sp,
              color: resultColor,
            ),
          ),
        ],
      ),
    );
  }

  Color _getResultColor(String result) {
    switch (result) {
      case '通过':
        return const Color.fromRGBO(26, 188, 156, 1.0);
      case '警告':
        return const Color.fromRGBO(247, 184, 75, 1.0);
      case '不通过':
        return const Color.fromRGBO(239, 68, 68, 1.0);
      default:
        return const Color.fromRGBO(100, 116, 139, 1.0);
    }
  }

  IconData _getResultIcon(String result) {
    switch (result) {
      case '通过':
        return Icons.check;
      case '警告':
        return Icons.warning_amber_outlined;
      case '不通过':
        return Icons.close;
      default:
        return Icons.help;
    }
  }
}
