import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:siopas/connection/connection.dart'; import 'package:http/http.dart' as http; import 'package:siopas/models/asset_status_model.dart'; // import 'package:siopas/models/m_asset_status_model.dart'; import 'package:siopas/models/user_model.dart'; import 'package:siopas/models/warehouse_mode.dart'; import 'package:siopas/providers/auth_provider.dart'; class CreatePengembalianBarangPage extends StatefulWidget { const CreatePengembalianBarangPage({super.key}); @override State createState() => _CreatePengembalianBarangPageState(); } class _CreatePengembalianBarangPageState extends State { List _dataAsset = []; List _dataWarehouse = []; bool _isLoading = false; String? token; AssetStatusModel? _valAsset; WarehouseModel? _valWarehouse; TextEditingController _enterAtController = TextEditingController(); TextEditingController _enterPicController = TextEditingController(); TextEditingController _kondisiPetiController = TextEditingController(); final _formKey = GlobalKey(); final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); Barcode? result; QRViewController? controller; @override void initState() { super.initState(); _getUserToken(); fetchDataWarehouse(); fetchDataAsset(); initializeDateFormatting('id_ID'); } void _getUserToken() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (mounted) { setState(() { token = prefs.getString('token'); }); } } Future fetchDataAsset() async { setState(() { _isLoading = true; }); final response = await http.get(Uri.parse('$baseUrl/asset-status/pengembalian/create')); if (mounted) { // Periksa apakah widget masih "mounted" if (response.statusCode == 200) { final jsonData = json.decode(response.body)['data']['asset_status']; final List newDataAsset = (jsonData as List) .map((item) => AssetStatusModel.fromJson(item)) .toList(); if (mounted) { // Periksa lagi sebelum memanggil setState setState(() { _dataAsset.addAll(newDataAsset); _isLoading = false; }); } } else { if (mounted) { setState(() { _isLoading = false; }); } throw Exception('Failed to fetch data Asset Status'); } } } Future fetchDataWarehouse() async { setState(() { _isLoading = true; }); final response = await http.get(Uri.parse('$baseUrl/m-warehouse')); if (mounted) { if (response.statusCode == 200) { final jsonData = json.decode(response.body)['data']['warehouse']; final List newDataWarehouse = (jsonData as List) .map((item) => WarehouseModel.fromJson(item)) .toList(); if (mounted) { setState(() { _dataWarehouse.addAll(newDataWarehouse); _isLoading = false; }); } } else { if (mounted) { setState(() { _isLoading = false; }); } throw Exception('Failed to fetch data Warehouse'); } } } Future _onQRViewCreated(QRViewController controller) async { this.controller = controller; bool scanned = false; // code to auto focus kamera belakang controller.flipCamera(); await Future.delayed(const Duration(milliseconds: 400)); controller.flipCamera(); controller.scannedDataStream.listen((scanData) { if (!scanned) { try { setState(() { result = scanData; List lines = result!.code!.split('\n'); String idPeti = ''; for (String line in lines) { if (line.contains(';')) { List values = line.split(';'); if (values.length >= 3) { idPeti = values[1]; break; } } } int? petiId = int.tryParse(idPeti); if (petiId != null) { _valAsset = _dataAsset.firstWhere( (peminjaman) => peminjaman.peti_id == petiId, orElse: () => _valAsset!, ); } else { // Jika nilai yang dipindai tidak sesuai, tampilkan pesan kesalahan ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Row( children: [ Icon( Icons.warning, color: Colors.black, // Warna ikon ), SizedBox(width: 8), // Jarak antara ikon dan teks Text( 'Nilai QR Code tidak sesuai dengan yang diharapkan.', style: TextStyle( color: Colors.black, fontSize: 12, ), // Warna teks ), ], ), backgroundColor: Colors.yellow[700], ), ); } scanned = true; }); controller.stopCamera(); Future.delayed(Duration(milliseconds: 500), () { if (mounted) { Navigator.pop(context); } }); } catch (e) { controller.stopCamera(); if (mounted) { Navigator.pushNamed(context, '/pengembalian-barang/create'); } print('Error scanning QR Code: $e'); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Row( children: [ Icon( Icons.error, // Icon error color: Colors.red[400], // Warna merah ), SizedBox(width: 8), Flexible( // Menggunakan Flexible untuk menghindari overflow child: Text( 'Peti ini belum ada di menu pinjaman, silahkan untuk mengecek kembali.', style: TextStyle( color: Colors.white, fontSize: 12, ), // Warna teks merah ), ), ], ), backgroundColor: Colors.red[700], ), ); } } }); } @override void dispose() { super.dispose(); controller?.dispose(); } @override Widget build(BuildContext context) { AuthProvider authProvider = Provider.of(context, listen: false); UserModel user = authProvider.user; var scanArea = (MediaQuery.of(context).size.width < 400 || MediaQuery.of(context).size.height < 400) ? 150.0 : 300.0; Future _updatePeminjaman() async { setState(() { _isLoading = true; }); try { final response = await http.put( Uri.parse('$baseUrl/asset-status/update/${_valAsset!.id}'), headers: { 'Content-Type': 'application/json', 'Authorization': token!, }, body: jsonEncode({ 'enter_at': _enterAtController.text, 'enter_pic': _enterPicController.text, 'enter_warehouse': _valWarehouse!.id, 'kondisi_peti': _kondisiPetiController.text, 'updated_by': user.fullname, }), ); print(response.body); if (response.statusCode == 200) { final jsonData = json.decode(response.body)['data']['asset_status']; print('Berhasil memperbarui data: $jsonData'); // Tampilkan snackbar ScaffoldMessenger.of(context).showSnackBar( SnackBar( backgroundColor: Colors.greenAccent[700], content: Row( children: [ Icon( Icons.check_circle_outline, color: Colors.white, ), SizedBox(width: 5), Text('Data berhasil dikembalikan'), ], ), duration: Duration(seconds: 3), // Durasi tampilan snackbar ), ); // Reset form input _valAsset = null; _enterAtController.text = ''; _enterPicController.text = ''; _kondisiPetiController.text = ''; _valWarehouse = null; } else { throw Exception('Gagal memperbarui data Asset Status'); } } catch (e) { print('Error updating data: $e'); ScaffoldMessenger.of(context).showSnackBar( SnackBar( backgroundColor: Colors.redAccent[700], content: Row( children: [ Icon( Icons.error_outline, color: Colors.white, ), SizedBox(width: 5), Text('Gagal memperbarui data'), ], ), duration: Duration(seconds: 2), // Durasi tampilan snackbar ), ); } finally { if (mounted) { setState(() { _isLoading = false; }); } } } return Scaffold( appBar: AppBar( elevation: 0, backgroundColor: Color.fromARGB(255, 5, 28, 158), title: Text('Pengembalian Barang'), leading: IconButton( icon: Icon(Icons.arrow_back), onPressed: () { Navigator.pushNamed(context, '/pengembalian-barang'); }, ), ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : SingleChildScrollView( child: Container( padding: EdgeInsets.all(10.0), child: Form( key: _formKey, child: Column( children: [ Row( children: [ Expanded( child: Card( elevation: 2, child: Container( margin: EdgeInsets.all(8), child: DropdownButtonFormField( validator: (value) { if (value == null) { return 'Harus diisi'; } return null; }, decoration: InputDecoration( labelText: 'Peti', border: OutlineInputBorder(), ), hint: Text("Pilih Peti"), value: _valAsset, items: _dataAsset.map((AssetStatusModel item) { return DropdownMenuItem( child: Text('${item.peti!.fix_lot!}'), value: item, ); }).toList(), onChanged: (AssetStatusModel? value) { setState(() { _valAsset = value; }); }, ), ), ), ), SizedBox(width: 8), // Spacer antara dua card Card( elevation: 2, child: Container( margin: EdgeInsets.all(8), height: MediaQuery.of(context).size.height / 10, decoration: BoxDecoration( color: Colors.indigoAccent, borderRadius: BorderRadius.circular(5), ), child: IconButton( onPressed: () { setState(() { _valAsset = null; // Mengatur _valAsset menjadi null }); showModalBottomSheet( context: context, isScrollControlled: true, // Set modal menjadi fullscreen builder: (BuildContext context) { return Stack( alignment: Alignment.center, children: [ Container( height: double.infinity, width: double.infinity, child: QRView( key: qrKey, onQRViewCreated: _onQRViewCreated, overlay: QrScannerOverlayShape( borderColor: Colors.red, borderRadius: 10, borderLength: 30, borderWidth: 10, cutOutSize: scanArea, ), ), ), Positioned( bottom: 30, height: 60, child: Container( height: 60, // Lebar dan tinggi sesuai kebutuhan width: 60, decoration: BoxDecoration( shape: BoxShape.circle, color: Colors .red, // Warna merah untuk close ), child: IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: Icon( Icons.close, size: 40, ), color: Colors.white, ), ), ), ], ); }, ); }, icon: Icon( Icons.qr_code, size: 30, ), color: Colors.white, // Warna ikon ), ), ), ], ), SizedBox(height: 16), // Visibility( // visible: false, // child: Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(8), child: TextFormField( controller: _enterPicController = TextEditingController(text: user.fullname), decoration: InputDecoration( labelText: 'Penanggung Jawab', border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return 'Harus diisi'; } return null; // Return null jika tidak ada kesalahan }, ), ), ), // ), SizedBox(height: 16), Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(8), child: FormBuilderDateTimePicker( validator: (value) { if (_enterAtController.text.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( backgroundColor: Colors.redAccent[700], content: Row( children: [ Icon( Icons.error_outline, color: Colors.white, ), SizedBox(width: 5), Text('Tanggal Pengembalian'), ], ), duration: Duration(seconds: 2), ), ); return null; // Return null jika ada kesalahan } return null; // Return null jika tidak ada kesalahan }, controller: _enterAtController, name: 'tanggal', inputType: InputType.date, // format: DateFormat('dd-MMMM-yyyy', 'id_ID'), format: DateFormat('yyyy-MM-dd', 'id_ID'), decoration: InputDecoration( labelText: 'Tanggal Pengembalian', border: OutlineInputBorder(), suffixIcon: Icon(Icons.calendar_today), ), ), ), ), SizedBox(height: 16), Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(8), child: DropdownButtonFormField( validator: (value) { if (value == null) { return 'Harus diisi'; } return null; }, decoration: InputDecoration( labelText: 'Pilih Gudang', border: OutlineInputBorder(), ), hint: Text("Pilih Gudang"), value: _valWarehouse, items: _dataWarehouse.map((WarehouseModel warehouse) { return DropdownMenuItem( child: Text('${warehouse.name}'), value: warehouse, ); }).toList(), onChanged: (WarehouseModel? value) { setState(() { _valWarehouse = value; }); }, ), ), ), SizedBox(height: 16.0), Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(8), child: TextFormField( controller: _kondisiPetiController, decoration: InputDecoration( labelText: 'Kondisi Barang', border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return 'Harus diisi'; } return null; // Return null jika tidak ada kesalahan }, ), ), ), SizedBox(height: 20.0), Visibility( visible: false, child: FractionallySizedBox( widthFactor: 1.0, child: Card( elevation: 1, child: Padding( padding: const EdgeInsets.all(8), child: Column( children: [ Text( 'Data dari QR Code:', style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, ), ), SizedBox(height: 8), Text( result != null && result!.code != null ? result!.code! : 'Belum ada data QR Code terpindai', style: TextStyle(fontSize: 14), ), ], ), ), ), ), ), ], ), ), ), ), bottomNavigationBar: BottomAppBar( height: MediaQuery.of(context).size.height / 8, color: Color.fromARGB(255, 5, 28, 158), // Warna latar belakang child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( width: MediaQuery.of(context).size.width / 3, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( height: 50, // Lebar dan tinggi sesuai kebutuhan width: 50, decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.red, // Warna merah untuk close ), child: IconButton( onPressed: () { // Navigator.pop(context); Navigator.pushNamed(context, '/pengembalian-barang'); }, icon: Icon(Icons.close, color: Colors.white), ), ), ], ), ), Container( width: MediaQuery.of(context).size.width / 3, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( height: 50, // Lebar dan tinggi sesuai kebutuhan width: 50, decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.green, // Warna hijau untuk save ), child: IconButton( onPressed: () { if (_formKey.currentState!.validate()) { try { if (_enterAtController.text.isNotEmpty) { _updatePeminjaman(); } } catch (e) { print('Error storing data: $e'); } } }, icon: Icon(Icons.save, color: Colors.white), ), ), ], ), ), ], ), ), ); } }