import 'package:chicken_farm/apis/index.dart'; import 'package:chicken_farm/components/vb_rfid_field.dart'; import 'package:chicken_farm/modes/rfid/rfid_model.dart'; import 'package:flutter/material.dart'; import 'package:chicken_farm/components/vb_app_bar.dart'; import 'package:chicken_farm/components/vb_dict_select.dart'; import 'package:chicken_farm/core/utils/toast.dart'; class BatchCullingPage extends StatefulWidget { const BatchCullingPage({super.key}); @override State createState() => _BatchCullingPageState(); } class _BatchCullingPageState extends State { final List _rfids = []; String? _cullReason; String? _disposalMethod; @override Widget build(BuildContext context) { return Scaffold( appBar: const VberAppBar(title: '批量淘汰', showLeftButton: true), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 电子编号区域 _buildRfidSection(), const SizedBox(height: 20), // 淘汰原因 _buildCullReasonSection(), const SizedBox(height: 20), // 处置方式 _buildDisposalMethodSection(), const SizedBox(height: 20), // 提交按钮 SizedBox( width: double.infinity, child: ElevatedButton( onPressed: _rfids.isNotEmpty && _cullReason != null && _disposalMethod != null ? _handleSubmit : null, style: ElevatedButton.styleFrom( backgroundColor: _rfids.isNotEmpty && _cullReason != null && _disposalMethod != null ? Colors.blue : Colors.grey, foregroundColor: Colors.white, ), child: const Text('提交'), ), ), const SizedBox(height: 20), // 已识别的电子编号列表 if (_rfids.isNotEmpty) ...[ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( '已识别的电子编号', style: TextStyle(fontWeight: FontWeight.bold), ), IconButton( icon: const Icon(Icons.clear, size: 18), onPressed: _clearRfids, tooltip: '清空编号', ), ], ), const SizedBox(height: 10), Expanded( child: Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(8), ), child: ListView.builder( padding: EdgeInsets.zero, itemCount: _rfids.length, itemBuilder: (context, index) { return ListTile( visualDensity: VisualDensity.compact, contentPadding: const EdgeInsets.symmetric( horizontal: 2, vertical: 0, ), title: Text(_rfids[index]), trailing: IconButton( icon: const Icon( Icons.delete, size: 18, color: Colors.red, ), onPressed: () => _removeRfid(index), ), ); }, ), ), ), ], const SizedBox(height: 20), ], ), ), ); } Widget _buildRfidSection() { return VberRfidField( rfids: _rfids, onRfidsScanned: (List scannedRfids) { // 过滤出未存在的RFID final newRfids = scannedRfids .where((rfid) => !_rfids.contains(rfid.uid)) .toList(); if (newRfids.isNotEmpty) { setState(() { // 将新的RFID添加到列表中 for (var rfid in newRfids) { _rfids.insert(0, rfid.uid); } }); ToastUtil.success("新增 ${newRfids.length} 枚电子编号"); } else { // 所有RFID都已存在 ToastUtil.info("电子编号已存在"); } }, multiple: true, label: '电子编号', multiplePlaceholder: '未识别', multipleFormat: '已识别 %d 枚电子编号', ); } Widget _buildCullReasonSection() { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('淘汰原因', style: TextStyle(fontWeight: FontWeight.bold)), const SizedBox(height: 10), Container( padding: const EdgeInsets.fromLTRB(16, 2, 16, 2), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(8), ), child: VberDictSelect( dictType: 'chicken_cull_reason', value: _cullReason, onChanged: (value) { setState(() { _cullReason = value; }); }, hint: '请选择淘汰原因', hideUnderline: true, ), ), ], ); } Widget _buildDisposalMethodSection() { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('处置方式', style: TextStyle(fontWeight: FontWeight.bold)), const SizedBox(height: 10), Container( padding: const EdgeInsets.fromLTRB(16, 2, 16, 2), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(8), ), child: VberDictSelect( dictType: 'chicken_disposal_method', value: _disposalMethod, onChanged: (value) { setState(() { _disposalMethod = value; }); }, hint: '请选择处置方式', hideUnderline: true, ), ), ], ); } // 移除指定索引的电子编号 void _removeRfid(int index) { setState(() { _rfids.removeAt(index); }); } // 清空所有已识别的电子编号 void _clearRfids() { setState(() { _rfids.clear(); }); ToastUtil.info('已清空所有电子编号'); } // 提交数据 void _handleSubmit() { final data = { 'rfids': _rfids, 'disposal_method': _disposalMethod, 'cull_reason': _cullReason, }; apis.breeding.submitApi .weight(data) .then((_) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('批量淘汰提交成功'), backgroundColor: Colors.green, ), ); // 提交后重置表单 setState(() { _rfids.clear(); _disposalMethod = null; _cullReason = null; }); } }) .catchError((err) { ToastUtil.error('批量淘汰提交失败'); if (mounted && err != null) { String errorMessage = err.toString(); if (err is Exception) { errorMessage = err.toString(); } ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(errorMessage), backgroundColor: Colors.red, ), ); } }); } }