import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:youmazgestion/Services/stock_managementDatabase.dart';

import '../Models/produit.dart';
//import '../Services/productDatabase.dart';
import 'gestionProduct.dart';

class EditProductPage extends StatelessWidget {
  final Product product;
  final TextEditingController _nameController;
  final TextEditingController _priceController;
  final TextEditingController _categoryController;

  EditProductPage({super.key, required this.product})
      : _nameController = TextEditingController(text: product.name),
        _priceController =
            TextEditingController(text: product.price.toString()),
        _categoryController = TextEditingController(text: product.category);

  void _saveChanges() async {
    final name = _nameController.text;
    final price = double.tryParse(_priceController.text) ?? 0.0;
    final category = _categoryController.text;

    if (name.isNotEmpty && price > 0) {
      final updatedProduct = Product(
        id: product.id,
        name: name,
        price: price,
        image: product.image,
        category: category,
      );

      await AppDatabase.instance.updateProduct(updatedProduct);

      Get.to(GestionProduit());
    } else {
      Get.snackbar(
        'Entrée invalide',
        'Veuillez entrer un nom et un prix valides',
        snackPosition: SnackPosition.BOTTOM,
        backgroundColor: Colors.red,
        colorText: Colors.white,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Modifier le produit'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              'Modifier le produit',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: _nameController,
              decoration: const InputDecoration(labelText: 'Nom du produit'),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: _priceController,
              decoration: const InputDecoration(labelText: 'Prix'),
              keyboardType:
                  const TextInputType.numberWithOptions(decimal: true),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: _categoryController,
              decoration: const InputDecoration(labelText: 'Catégorie'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: _saveChanges,
              child: const Text('Enregistrer les modifications'),
            ),
          ],
        ),
      ),
    );
  }
}
