import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:get/get.dart';

class DatakebunController extends GetxController {
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;

  var kebunData = <Map<String, dynamic>>[].obs;

  @override
  void onInit() {
    super.onInit();
    fetchKebunData();
  }

  Future<void> fetchKebunData() async {
    try {
      final snapshot = await _firestore.collection('kebun').get();
      kebunData.value = snapshot.docs.map((doc) {
        final data = doc.data();
        return {
  'id': doc.id,
  'nama_kebun': data['nama_kebun'],
  'lokasi': data['alamat'], // ganti kalau field di Firestore namanya 'alamat'
  'nama_petani': data['nama_petani'],
  'tanaman': data['tanaman'],
};

      }).toList();
    } catch (e) {
      Get.snackbar("Error", "Gagal mengambil data: $e");
    }
  }

  Future<void> deleteKebun(String id) async {
    try {
      await _firestore.collection('kebun').doc(id).delete();
      fetchKebunData();
      Get.snackbar("Berhasil", "Data kebun berhasil dihapus");
    } catch (e) {
      Get.snackbar("Error", "Gagal menghapus: $e");
    }
  }
}
