import 'package:flutter/material.dart';
import '../../../../../core/services/it_support_service.dart';
import '../it_support_widgets.dart';

class ITSupportTicketsPage extends StatelessWidget {
  const ITSupportTicketsPage({super.key, required this.tickets, required this.more,
    required this.onRefresh, required this.onLoadMore, required this.onOpen});
  final List<SupportJson> tickets;
  final bool more;
  final Future<void> Function() onRefresh;
  final VoidCallback onLoadMore;
  final ValueChanged<int> onOpen;

  @override
  Widget build(BuildContext context) => RefreshIndicator(onRefresh: onRefresh,
    child: ListView.builder(physics: const AlwaysScrollableScrollPhysics(),
      padding: const EdgeInsets.fromLTRB(16, 4, 16, 85), itemCount: tickets.length + 1,
      itemBuilder: (context, index) {
        if (index == tickets.length) return _listFooter(tickets.isEmpty
          ? 'No tickets yet. Submit a ticket or adjust your filters.' : '${tickets.length} tickets loaded', more, onLoadMore);
        final ticket = tickets[index];
        final percent = supportInt(ticket['percent']);
        return Padding(padding: const EdgeInsets.only(bottom: 8), child: Material(color: Colors.white,
          shape: const RoundedRectangleBorder(side: BorderSide(color: supportBorder)), clipBehavior: Clip.antiAlias,
          child: InkWell(onTap: () => onOpen(supportInt(ticket['id'])), child: Padding(padding: const EdgeInsets.all(13),
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Row(children: [Text('#${ticket['id']}', style: const TextStyle(color: supportGreen, fontSize: 12, fontWeight: FontWeight.w800)),
                const SizedBox(width: 8), Expanded(child: Text('${ticket['subject']}', maxLines: 2, overflow: TextOverflow.ellipsis,
                  style: const TextStyle(color: supportInk, fontWeight: FontWeight.w700, fontSize: 14))),
                const SizedBox(width: 8), SupportBadge(supportLabel(ticket['priority']),
                  color: ticket['priority'] == 'urgent' ? Colors.red : supportGold)]),
              const SizedBox(height: 8), Wrap(spacing: 8, runSpacing: 6, children: [
                SupportBadge(supportLabel(ticket['status'])),
                Text('${ticket['tracker']} · ${ticket['campus']}', style: const TextStyle(fontSize: 12, color: Color(0xFF6C7A71))),
                Text('Assigned: ${ticket['assignee'] ?? 'Awaiting assignment'}', style: const TextStyle(fontSize: 12, color: Color(0xFF6C7A71))),
              ]),
              const SizedBox(height: 10), Row(children: [Expanded(child: LinearProgressIndicator(value: percent / 100,
                minHeight: 4, backgroundColor: const Color(0xFFEDF2EE), color: supportGreen)), const SizedBox(width: 10),
                Text('$percent%', style: const TextStyle(fontSize: 11, fontWeight: FontWeight.w700, color: supportGreen))]),
            ])))));
      }));
}

Widget _listFooter(String message, bool more, VoidCallback onLoadMore) => Padding(
  padding: const EdgeInsets.all(24), child: Center(child: more
    ? TextButton(onPressed: onLoadMore, child: const Text('Load more'))
    : Text(message, textAlign: TextAlign.center, style: const TextStyle(color: Color(0xFF7A887F), fontSize: 12))));
