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 'show.dart'; class AssetStatusPage extends StatefulWidget { const AssetStatusPage({super.key}); @override State createState() => AssetStatusPageState(); } class AssetStatusPageState 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 Peminjaman 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('Peminjaman Barang'), rowsPerPage: _pageSize, availableRowsPerPage: const [10, 25, 50], onRowsPerPageChanged: (value) { setState(() { _pageSize = value!; }); }, columns: const [ DataColumn(label: Text('No')), DataColumn(label: Text('')), DataColumn(label: Text('Kode Peti')), DataColumn(label: Text('Nama Customer')), DataColumn(label: Text('Tgl Peminjaman')), DataColumn(label: Text('PJ Peminjaman')), DataColumn(label: Text('Asal Gudang')), ], 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, '/peminjaman-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( GestureDetector( onTap: () { if (item.id != null) { Navigator.push( context, MaterialPageRoute( builder: (context) => DetailPeminjamanBarangPage( assetId: item.id!, ), ), ); print('asset id: ${item.id}'); } }, child: Icon(Icons.article, size: 40, color: Colors.indigo[700]), // Ganti ikon sesuai kebutuhan ), ), DataCell( Text( // item.asset.exit_at.toString(), item.peti!.customer!.code_customer.toString() + '-' + item.peti!.tipe_peti!.type.toString(), ), ), DataCell( Text( item.peti!.customer!.name.toString(), ), ), DataCell( Text( // item.asset.exit_at.toString(), // item.exit_at.toString(), // DateFormat('dd-MM-yyyy').format(item.est_pengembalian!) item.est_pengembalian != null ? DateFormat('dd-MM-yyyy').format(item.est_pengembalian!) : '-', ), ), DataCell( Text( item.exit_pic.toString(), ), ), DataCell( Text(item.warehouse!.name.toString()), ), ]); } @override bool get isRowCountApproximate => false; @override int get rowCount => data.length; @override int get selectedRowCount => 0; }