import 'dart:convert'; import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:data_table_2/data_table_2.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:siopas/models/asset_status_model.dart'; import 'package:siopas/providers/asset_status_provider.dart'; import 'package:http/http.dart' as http; import '../../connection/connection.dart'; import '../peminjaman_barang/show.dart'; class PengembalianBarangPage extends StatefulWidget { const PengembalianBarangPage({super.key}); @override State createState() => PengembalianBarangPageState(); } class PengembalianBarangPageState extends State { String? token; int _currentPage = 1; int _pageSize = 10; List _data = []; bool _isLoading = false; @override void initState() { super.initState(); _getUserToken(); fetchData(); } void _getUserToken() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (mounted) { setState(() { token = prefs.getString('token'); }); } } Future fetchData() async { if (mounted) { setState(() { _isLoading = true; }); try { final response = await http.get(Uri.parse('$baseUrl/asset-status')); if (response.statusCode == 200) { final jsonData = json.decode(response.body)['data']['asset_status']; final List newData = (jsonData as List) .map((item) => AssetStatusModel.fromJson(item)) .toList(); if (mounted) { setState(() { _data.addAll(newData); _isLoading = false; }); } } else { if (mounted) { setState(() { _isLoading = false; }); } throw Exception('Failed to fetch data'); } } catch (e) { if (mounted) { setState(() { _isLoading = false; }); } print('Error fetching data: $e'); } } } void _loadMoreData() { if (mounted && !_isLoading) { setState(() { _currentPage++; }); fetchData(); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.indigo[700], elevation: 0, title: Text('Data Pengembalian Barang', style: TextStyle( fontSize: 16, )), leading: IconButton( icon: Icon(Icons.arrow_back, color: Colors.white), onPressed: () { Navigator.pushNamed(context, '/home'); }, ), ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : SingleChildScrollView( child: SizedBox( width: double.infinity, child: PaginatedDataTable( header: const Text('Pengembalian Barang'), rowsPerPage: _pageSize, availableRowsPerPage: const [10, 25, 50], onRowsPerPageChanged: (value) { setState(() { _pageSize = value!; }); }, columns: const [ DataColumn(label: Text('No')), DataColumn(label: Text('Kode Peti')), DataColumn(label: Text('Tgl Peminjaman')), DataColumn(label: Text('Estimasi Pengembalian')), DataColumn(label: Text('PJ Peminjaman')), DataColumn(label: Text('Asal WH Peminjaman')), DataColumn(label: Text('Tgl Pengembalian')), DataColumn(label: Text('PJ Pengembalian')), DataColumn(label: Text('Tujuan WH Pengembalian')), DataColumn(label: Text('Kondisi Peti')), DataColumn(label: Text('Status')), ], source: _DataSource(data: _data, context: context), ), ), ), bottomNavigationBar: BottomAppBar( color: Color.fromARGB(255, 5, 28, 158), // Warna latar belakang child: Container( height: 65.0, child: Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, children: [ InkWell( customBorder: CircleBorder(), onTap: () { // Aksi ketika ikon diklik Navigator.pushNamed(context, '/pengembalian-barang/create'); }, child: Container( width: 45, height: 45, decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.greenAccent[700], ), child: Icon( Icons.add, size: 30, color: Colors.white, ), ), ), ], ), ), ), ); } } class _DataSource extends DataTableSource { final List data; final BuildContext context; _DataSource({required this.data, required this.context}); @override DataRow? getRow(int index) { if (index >= data.length) { return null; } final item = data[index]; return DataRow(cells: [ DataCell( Text( (index + 1).toString(), ), ), DataCell( Text( // item.asset.exit_at.toString(), item.peti!.customer!.code_customer.toString() + '-' + item.peti!.tipe_peti!.type.toString(), ), ), DataCell( GestureDetector( onTap: () { if (item.id != null) { Navigator.push( context, MaterialPageRoute( builder: (context) => DetailPeminjamanBarangPage( assetId: item.id!, ), ), ); print('asset id: ${item.id}'); } }, child: Text( item.exit_at != null ? DateFormat('dd-MM-yyyy').format(item.exit_at!) : '-', ), ), ), DataCell( Text( // item.asset.exit_at.toString(), item.est_pengembalian != null ? DateFormat('dd-MM-yyyy').format(item.est_pengembalian!) : '-', ), ), DataCell( Text( item.exit_pic.toString() != 'null' ? item.exit_pic.toString() : '-', ), ), DataCell( Text(item.warehouse!.name.toString() != 'null' ? item.warehouse!.name.toString() : '-'), ), DataCell( Text( item.enter_at != null ? DateFormat('dd-MM-yyyy').format(item.enter_at!) : '-', ), ), DataCell( Text( item.enter_pic.toString() != 'null' ? item.enter_pic.toString() : '-', ), ), DataCell( Text( item.warehouse_enter!.id == item.enter_warehouse ? item.warehouse_enter!.name.toString() : '-', ), ), DataCell( Text(item.kondisi_peti.toString() != 'null' ? item.kondisi_peti.toString() : '-'), ), DataCell( Text(item.warehouse_enter!.name == 'null' ? '-' : 'Sudah Dikembalikan'), ), ]); } @override bool get isRowCountApproximate => false; @override int get rowCount => data.length; @override int get selectedRowCount => 0; }