import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller/home_controller.dart';

class HomePage extends GetView<HomeController> {
  static const String namedRoute = '/home-page';
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    var homCtrl = controller;
    return PopScope(
      canPop: false,
      child: SafeArea(
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.blueGrey,
            foregroundColor: Colors.white,
            title: const Text('Tank List'),
            centerTitle: true,
            actions: [
              IconButton(
                icon: const Icon(Icons.refresh),
                onPressed: () {
                  controller.onInit();
                },
              ),
              IconButton(
                onPressed: () => homCtrl.logout(),
                icon: Icon(Icons.logout),
              ),
            ],
          ),
          body: Obx(
            () => homCtrl.isLoading.value == false
                ? ListView.builder(
                    itemBuilder: (context, index) {
                      var tank = homCtrl.listTankModel[index];
                      return ListTile(
                        title: Text(
                          '${tank.szDate} - ${tank.szLiters}L - ${tank.szPricePerLiter}€/L',
                        ),
                        subtitle: Text(
                          'Total: ${tank.szPriceTotal}€ - Odometer: ${tank.szOdometer}km',
                        ),
                      );
                    },
                    itemCount: homCtrl.listTankModel.length,
                  )
                : Center(child: CircularProgressIndicator()),
          ),
        ),
      ),
    );
  }
}
