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) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.r),
      ),
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
      margin: EdgeInsets.only(bottom: 20.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  Container(
                    width: 60.w,
                    height: 60.h,
                    decoration: BoxDecoration(
                      color: const Color.fromRGBO(242, 243, 245, 1.0),
                      borderRadius: BorderRadius.circular(8.r),
                    ),
                    child: Icon(
                      record.icon,
                      color: const Color.fromRGBO(100, 116, 139, 1.0),
                      size: 32.sp,
                    ),
                  ),
                  SizedBox(width: 12.w),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        record.deviceName,
                        style: TextStyle(
                          fontSize: 26.sp,
                          fontWeight: FontWeight.bold,
                          color: const Color.fromRGBO(10, 13, 20, 1.0),
                        ),
                      ),
                      SizedBox(height: 8.h),
                      Row(
                        children: [
                          Icon(
                            Icons.person,
                            color: const Color.fromRGBO(66, 165, 245, 1.0),
                            size: 18.sp,
                          ),
                          SizedBox(width: 4.w),
                          Text(
                            record.inspectorName,
                            style: TextStyle(
                              fontSize: 22.sp,
                              color: const Color.fromRGBO(100, 116, 139, 1.0),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 6.h),
                decoration: BoxDecoration(
                  color: _getResultColor(record.result).withOpacity(0.1),
                  borderRadius: BorderRadius.circular(10.r),
                  border: Border.all(
                    color: _getResultColor(record.result),
                    width: 1.w,
                  ),
                ),
                child: Row(
                  children: [
                    Icon(
                      _getResultIcon(record.result),
                      color: _getResultColor(record.result),
                      size: 18.sp,
                    ),
                    SizedBox(width: 4.w),
                    Text(
                      record.result,
                      style: TextStyle(
                        fontSize: 22.sp,
                        color: _getResultColor(record.result),
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: 12.h),
          Row(
            children: [
              Icon(
                Icons.access_time,
                color: const Color.fromRGBO(100, 116, 139, 1.0),
                size: 20.sp,
              ),
              SizedBox(width: 4.w),
              Text(
                record.inspectionTime,
                style: TextStyle(
                  fontSize: 22.sp,
                  color: const Color.fromRGBO(100, 116, 139, 1.0),
                ),
              ),
            ],
          ),
          SizedBox(height: 16.h),
          Container(
            width: double.infinity,
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
            decoration: BoxDecoration(
              color: const Color.fromRGBO(242, 243, 245, 1.0),
              borderRadius: BorderRadius.circular(12.r),
            ),
            child: Text(
              record.remark,
              style: TextStyle(
                fontSize: 22.sp,
                color: const Color.fromRGBO(100, 116, 139, 1.0),
              ),
            ),
          ),
          SizedBox(height: 16.h),
          Divider(
            color: const Color.fromRGBO(242, 243, 245, 1.0),
            height: 1.h,
          ),
          SizedBox(height: 16.h),
          Text(
            '巡检项目',
            style: TextStyle(
              fontSize: 24.sp,
              fontWeight: FontWeight.bold,
              color: const Color.fromRGBO(71, 85, 105, 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(item.result);

    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: Color.fromRGBO(209, 250, 229, 1.0), // resultColor.withValues(alpha: 0.18),
        borderRadius: BorderRadius.circular(18.r),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            _getResultIcon(item.result),
            color: resultColor,
            size: 18.sp,
          ),
          SizedBox(width: 6.w),
          Text(
            item.name,
            style: TextStyle(
              fontSize: 22.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;
    }
  }
}
