import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:shimmer/shimmer.dart'; import 'package:siopas/models/asset_status_model.dart'; import 'package:siopas/models/customer_model.dart'; import 'package:siopas/models/m_asset_status_model.dart'; import 'package:siopas/pages/peminjaman_barang/controller/peminjaman_controller.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; import 'package:collection/collection.dart'; import '../../models/warehouse_mode.dart'; import '../../services/controllerApi.dart'; import '../../widget/loading_shimmer_show.dart'; class DetailPeminjamanBarangPage extends StatefulWidget { final String peminjamanId; const DetailPeminjamanBarangPage({Key? key, required this.peminjamanId}) : super(key: key); @override _DetailPeminjamanBarangPageState createState() => _DetailPeminjamanBarangPageState(); } class _DetailPeminjamanBarangPageState extends State { AssetStatusModel? peminjamanInfo; WarehouseModel? warehouseInfo; List? petiData; List? customerData; List? warehouseData; late Future _dataFuture; bool loading = true; // Atur loading ke true pada awalnya @override void initState() { super.initState(); // Set loading ke true pada awalnya if (mounted) { if (mounted) { setState(() { loading = false; }); } } _dataFuture = fetchData(); } Future fetchData() async { await getPeminjamanIdData(); await customerListAPI(); await petiListAPI(); await warehouseListAPI(); initializeDateFormatting('id_ID', null); // Tandai bahwa proses loading telah selesai if (mounted) { setState(() { loading = false; }); } } Future customerListAPI() async { if (mounted) { await ControllerApi().fetchCustomerDataAPI().then((value) { setState(() { customerData = (value as List) .map((item) => CustomerModel.fromJson(item)) .toList(); loading = false; }); }); } } Future petiListAPI() async { if (mounted) { await ControllerApi().fetchPetiDataAPI().then((value) { setState(() { petiData = (value as List) .map((item) => PetiAssetModel.fromJson(item)) .toList(); loading = false; }); }); } } Future warehouseListAPI() async { if (mounted) { await ControllerApi().fetchWarehouseDataAPI().then((value) { setState(() { warehouseData = (value as List) .map((item) => WarehouseModel.fromJson(item)) .toList(); loading = false; }); }); } } Future getPeminjamanIdData() async { List peminjamans = await Controller().fetchPeminjamanDataId(); peminjamanInfo = peminjamans.firstWhereOrNull( (peminjaman) => peminjaman.id.toString() == widget.peminjamanId, ); if (mounted) { setState(() {}); } } String _formatDate(String? date) { if (date != null) { DateTime parsedDate = DateTime.parse(date); String formattedDate = DateFormat('EEEE, dd MMMM yyyy', 'id_ID').format(parsedDate); return formattedDate; } else { return ''; } } Future _deletePeminjaman() async { try { // Panggil fungsi untuk menghapus peminjaman berdasarkan ID await Controller().deletePeminjamanById(widget.peminjamanId); // Navigasi kembali ke halaman sebelumnya atau halaman yang sesuai Navigator.pushNamed(context, '/peminjaman-barang'); } catch (e) { // Tangani kesalahan jika terjadi print('Gagal menghapus peminjaman: $e'); } } @override Widget build(BuildContext context) { PetiAssetModel? petiSqfliteApi; petiSqfliteApi = petiData?.firstWhereOrNull( (peti) => peti.id == peminjamanInfo!.peti_id, ); CustomerModel? customerSqfliteApi; customerSqfliteApi = customerData?.firstWhereOrNull( (customer) => customer.id == peminjamanInfo?.customer_id, ); WarehouseModel? warehouseSqfliteApi; warehouseSqfliteApi = warehouseData?.firstWhereOrNull( (warehouse) => warehouse.id == peminjamanInfo!.warehouse_id, ); // WarehouseModel? warehouseTujuanSqfliteApi; // warehouseTujuanSqfliteApi = warehouseData?.firstWhereOrNull( // (warehouse) => warehouse.id == peminjamanInfo!.exit_warehouse, // ); Future _showDeleteConfirmationDialog() async { return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16.0), ), title: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ Icon( Icons.info, color: Colors.blue, ), SizedBox(width: 8), Text( 'Konfirmasi Hapus', style: TextStyle( fontSize: 18, ), ), ], ), IconButton( icon: Icon( Icons.close, color: Colors.black54, ), onPressed: () { Navigator.of(context).pop(); }, ), ], ), content: SingleChildScrollView( child: ListBody( children: [ Text( 'Anda yakin ingin menghapus peminjaman ini? ${petiSqfliteApi!.fix_lot.toString()}', style: TextStyle( fontSize: 16, ), ), ], ), ), actions: [ Container( margin: EdgeInsets.only(right: 3.0), child: ElevatedButton( child: Text('Hapus'), onPressed: () { // Panggil fungsi untuk menghapus peminjaman berdasarkan ID _deletePeminjaman(); Navigator.pushNamed(context, '/peminjaman-barang'); EasyLoading.showSuccess( 'Berhasil menghapus data peminjaman'); }, style: ElevatedButton.styleFrom( primary: Colors.red, ), ), ), ], ); }, ); } return WillPopScope( onWillPop: () async { // Mencegah kembali ke halaman sebelumnya return false; }, child: Scaffold( backgroundColor: Colors.grey[200], appBar: AppBar( backgroundColor: Colors.indigo[700], elevation: 0, title: Text( 'Detail Peti Out', style: TextStyle( color: Colors.white, fontSize: 16, ), ), leading: IconButton( onPressed: () { Navigator.pop(context); }, icon: Icon(Icons.arrow_back), ), actions: [ IconButton( icon: Icon(Icons.delete, color: Colors.redAccent), onPressed: () { _showDeleteConfirmationDialog(); }, ), ], ), body: FutureBuilder( future: _dataFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return ShimmerShow(); } else if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return Padding( padding: EdgeInsets.all(16.0), child: Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(15.0), ), elevation: 5, child: Column( children: [ Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(15.0)), ), elevation: 0, margin: EdgeInsets.all(0), color: Colors.indigo[700], child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon(Icons.article, size: 30, color: Colors.white), SizedBox(width: 10), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(height: 5), Text( petiSqfliteApi != null && petiSqfliteApi.fix_lot != null ? petiSqfliteApi!.fix_lot.toString() : '-', style: TextStyle( fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white, ), ), ], ), ], ), ], ), ), ), SizedBox(height: 10), if (peminjamanInfo != null) ...[ _buildDetailItem( 'Kode Peti', petiSqfliteApi != null && petiSqfliteApi.fix_lot != null ? petiSqfliteApi!.fix_lot.toString() : '-', ), Divider(thickness: 1), _buildDetailItem( 'Nama Customer', customerSqfliteApi != null && customerSqfliteApi.name != null ? customerSqfliteApi!.name.toString() : '-', ), Divider(thickness: 1), _buildDetailItem('Tgl Peminjaman', _formatDate(peminjamanInfo!.exit_at.toString())), Divider(thickness: 1), _buildDetailItem( 'Est Peminjaman', _formatDate( peminjamanInfo!.est_pengembalian.toString())), Divider(thickness: 1), _buildDetailItem( 'PIC/PJ:', peminjamanInfo!.exit_pic.toString()), Divider(thickness: 1), _buildDetailItem( ' Gudang', warehouseSqfliteApi != null && warehouseSqfliteApi.name != null ? warehouseSqfliteApi!.name.toString() : '-'), Divider(thickness: 1), // ... tambahkan data lainnya sesuai kebutuhan ], ], ), ), ); } }, ), ), ); } Widget _buildDetailItem(String label, String value) { return Padding( padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( label, style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold), ), Text( value, style: TextStyle(fontSize: 12), ), ], ), ); } }