import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';

import '../controllers/datakebun_controller.dart';

class DatakebunView extends GetView<DatakebunController> {
  const DatakebunView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Data Kebun',
          style: GoogleFonts.poppins(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.green,
        
      ),
      body: Obx(() {
        if (controller.kebunData.isEmpty) {
          return Center(
            child: Text(
              "Tidak ada data kebun",
              style: GoogleFonts.poppins(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.grey,
              ),
            ),
          );
        }
        return Padding(
          padding: const EdgeInsets.all(16),
          child: _buildKebunTable(),
        );
      }),
    );
  }

  Widget _buildKebunTable() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.15),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      padding: const EdgeInsets.all(12),
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: ConstrainedBox(
          constraints: const BoxConstraints(minWidth: 600),
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: DataTable(
              border: TableBorder.all(color: Colors.black26),
              headingRowColor: MaterialStateColor.resolveWith(
                (states) => const Color(0xFFC8E6C9),
              ),
              columnSpacing: 16,
              columns: _buildTableColumns(),
              rows: controller.kebunData.map((data) => _buildDataRow(data)).toList(),
            ),
          ),
        ),
      ),
    );
  }

  List<DataColumn> _buildTableColumns() {
  return [
    _tableColumn("Nama Kebun"),
    _tableColumn("Alamat"),
    _tableColumn("Nama Petani"),
    _tableColumn("Tanaman"),
    _tableColumn("Aksi"),
  ];
}


  DataColumn _tableColumn(String label) {
    return DataColumn(
      label: Text(
        label,
        style: GoogleFonts.poppins(
          fontSize: 13,
          fontWeight: FontWeight.w600,
          color: Colors.black87,
        ),
      ),
    );
  }

  DataRow _buildDataRow(Map<String, dynamic> data) {
    return DataRow(
  cells: [
    DataCell(_wrapText(data['nama_kebun'] ?? '-')),
    DataCell(_wrapText(data['lokasi'] ?? '-')),  // alamat
    DataCell(_wrapText(data['nama_petani'] ?? '-')),
    DataCell(_wrapText(data['tanaman'] ?? '-')),
    DataCell(
      Row(
        children: [
          IconButton(
            icon: const Icon(Icons.edit, color: Colors.orange),
            onPressed: () {
              Get.snackbar("Edit", "Fitur edit untuk ${data['nama_kebun']}");
            },
          ),
          IconButton(
            icon: const Icon(Icons.delete, color: Colors.red),
            onPressed: () {
              Get.defaultDialog(
                title: "Konfirmasi",
                middleText: "Yakin hapus ${data['nama_kebun']}?",
                textCancel: "Batal",
                textConfirm: "Hapus",
                confirmTextColor: Colors.white,
                onConfirm: () {
                  controller.deleteKebun(data['id']);
                  Get.back();
                },
              );
            },
          ),
        ],
      ),
    ),
  ],
);

  }

  Widget _wrapText(String text) {
    return Text(
      text,
      overflow: TextOverflow.ellipsis,
      maxLines: 2,
      style: GoogleFonts.poppins(fontSize: 12),
    );
  }
}
