import 'dart:convert'; import 'dart:math'; import 'package:bruno/bruno.dart'; import 'package:flutter/material.dart'; import 'package:wms_app/api_client/stock_in.dart'; import 'package:wms_app/model/bo/stock_in_data_xugong.dart'; import '../../common/colorCom.dart'; import '../../utils/dialogUtils.dart'; import 'package:logger/logger.dart'; import 'package:uuid/uuid.dart'; class StockInBuy extends StatefulWidget { const StockInBuy({super.key}); @override State createState() => _StockInBuyState(); } class _StockInBuyState extends State { List stockInDataXuGong = []; static var logger = Logger( printer: PrettyPrinter(), ); static var uuidGen = Uuid(); @override void initState() { super.initState(); _fetchOrderData(); } void _fetchOrderData() { _fetchGoodsData(); logger.e("yuqili1 ${stockInDataXuGong}"); } Future _fetchGoodsData() async { final response = await StockInApi.getAppOrderGoodsList(); if (response["code"] != 200) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showWarningMessage(thisContext, "警告", "服务器请求失败", btnLabel: "我知道了"); } return; } final data = jsonDecode(response["data"]); logger.e("yuqili2 $data"); final List res = data["rows"]; stockInDataXuGong = res.map((item) => StockInDataXuGong.fromJson(item)).toList(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( iconTheme: const IconThemeData(color: Colors.white), leading: IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: const Icon(Icons.arrow_back), ), centerTitle: true, backgroundColor: ColorCommon.colorScheme, title: const Text( "采购有货入库", style: TextStyle(color: Colors.white), ), ), body: FutureBuilder( future: _fetchGoodsData(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); } else if (snapshot.hasError) { return Center(child: Text("请求发生错误: ${snapshot.error}")); } else { return Center( child: Padding( padding: const EdgeInsets.only(top: 5, left: 10, right: 10), child: Column( children: [ Expanded( child: ListView.builder( itemCount: stockInDataXuGong.length, itemBuilder: (context, index) { final item = stockInDataXuGong[index]; return Card( margin: const EdgeInsets.symmetric(vertical: 5), child: ListTile( onTap: () => _editGood(item), title: Row( children: [ Expanded(child: Text("物料号: ${item.goodsId}")), Expanded(child: Text("数量: ${item.goodsNum}")), Expanded(child: Text("描述: ${item.goodsDesc}")), Expanded(child: Text("重量: ${item.weight} KG")), Expanded(child: Text("尺寸: ${item.size}")), IconButton( icon: const Icon(Icons.add), onPressed: () { _addMaterial(item); }, ), IconButton( icon: const Icon(Icons.edit), onPressed: () { _editGood(item); }, ), ], ), ), ); }, ), ), Padding( padding: const EdgeInsets.all(10), child: ElevatedButton( onPressed: _completeStockIn, child: const Text("码盘完成"), ), ), ], ), ), ); } }, ), ); } void _editGood(StockInDataXuGong item) async { final updatedItem = await Navigator.of(context).push( MaterialPageRoute( builder: (context) => EditStockInPage(item: item), ), ); if (updatedItem != null) { setState(() { final index = stockInDataXuGong.indexWhere((e) => e.goodsId == item.goodsId); if (index != -1) { stockInDataXuGong[index] = updatedItem; // 更新列表 } }); } } void _addMaterial(StockInDataXuGong item) { StockInApi.addStockIn(item).then((response) { logger.e("yuqili $response"); if (response["code"] != 200) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showWarningMessage(thisContext, "警告", "服务器请求失败", btnLabel: "我知道了"); } return; } final data = Map.from(jsonDecode(response["data"])); if (data["code"] == 200) { DialogUtils.showSuccessMessage(context, "成功", "添加入库", btnLabel: "我知道了"); return; } }).catchError((err) { var thisContext = context; if (thisContext.mounted) { DialogUtils.showErrorMessage( thisContext, "请求发生错误", "请求服务器发生错误:${err.toString()}", btnLabel: "我知道了"); } return; }); } void _completeStockIn() { if (stockInDataXuGong.isEmpty) { DialogUtils.showWarningMessage(context, "警告", "您的码盘数据为空", btnLabel: "确定"); return; } DialogUtils.showConfirmMessage(context, "码盘完成", "是否完成入库?", confirmBtn: "完成", confirm: () { BrnLoadingDialog.show(context, content: "正在请求入库"); StockInApi.addCompleted(uuidGen.v4()).then((response) { logger.e("yuqili $response"); if (response["code"] != 200) { DialogUtils.showWarningMessage(context, "警告", "服务器请求失败", btnLabel: "我知道了"); return; } final data = Map.from(jsonDecode(response["data"])); if (data["code"] == 200) { setState(() { stockInDataXuGong.clear(); }); DialogUtils.showSuccessMessage(context, "码盘成功", "", btnLabel: "我知道了"); } else { DialogUtils.showWarningMessage( context, "警告", "服务器返回失败:${data["message"]}", btnLabel: "我知道了"); } }).catchError((err) { DialogUtils.showErrorMessage(context, "请求发生错误", "请求服务器发生错误:${err.toString()}", btnLabel: "我知道了"); }).whenComplete(() { BrnLoadingDialog.dismiss(context); }); }); } } class EditStockInPage extends StatefulWidget { final StockInDataXuGong item; const EditStockInPage({super.key, required this.item}); @override _EditStockInPageState createState() => _EditStockInPageState(); } class _EditStockInPageState extends State { late TextEditingController goodsIdController; late TextEditingController goodsNumController; late TextEditingController goodsDescController; late TextEditingController weightController; late TextEditingController sizeController; @override void initState() { super.initState(); goodsIdController = TextEditingController(text: widget.item.goodsId); goodsNumController = TextEditingController(text: widget.item.goodsNum.toString()); goodsDescController = TextEditingController(text: widget.item.goodsDesc); weightController = TextEditingController(text: widget.item.weight.toString()); sizeController = TextEditingController(text: widget.item.size); } @override void dispose() { goodsIdController.dispose(); goodsNumController.dispose(); goodsDescController.dispose(); weightController.dispose(); sizeController.dispose(); super.dispose(); } void _saveEdits() { // Create the updated item StockInDataXuGong updatedItem = StockInDataXuGong( goodsId: goodsIdController.text, goodsNum: double.parse(goodsNumController.text), orderNo: widget.item.orderNo, // Retain the original value orderType: widget.item.orderType, // Retain the original value customerName: widget.item.customerName, // Retain the original value goodsDesc: goodsDescController.text, weight: double.parse(weightController.text), size: sizeController.text, spare1: widget.item.spare1, // Retain the original value spare2: widget.item.spare2, // Retain the original value containerNo: widget.item.containerNo, // Retain the original value ); // Pass the updated item back to the previous page Navigator.of(context).pop(updatedItem); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( iconTheme: const IconThemeData(color: Colors.white), centerTitle: true, title: const Text('编辑物料'), backgroundColor: ColorCommon.colorScheme, ), body: Padding( padding: const EdgeInsets.all(16.0), child: ListView( children: [ TextField( controller: goodsIdController, decoration: const InputDecoration(labelText: '物料号'), ), TextField( controller: goodsNumController, decoration: const InputDecoration(labelText: '数量'), keyboardType: TextInputType.number, ), TextField( controller: goodsDescController, decoration: const InputDecoration(labelText: '描述'), ), TextField( controller: weightController, decoration: const InputDecoration(labelText: '重量 (kg)'), keyboardType: TextInputType.number, ), TextField( controller: sizeController, decoration: const InputDecoration(labelText: '尺寸'), ), // Non-editable fields styled to look like editable ones but greyed out TextField( controller: TextEditingController(text: widget.item.orderNo), decoration: const InputDecoration(labelText: "单据号"), enabled: false, style: TextStyle(color: Colors.grey), ), TextField( controller: TextEditingController(text: widget.item.orderType), decoration: const InputDecoration(labelText: "单据类型"), enabled: false, style: TextStyle(color: Colors.grey), ), TextField( controller: TextEditingController(text: widget.item.customerName), decoration: const InputDecoration(labelText: "客户名称"), enabled: false, style: TextStyle(color: Colors.grey), ), TextField( controller: TextEditingController(text: widget.item.containerNo), decoration: const InputDecoration(labelText: "容器号"), enabled: false, style: TextStyle(color: Colors.grey), ), TextField( controller: TextEditingController(text: widget.item.spare1), decoration: const InputDecoration(labelText: "备用1"), enabled: false, style: TextStyle(color: Colors.grey), ), TextField( controller: TextEditingController(text: widget.item.spare2), decoration: const InputDecoration(labelText: "备用2"), enabled: false, style: TextStyle(color: Colors.grey), ), Padding( padding: const EdgeInsets.symmetric(vertical: 20.0), child: ElevatedButton( onPressed: _saveEdits, child: const Text('保存'), ), ), ], ), ), ); } }