import 'package:conalep_pos/themes/themes.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class CorteCajaForm extends StatefulWidget {
  @override
  _CorteCajaFormState createState() => _CorteCajaFormState();
}

class _CorteCajaFormState extends State<CorteCajaForm> {
  final TextEditingController _fondoController = TextEditingController();
  final TextEditingController _depositosController = TextEditingController();
  final TextEditingController _fondoSiguienteController =
      TextEditingController();
  final TextEditingController _ventasMostradorController =
      TextEditingController();
  final TextEditingController _gastosDiaController = TextEditingController();
  double corteFinal = 0.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text(
            'Corte de Caja Conalep',
            style: TextStyle(color: AppTheme.quaternary),
          ),
          iconTheme: IconThemeData(color: AppTheme.quaternary)),
      body: Align(
        alignment: Alignment.topLeft,
        child: Container(
          padding: EdgeInsets.all(16),
          width: MediaQuery.of(context).size.width * 0.5,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("Fecha: ${DateFormat('dd/MM/yyyy').format(DateTime.now())}",
                  style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
              SizedBox(height: 20),
              Table(
                columnWidths: {
                  0: FlexColumnWidth(3),
                  1: FlexColumnWidth(2),
                },
                children: [
                  _buildTableRow("Fondo", _fondoController),
                  _buildTableRow("Depósitos", _depositosController),
                  _buildTableRow(
                      "Fondo día siguiente", _fondoSiguienteController),
                  _buildTableRow(
                      "Ventas mostrador efectivo", _ventasMostradorController),
                  _buildTableRow("Gastos del día", _gastosDiaController),
                  TableRow(
                    children: [
                      Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Text("Corte final",
                            style: TextStyle(fontWeight: FontWeight.bold)),
                      ),
                      Padding(
                        padding:
                            EdgeInsets.symmetric(vertical: 8, horizontal: 12),
                        child: Text("\$${corteFinal.toStringAsFixed(2)}",
                            style: TextStyle(fontWeight: FontWeight.bold)),
                      ),
                    ],
                  ),
                ],
              ),
              SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  ElevatedButton(
                    onPressed: calcularCorteFinal,
                    child: Text(
                      'Finalizar',
                      style: TextStyle(color: AppTheme.quaternary),
                    ),
                    style: ElevatedButton.styleFrom(primary: AppTheme.primary),
                  ),
                  ElevatedButton.icon(
                    icon: Image.asset(
                      'assets/csv_white.png',
                      scale: 3,
                    ),
                    label: Text('Generar CSV',
                        style: TextStyle(color: AppTheme.quaternary)),
                    onPressed: () {
                      // Lógica para generar CSV
                    },
                    style: ElevatedButton.styleFrom(
                        primary: AppTheme.primary,
                        padding: EdgeInsets.all(20),
                        shape: RoundedRectangleBorder(
                            borderRadius:
                                BorderRadius.all(Radius.circular(20)))),
                  ),
                  ElevatedButton.icon(
                    icon: Icon(Icons.picture_as_pdf,
                        color: AppTheme.quaternary), // Icono para PDF
                    label: Text('Generar PDF',
                        style: TextStyle(color: AppTheme.quaternary)),
                    onPressed: () {
                      // Lógica para generar PDF
                    },
                    style: ElevatedButton.styleFrom(primary: AppTheme.primary),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  TableRow _buildTableRow(String label, TextEditingController controller) {
    return TableRow(
      children: [
        Padding(
          padding: EdgeInsets.all(8.0),
          child: Text(label, style: TextStyle(fontWeight: FontWeight.bold)),
        ),
        TextField(
          controller: controller,
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            hintText: "\$",
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
          ),
        ),
      ],
    );
  }

  void calcularCorteFinal() {
    double fondo = double.tryParse(_fondoController.text) ?? 0;
    double depositos = double.tryParse(_depositosController.text) ?? 0;
    double fondoSiguiente =
        double.tryParse(_fondoSiguienteController.text) ?? 0;
    double ventasMostrador =
        double.tryParse(_ventasMostradorController.text) ?? 0;
    double gastosDia = double.tryParse(_gastosDiaController.text) ?? 0;

    setState(() {
      corteFinal =
          (fondo + depositos + ventasMostrador) - (fondoSiguiente + gastosDia);
    });
  }
}
