import 'package:flutter/material.dart';
import 'package:glowwheels/helpers/shopid_helper.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'package:glowwheels/models/order_model.dart';
import '../provider/order_provider.dart';
import '../widgets/order_card.dart';

class OrdersScreen extends StatefulWidget {
  @override
  _OrdersScreenState createState() => _OrdersScreenState();
}

class _OrdersScreenState extends State<OrdersScreen> {
  late String shopId;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      shopId = getShopId(context)!;
      Provider.of<OrdersProvider>(context, listen: false)
          .fetchOrders(shopId, refresh: false);
    });
  }

  Future<void> _refreshOrders() async {
    shopId = getShopId(context)!;
    await Provider.of<OrdersProvider>(context, listen: false)
        .fetchOrders(shopId, refresh: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        automaticallyImplyLeading: false,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset('assets/icon/order_appbar_icon.png'),
            SizedBox(width: 20),
            Text(
              'Orders',
              style: GoogleFonts.poppins(
                fontSize: 24,
                color: Color.fromRGBO(43, 46, 53, 1),
                fontWeight: FontWeight.w600,
              ),
            ),
          ],
        ),
        centerTitle: true,
        foregroundColor: Colors.black,
        elevation: 1,
      ),
      body: Padding(
        padding: const EdgeInsets.only(top: 8.0),
        child: Consumer<OrdersProvider>(
          builder: (context, orderProvider, _) {
            final orders = orderProvider.orders;

            if (orderProvider.isLoading && !orderProvider.isRefreshing) {
              return Center(child: CircularProgressIndicator());
            }

            if (orders.isEmpty) {
              return RefreshIndicator(
                onRefresh: _refreshOrders,
                child: ListView(
                  physics: const AlwaysScrollableScrollPhysics(),
                  children: [
                    SizedBox(height: 100),
                    Center(
                      child: Image.asset(
                        'assets/images/noorder.png',
                        width: 200,
                        height: 200,
                      ),
                    ),
                    SizedBox(height: 16),
                    Center(
                      child: Text(
                        'No orders found',
                        style: GoogleFonts.poppins(fontSize: 16),
                      ),
                    ),
                  ],
                ),
              );
            }

            return RefreshIndicator(
              onRefresh: _refreshOrders,
              child: ListView.builder(
                itemCount: orders.length,
                itemBuilder: (context, index) {
                  return OrderCard(order: orders[index]);
                },
              ),
            );
          },
        ),
      ),
    );
  }
}
