155 lines
5.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/constants/colors.dart';
import '../../../core/constants/api_constants.dart';
import '../../connect/screens/social_connect_details_screen.dart';
import '../../connect/screens/channels_screen.dart';
import '../../posts/screens/posts_screen.dart';
class AutomationScreen extends StatefulWidget {
const AutomationScreen({super.key});
@override
State<AutomationScreen> createState() => _AutomationScreenState();
}
class _AutomationScreenState extends State<AutomationScreen> {
int _limit = 20;
bool _isLoading = false;
String _lastRun = "";
Future<void> _triggerAutoReply() async {
setState(() => _isLoading = true);
try {
final prefs = await SharedPreferences.getInstance();
final email = prefs.getString('user_email');
final response = await http.post(
Uri.parse('${ApiConstants.liveBaseUrl}/social/automation/auto-reply?userId=$email'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'limit': _limit}),
);
if (response.statusCode == 200) {
setState(() {
_lastRun = DateTime.now().toString().split('.').first;
});
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Auto reply triggered successfully!")),
);
} else {
final data = jsonDecode(response.body);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(data['message'] ?? "Auto reply failed")),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("An error occurred")),
);
} finally {
setState(() => _isLoading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.darkBg,
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Container(
padding: const EdgeInsets.all(32),
decoration: BoxDecoration(
color: AppColors.cardBg,
borderRadius: BorderRadius.circular(30),
border: Border.all(color: Colors.white.withOpacity(0.1)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ShaderMask(
shaderCallback: (bounds) => const LinearGradient(
colors: [Colors.blue, Colors.pink],
).createShader(bounds),
child: Text(
"Auto Reply",
style: GoogleFonts.nunito(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
const SizedBox(height: 32),
Align(
alignment: Alignment.centerLeft,
child: Text(
"Auto Reply Limit",
style: GoogleFonts.nunito(color: Colors.white70, fontSize: 16),
),
),
const SizedBox(height: 8),
TextField(
keyboardType: TextInputType.number,
style: const TextStyle(color: Colors.white),
onChanged: (value) => _limit = int.tryParse(value) ?? 20,
decoration: InputDecoration(
fillColor: Colors.black26,
hintText: "Enter limit",
hintStyle: TextStyle(color: Colors.white24),
),
),
const SizedBox(height: 32),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isLoading ? null : _triggerAutoReply,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
).copyWith(
elevation: ButtonStyleButton.allOrNull(0),
),
child: Ink(
decoration: BoxDecoration(
gradient: const LinearGradient(colors: [Colors.blue, Colors.pink]),
borderRadius: BorderRadius.circular(12),
),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 16),
alignment: Alignment.center,
child: Text(
_isLoading ? "Processing..." : "Start Auto Reply",
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
),
),
),
),
if (_lastRun.isNotEmpty) ...[
const SizedBox(height: 24),
Text(
"Last executed: $_lastRun",
style: const TextStyle(color: Colors.white54, fontSize: 14),
),
],
const SizedBox(height: 16),
const Text(
"⏳ Auto-run happens every 1 hour",
style: TextStyle(color: Colors.blueAccent, fontWeight: FontWeight.bold),
),
],
),
),
),
),
);
}
}