import 'dart:convert'; import 'dart:core'; import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:siopas/models/asset_status_model.dart'; import 'package:siopas/models/customer_model.dart'; import 'package:siopas/models/disposal_model.dart'; import 'package:siopas/models/type_peti_model.dart'; import 'package:siopas/pages/peminjaman_barang/conn/syncronize.dart'; import 'package:siopas/services/controllerApi.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/models/m_asset_status_model.dart'; import 'package:siopas/models/warehouse_mode.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:intl/date_symbol_data_local.dart'; // Import package intl import 'package:uuid/uuid.dart'; import '../../connection/connection.dart'; import 'package:http/http.dart' as http; import '../../models/user_model.dart'; import '../../providers/auth_provider.dart'; import '../../services/syncronizeAPI.dart'; import 'controller/peminjaman_controller.dart'; import 'package:collection/collection.dart'; class CreatePeminjamanBarang extends StatefulWidget { const CreatePeminjamanBarang({Key? key}) : super(key: key); @override State createState() => _CreatePeminjamanBarangState(); } class _CreatePeminjamanBarangState extends State { String? token; List? typePetiSqfliteApi; // List? customerSqfliteApi; PetiAssetModel? petiSqfliteApi; WarehouseModel? warehouseSqfliteApi; WarehouseModel? warehouseTujuanSqfliteApi; DisposalPetiModel? disposalSqfliteApi; CustomerModel? customerSqfliteApi; List? _valpeti; // Change this line List? _valwarehouse; List? _valwarehouseTujuan; List? _valcustomer; List? _valdisposal; List _allPetiList = []; List _unrestrictedPetiList = []; List _filteredPetiList = []; bool isQRCodeScanned = false; bool loading = true; TextEditingController _exit_atController = TextEditingController(); TextEditingController _est_pengembalianController = TextEditingController(); TextEditingController _penanggungJawabController = TextEditingController(); final _formKey = GlobalKey(); final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); Barcode? result; QRViewController? controller; @override void initState() { super.initState(); _getUserToken(); warehouseListAPI(); petiListAPI(); disposalListAPI(); customerListAPI(); initializeDateFormatting('id_ID', null); } void _getUserToken() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (mounted) { setState(() { token = prefs.getString('token'); loading = false; }); } } Future disposalListAPI() async { if (mounted) { await ControllerApi().fetchDisposalDataAPI().then((value) { setState(() { _valdisposal = (value as List) .map((item) => DisposalPetiModel.fromJson(item)) .toList(); loading = false; }); }); } } Future customerListAPI() async { if (mounted) { await ControllerApi().fetchCustomerDataAPI().then((value) { setState(() { _valcustomer = (value as List) .map((item) => CustomerModel.fromJson(item)) .toList(); loading = false; }); }); } } Future warehouseListAPI() async { if (mounted) { await ControllerApi().fetchWarehouseDataAPI().then((value) { setState(() { _valwarehouse = (value as List) .map((item) => WarehouseModel.fromJson(item)) .toList(); loading = false; }); }); } } Future petiListAPI() async { if (mounted) { await ControllerApi().fetchPetiDataAPI().then((value) { setState(() { _valpeti = (value as List) .map((item) => PetiAssetModel.fromJson(item)) .toList(); loading = false; }); }); } } Future isInteret() async { await SyncronizationPeminjamanData.isInternet().then((connection) { if (connection) { print("Internet connection available"); } else { ScaffoldMessenger.of(context) .showSnackBar(SnackBar(content: Text("No Internet"))); } }); } // 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 = ''; // String idWarehouse = ''; // for (String line in lines) { // if (line.contains(';')) { // List values = line.split(';'); // if (values.length >= 3) { // idPeti = values[1]; // idWarehouse = values[2]; // break; // } // } // } // int? petiId = int.tryParse(idPeti); // int? warehouseId = int.tryParse(idWarehouse); // // AuthProvider authProvider = // // Provider.of(context, listen: false); // // UserModel user = authProvider.user; // if (petiId != null && warehouseId != null) { // // Check apakah peti dengan warehouse_id yang sesuai ada dalam daftar yang diizinkan // PetiAssetModel? allowedPeti = _valpeti?.firstWhereOrNull( // (peti) => // peti.id == petiId && // peti.deleted_at != true && // // peti.warehouse_id == user.warehouse_id && // peti.status == 'AKTIF', // ); // if (allowedPeti != null) { // petiSqfliteApi = allowedPeti; // warehouseSqfliteApi = _valwarehouse?.firstWhereOrNull( // // warehouseSqfliteApi = _valwarehouse?.firstWhere( // (warehouse) => warehouse.id == allowedPeti.warehouse_id, // ); // } else { // // Tampilkan pesan error jika data tidak sesuai dengan hak akses // ScaffoldMessenger.of(context).showSnackBar( // SnackBar( // content: Text( // 'Data Peti tidak ditemukan atau tidak sesuai dengan hak akses.', // style: TextStyle( // color: Colors.white, // fontSize: 12, // ), // ), // backgroundColor: Colors.red[700], // action: SnackBarAction( // label: 'Tutup', // onPressed: () { // ScaffoldMessenger.of(context).hideCurrentSnackBar(); // }, // ), // ), // ); // petiSqfliteApi = null; // warehouseSqfliteApi = null; // } // } else { // // Tampilkan pesan error jika nilai yang dipindai tidak sesuai // ScaffoldMessenger.of(context).showSnackBar( // SnackBar( // content: Row( // children: [ // Icon( // Icons.warning, // color: Colors.black, // ), // SizedBox(width: 8), // Text( // 'Nilai QR Code tidak sesuai dengan yang diharapkan.', // style: TextStyle( // color: Colors.black, // fontSize: 12, // ), // ), // ], // ), // backgroundColor: Colors.yellow[700], // ), // ); // petiSqfliteApi = null; // warehouseSqfliteApi = null; // } // scanned = true; // }); // controller.stopCamera(); // Future.delayed(Duration(milliseconds: 500), () { // if (mounted) { // Navigator.of(context).pop(); // } // }); // } catch (e) { // controller.stopCamera(); // // Reset nilai dropdown Peti dan Warehouse // setState(() { // petiSqfliteApi = null; // warehouseSqfliteApi = null; // }); // print('Error scanning QR Code: $e'); // ScaffoldMessenger.of(context).showSnackBar( // SnackBar( // content: Row( // children: [ // Icon( // Icons.error, // color: Colors.red[400], // ), // SizedBox(width: 8), // Flexible( // child: Text( // e.toString(), // style: TextStyle( // color: Colors.white, // fontSize: 12, // ), // ), // ), // ], // ), // backgroundColor: Colors.red[700], // ), // ); // } // } // }, // ); // } 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 = ''; String idWarehouse = ''; for (String line in lines) { if (line.contains(';')) { List values = line.split(';'); if (values.length >= 3) { idPeti = values[1]; idWarehouse = values[2]; break; } } } int? petiId = int.tryParse(idPeti); int? warehouseId = int.tryParse(idWarehouse); // AuthProvider authProvider = Provider.of(context, listen: false); // UserModel user = authProvider.user; // Dalam fungsi yang menangani pemindaian QR code // Dalam fungsi yang menangani pemindaian QR code if (petiId != null && warehouseId != null) { // Check apakah peti dengan warehouse_id yang sesuai ada dalam daftar yang diizinkan PetiAssetModel? allowedPeti = _valpeti?.firstWhereOrNull( (peti) => peti.id == petiId && peti.deleted_at != true && peti.status == 'AKTIF', ); if (allowedPeti != null) { setState(() { isQRCodeScanned = true; // Mengisi _filteredPetiList untuk dropdown hasil QR Code _filteredPetiList = [ _valpeti!.firstWhere((peti) => peti.id == allowedPeti.id) ]; petiSqfliteApi = allowedPeti; warehouseSqfliteApi = _valwarehouse?.firstWhereOrNull( (warehouse) => warehouse.id == allowedPeti.warehouse_id, ); customerSqfliteApi = _valcustomer?.firstWhereOrNull( (customer) => customer.id == allowedPeti.customer_id, ); }); } else { // Tampilkan pesan error jika data tidak sesuai dengan hak akses ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( 'Data Peti tidak ditemukan atau tidak sesuai dengan hak akses.', style: TextStyle( color: Colors.white, fontSize: 12, ), ), backgroundColor: Colors.red[700], action: SnackBarAction( label: 'Tutup', onPressed: () { ScaffoldMessenger.of(context).hideCurrentSnackBar(); }, ), ), ); setState(() { isQRCodeScanned = false; petiSqfliteApi = null; warehouseSqfliteApi = null; }); } } else { // Tampilkan pesan error jika nilai yang dipindai tidak sesuai ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Row( children: [ Icon( Icons.warning, color: Colors.black, ), SizedBox(width: 8), Text( 'Nilai QR Code tidak sesuai dengan yang diharapkan.', style: TextStyle( color: Colors.black, fontSize: 12, ), ), ], ), backgroundColor: Colors.yellow[700], ), ); setState(() { isQRCodeScanned = false; petiSqfliteApi = null; warehouseSqfliteApi = null; }); } scanned = true; }); controller.stopCamera(); Future.delayed(Duration(milliseconds: 500), () { if (mounted) { Navigator.of(context).pop(); } }); } catch (e) { controller.stopCamera(); // Reset nilai dropdown Peti dan Warehouse setState(() { petiSqfliteApi = null; warehouseSqfliteApi = null; }); print('Error scanning QR Code: $e'); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Row( children: [ Icon( Icons.error, color: Colors.red[400], ), SizedBox(width: 8), Flexible( child: Text( e.toString(), style: TextStyle( color: Colors.white, fontSize: 12, ), ), ), ], ), backgroundColor: Colors.red[700], ), ); } } }, ); } @override Widget build(BuildContext context) { AuthProvider authProvider = Provider.of(context, listen: false); UserModel user = authProvider.user; var uuid = Uuid(); DateTime? parseDateTime(String? dateTimeString) { if (dateTimeString == null || dateTimeString.isEmpty) { return null; } try { return DateTime.parse(dateTimeString); } catch (e) { print('Error parsing DateTime: $e'); return null; } } Future saveAssetData() async { DateTime now = DateTime.now().toLocal(); String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(now); if (_exit_atController.text.isNotEmpty) { AssetStatusModel assetAddModel = AssetStatusModel( id: null, peti_id: petiSqfliteApi!.id, customer_id: customerSqfliteApi!.id, warehouse_id: warehouseSqfliteApi!.id, exit_at: parseDateTime(_exit_atController.text), exit_pic: _penanggungJawabController.text, exit_warehouse: warehouseTujuanSqfliteApi!.id, est_pengembalian: parseDateTime(_est_pengembalianController.text), created_by: user.fullname, created_at: parseDateTime(formattedDate), mobile_id: uuid.v4(), ); // Call addData function int result = await Controller().addPeminjamanData(assetAddModel); if (result > 0) { print("Success Tambah data"); // print(result); Navigator.pushNamed(context, '/peminjaman-barang'); EasyLoading.showSuccess("Data Berhasil Disimpan"); } else { print("Failed"); } } } var scanArea = (MediaQuery.of(context).size.width < 400 || MediaQuery.of(context).size.height < 400) ? 250.0 : 300.0; return Scaffold( appBar: AppBar( automaticallyImplyLeading: false, backgroundColor: Colors.indigo[700], elevation: 0, title: Text('Buat Peminjaman Barang', style: TextStyle( fontSize: 16, )), ), body: loading ? Center(child: CircularProgressIndicator()) : SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(8.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: petiSqfliteApi, // items: (_valpeti ?? []) // .where((peti) => // peti.deleted_at != true && // (peti.warehouse_id == // user.warehouse_id) && // (peti.status == 'AKTIF')) // .map((PetiAssetModel peti) { // return DropdownMenuItem( // child: Text( // '${peti.fix_lot}', // Menampilkan warehouse_id // style: TextStyle( // fontSize: 12, // ), // ), // value: peti, // ); // }).toList(), // onChanged: (PetiAssetModel? value) { // setState( // () { // petiSqfliteApi = value; // if (value != null) { // warehouseSqfliteApi = // _valwarehouse?.firstWhere( // (warehouse) => // warehouse.id == // value.warehouse_id, // ); // customerSqfliteApi = // _valcustomer?.firstWhereOrNull( // (customer) => // customer.id == // value.customer_id, // ); // } // }, // ); // }, // ), DropdownButtonFormField( validator: (value) { if (value == null) { return 'Harus diisi'; } return null; }, decoration: InputDecoration( labelText: 'Peti', border: OutlineInputBorder(), ), hint: Text("Pilih Peti"), value: petiSqfliteApi, items: (isQRCodeScanned ? _filteredPetiList : (_valpeti ?? []).where((peti) => peti.deleted_at != true && (peti.warehouse_id == user.warehouse_id) && (peti.status == 'AKTIF'))) .map((PetiAssetModel peti) { return DropdownMenuItem( child: Text( '${peti.fix_lot}', style: TextStyle( fontSize: 12, ), ), value: peti, ); }).toList(), onChanged: (PetiAssetModel? value) { setState(() { petiSqfliteApi = value; if (value != null) { warehouseSqfliteApi = _valwarehouse?.firstWhere( (warehouse) => warehouse.id == value.warehouse_id, ); customerSqfliteApi = _valcustomer?.firstWhereOrNull( (customer) => customer.id == value.customer_id, ); // Perbarui _unrestrictedPetiList sesuai pemilihan manual _unrestrictedPetiList = [ _valpeti!.firstWhere( (peti) => peti.id == value.id) ]; } }); }, ), ), ), ), 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: () { 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), 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: 'Customer', border: OutlineInputBorder(), ), hint: Text("Pilih Customer"), value: customerSqfliteApi, items: (_valcustomer ?? []) .where( (customer) => customer.deleted_at != true) .map((CustomerModel customer) { return DropdownMenuItem( child: Text( '${customer.name}', // Menampilkan warehouse_id style: TextStyle( fontSize: 12, ), ), value: customer, ); }).toList(), onChanged: (CustomerModel? value) { setState( () { customerSqfliteApi = value; }, ); }, ), ), ), SizedBox(height: 16), Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(8), child: FormBuilderDateTimePicker( controller: _exit_atController, name: 'tanggal_peminjaman', inputType: InputType.date, format: DateFormat('yyyy-MM-dd', 'id_ID'), decoration: InputDecoration( labelText: 'Tanggal Peminjaman', border: OutlineInputBorder(), suffixIcon: Icon(Icons.calendar_today), ), onChanged: (DateTime? selectedDate) { if (selectedDate != null) { // Mengisi tanggal estimasi 7 hari ke depan DateTime estimasiPengembalian = selectedDate.add(Duration(days: 7)); _est_pengembalianController.text = DateFormat('yyyy-MM-dd') .format(estimasiPengembalian); } }, ), ), ), SizedBox(height: 16), Card( elevation: 2, child: Column( children: [ SizedBox(height: 8), Container( margin: EdgeInsets.only( left: 8), // Atur nilai left sesuai kebutuhan child: Align( alignment: Alignment.centerLeft, child: Text( '* Terisi otomatis 7 hari setelah tanggal peminjaman', style: TextStyle( fontSize: 12, color: Colors.red, fontStyle: FontStyle.italic), ), ), ), SizedBox(height: 8), Padding( padding: const EdgeInsets.all(8), child: FormBuilderDateTimePicker( controller: _est_pengembalianController, name: 'estimasi_pengembalian', inputType: InputType.date, format: DateFormat('yyyy-MM-dd', 'id_ID'), decoration: InputDecoration( labelText: 'Estimasi Tanggal Pengembalian', border: OutlineInputBorder(), suffixIcon: Icon(Icons.calendar_today), ), ), ), ], ), ), SizedBox(height: 16), Card( elevation: 2, child: Padding( padding: const EdgeInsets.all(8), child: TextFormField( controller: _penanggungJawabController = 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: 13), Card( child: Padding( padding: const EdgeInsets.all(8.0), child: DropdownButtonFormField( validator: (value) { if (value == null) { return 'Harus diisi'; } return null; }, decoration: InputDecoration( labelText: 'Asal Gudang', border: OutlineInputBorder(), ), hint: Text("Asal Gudang"), value: warehouseSqfliteApi, items: (_valwarehouse ?? []) .where((warehouse) => warehouse.deleted_at != true) .map((WarehouseModel warehouse) // _valwarehouse?.map((WarehouseModel warehouse) { return DropdownMenuItem( child: Text('${warehouse.name}'), value: warehouse, ); }).toList() ?? [], onChanged: (WarehouseModel? value) { setState(() { warehouseSqfliteApi = value; }); }, ), ), ), SizedBox(height: 13), Card( child: Padding( padding: const EdgeInsets.all(8.0), child: DropdownButtonFormField( validator: (value) { if (value == null) { return 'Harus diisi'; } return null; }, decoration: InputDecoration( labelText: 'Tujuan Gudang', border: OutlineInputBorder(), ), hint: Text("Tujuan Gudang"), value: warehouseTujuanSqfliteApi, items: (_valwarehouse ?? []) .where((warehouseTujuan) => warehouseTujuan.deleted_at != true) .map((WarehouseModel warehouseTujuan) // _valwarehouse?.map((WarehouseModel warehouse) { return DropdownMenuItem( child: Text('${warehouseTujuan.name}'), value: warehouseTujuan, ); }).toList() ?? [], onChanged: (WarehouseModel? value) { setState(() { warehouseTujuanSqfliteApi = value; }); }, ), ), ), ], ), ), ), ), 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, '/peminjaman-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: () async { if (_formKey.currentState!.validate()) { try { if (_exit_atController.text.isNotEmpty && petiSqfliteApi != null && warehouseSqfliteApi != null) { await saveAssetData(); } } catch (e) { print('Error storing data: $e'); } } }, icon: Icon(Icons.save, color: Colors.white), ), ), ], ), ), ], ), ), ); } }