import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'package:cheminova/models/Daily_Task_Response.dart'; import 'package:cheminova/widgets/common_app_bar.dart'; import 'package:cheminova/widgets/common_background.dart'; import 'package:cheminova/widgets/common_drawer.dart'; import '../constants/constant.dart'; import '../provider/daily_task_provider.dart'; import 'Add_products_screen.dart'; class UpdateInventoryTaskScreen extends StatefulWidget { const UpdateInventoryTaskScreen({super.key}); @override _UpdateInventoryTaskScreenState createState() => _UpdateInventoryTaskScreenState(); } class _UpdateInventoryTaskScreenState extends State { late DailyTaskProvider _dailyTaskProvider; // Provider to manage daily tasks @override void initState() { super.initState(); _dailyTaskProvider = DailyTaskProvider(); // Initialize the provider apiCall(); // Fetch tasks from the API } // Function to fetch new and pending tasks void apiCall() async { await _dailyTaskProvider.getTask(type: 'New', isAddPending: true); // Get new tasks await _dailyTaskProvider.getTask(type: 'Pending', isAddPending: true); // Get pending tasks } @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (context) => _dailyTaskProvider, // Provide the DailyTaskProvider to the widget tree child: Scaffold( extendBodyBehindAppBar: true, // Extend the body behind the app bar appBar: _buildAppBar(), // Build the app bar drawer: const CommonDrawer(), // Navigation drawer body: CommonBackground( child: SafeArea( child: _buildTaskList(), // Build the list of tasks ), ), ), ); } // Function to build the app bar CommonAppBar _buildAppBar() { return CommonAppBar( backgroundColor: Colors.transparent, // Transparent background for app bar elevation: 0, // No shadow for app bar actions: [ // Back button in the app bar IconButton( onPressed: () => Navigator.pop(context), // Navigate back icon: Image.asset('assets/Back_attendance.png'), padding: const EdgeInsets.only(right: 20), ), ], title: const Text( 'Inventory Update Tasks', style: TextStyle(color: Colors.black87, fontSize: 20), // Title style ), ); } // Function to build the task list Widget _buildTaskList() { return Consumer( builder: (context, value, child) { // Show loading indicator while fetching tasks if (value.isLoading) { return const Center(child: CircularProgressIndicator()); } // Filter tasks that require inventory updates final inventoryTasks = value.newTasksList .where((task) => task.task?.toLowerCase() == 'update inventory data') .toList(); // Show message if there are no inventory tasks if (inventoryTasks.isEmpty) { return const Center( child: Text( 'NO TASK', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black87, ), ), ); } // Build the list view for inventory tasks return ListView.separated( padding: const EdgeInsets.all(16), itemCount: inventoryTasks.length, // Number of tasks to display separatorBuilder: (context, index) => const SizedBox(height: 8), // Space between items itemBuilder: (context, index) => _buildTaskCard(inventoryTasks[index]), // Build each task card ); }, ); } // Function to build an individual task card Widget _buildTaskCard(Tasks tasksList) { return InkWell( onTap: () { // Navigate to AddProductsScreen when a task card is tapped if (tasksList.sId != null && tasksList.addedFor != null) { Navigator.push( navigatorKey.currentContext!, MaterialPageRoute( builder: (context) => AddProductsScreen( distributorType: tasksList.addedFor!, // Pass distributor type inventoryId: tasksList.sId!, // Pass inventory ID tradeName: tasksList.tradeName ?? '', // Pass trader name pdRdId: tasksList.addedForId!, // Pass PD/RD ID ), ), ); } }, child: Card( color: Colors.white, // Card color shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)), // Card shape child: ListTile( leading: const Icon(Icons.inventory, color: Colors.blueAccent), // Leading icon title: Text( tasksList.task ?? '', // Task name style: const TextStyle( color: Colors.black87, fontWeight: FontWeight.w700, fontSize: 16, fontFamily: 'Anek', // Font style ), ), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, // Align subtitles to the start children: [ Text('Distributor: ${tasksList.addedFor ?? ""}'), // Display distributor name Text('Trader Name: ${tasksList.tradeName ?? ''}'), // Display trader name // Display due date if it exists if (tasksList.taskDueDate != null) Text('Due Date: ${DateFormat('dd/MM/yyyy').format(DateTime.parse(tasksList.taskDueDate!))}'), Text('Priority: ${tasksList.taskPriority}'), // Display task priority ], ), trailing: const Icon(Icons.arrow_forward_ios, color: Colors.black87), // Trailing icon ), ), ); } }