import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'inspection_history_state.dart';

class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
  InspectionHistoryCubit({required String deviceId})
      : super(InspectionHistoryState(
          deviceId: deviceId,
          totalCount: 0,
          passCount: 0,
          warningCount: 0,
          failCount: 0,
          records: [],
        )) {
    _loadHistoryData(deviceId);
  }

  void _loadHistoryData(String deviceId) {
    // TODO: 从API或本地数据库加载巡检历史数据
    // 这里使用模拟数据
    final mockRecords = [
      InspectionRecord(
        deviceName: '客厅主控网关',
        inspectorName: '张师傅',
        inspectionTime: '2024-01-15 14:30:00',
        result: '通过',
        remark: '设备运行正常，各项指标正常',
        items: [
          const InspectionItemRecord(name: '电源状态', result: '通过'),
          const InspectionItemRecord(name: '网络连接', result: '通过'),
          const InspectionItemRecord(name: 'CPU使用率', result: '通过'),
          const InspectionItemRecord(name: '内存占用', result: '通过'),
        ],
        icon: Icons.wifi,
      ),
      InspectionRecord(
        deviceName: '智能摄像头-门口',
        inspectorName: '王师傅',
        inspectionTime: '2024-01-15 10:00:00',
        result: '警告',
        remark: '画面有轻微噪点，可能需要清洁镜头',
        items: [
          const InspectionItemRecord(name: '画面清晰度', result: '警告'),
          const InspectionItemRecord(name: '夜视功能', result: '通过'),
          const InspectionItemRecord(name: '录像存储', result: '通过'),
          const InspectionItemRecord(name: '云台控制', result: '通过'),
        ],
        icon: Icons.videocam,
      ),
      InspectionRecord(
        deviceName: '厨房智能插座',
        inspectorName: '李师傅',
        inspectionTime: '2024-01-15 10:00:00',
        result: '不通过',
        remark: '插座过热保护触发，需更换',
        items: [
          const InspectionItemRecord(name: '温度检测', result: '不通过'),
          const InspectionItemRecord(name: '负载功率', result: '警告'),
          const InspectionItemRecord(name: '过流保护', result: '通过'),
          const InspectionItemRecord(name: '电源开关', result: '通过'),
        ],
        icon: Icons.power,
      ),
    ];

    final mockState = InspectionHistoryState(
      deviceId: deviceId,
      totalCount: mockRecords.length,
      passCount: mockRecords.where((r) => r.result == '通过').length,
      warningCount: mockRecords.where((r) => r.result == '警告').length,
      failCount: mockRecords.where((r) => r.result == '不通过').length,
      records: mockRecords,
    );

    emit(mockState);
  }
}
