import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; import '../../config/apiUrl.dart'; import '../../services/apiService.dart'; import '../../utils/auth_utils.dart'; class CommentModalList extends StatefulWidget { final String planId; final Color layoutColorForUser; final String role; const CommentModalList({ Key? key, required this.planId, required this.layoutColorForUser, required this.role, }) : super(key: key); @override _CommentModalListState createState() => _CommentModalListState(); } class _CommentModalListState extends State { final ApiService apiService = ApiService(); late Future>> _commentsFuture; // Future>> fetchComments1() async { // final response = await http.get( // Uri.parse( // '$apiUrl/api/plans/getRemarksByPlanId?plan_id=${widget.planId}', // ), // ); // // if (response.statusCode == 200) { // final jsonData = json.decode(response.body); // final List dataList = jsonData['data']; // return dataList.cast>(); // } else { // throw Exception('Failed to load comments'); // } // } Future>> fetchComments() async { // final String apiUrldata = // '$apiUrl/getRemarksByPlanId?plan_id=${widget.planId}&user_id=$userId'; final String apiUrldata = '$apiUrl/api/plans/getRemarksByPlanId?plan_id=${widget.planId}'; final token = await getToken(); if (token == null) { throw Exception('Token not found. Please log in.'); } final response = await http.get( Uri.parse(apiUrldata), headers: { 'Authorization': 'Bearer $token', 'app-signature': 'ts-traveltool-2025-signature-123456', 'Content-Type': 'application/json', }, ); if (response.statusCode == 200) { final jsonData = json.decode(response.body); final List dataList = jsonData['data']; return dataList.cast>(); } else if (response.statusCode == 403) { print("403-FORB"); await apiService.logout(context); return []; // throw Exception('Failed to load users'); } else { throw Exception('Failed to load comments'); } } @override void initState() { super.initState(); _commentsFuture = fetchComments(); } @override Widget build(BuildContext context) { return AlertDialog( backgroundColor: Colors.white, title: Text('Comments', style: GoogleFonts.poppins(color: Colors.black)), content: ConstrainedBox( constraints: const BoxConstraints( maxWidth: 500, // ✅ You can adjust this width maxHeight: 400, // ✅ Optional: limit height to make it scrollable vertically ), child: FutureBuilder>>( future: _commentsFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); } if (snapshot.hasError) { return Text( 'No Comments For This Plan', style: GoogleFonts.poppins(fontSize: 12, color: Colors.black), ); } if (!snapshot.hasData || snapshot.data!.isEmpty) { return Text( 'No comments found.', style: GoogleFonts.poppins(fontSize: 12, color: Colors.black), ); } final comments = snapshot.data!; return SingleChildScrollView( scrollDirection: Axis.horizontal, child: DataTable( columns: [ DataColumn( label: Text( 'Name', style: GoogleFonts.poppins( fontSize: 11, color: Colors.black, ), ), ), DataColumn( label: Text( 'Comment', style: GoogleFonts.poppins( fontSize: 11, color: Colors.black, ), ), ), DataColumn( label: Text( 'Updated On', style: GoogleFonts.poppins( fontSize: 11, color: Colors.black, ), ), ), ], rows: comments.map((comment) { final name = comment['created_by_name'] ?? 'Unknown'; final remark = comment['remarks'] ?? ''; final rawDateStr = comment['updated_on']; String updatedOn = ''; if (rawDateStr != null && rawDateStr.isNotEmpty) { try { final parsedDate = DateTime.parse(rawDateStr); updatedOn = DateFormat( 'd MMM yyyy', ).format(parsedDate); // e.g., 15 May 2025 } catch (e) { updatedOn = rawDateStr.split(' ').first; // fallback } } return DataRow( cells: [ DataCell( Text( name, style: GoogleFonts.poppins( fontSize: 12, color: Colors.black, fontWeight: FontWeight.w500, ), ), ), DataCell( Text( remark, style: GoogleFonts.poppins( fontSize: 12, color: Colors.black, fontWeight: FontWeight.w500, ), ), ), DataCell( Text( updatedOn, style: GoogleFonts.poppins( fontSize: 12, color: Colors.black, fontWeight: FontWeight.w500, ), ), ), ], ); }).toList(), ), ); }, ), ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text( 'Close', style: GoogleFonts.poppins( fontSize: 12, fontWeight: FontWeight.w500, color: widget.layoutColorForUser, ), ), ), ], ); } }