import '/models/mesa_model.dart';
import '/themes/themes.dart';
import '/viewmodels/mesa_view_model.dart';
import '/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MesaForm extends StatefulWidget {
  final Mesa mesa;

  const MesaForm({Key? key, required this.mesa}) : super(key: key);

  @override
  _MesaFormState createState() => _MesaFormState();
}

class _MesaFormState extends State<MesaForm> {
  final _nombre = TextEditingController();
  final _clave = TextEditingController();
  bool _activa = true;

  @override
  void initState() {
    super.initState();
    _nombre.text = widget.mesa.nombre ?? "";
    _clave.text = widget.mesa.clave ?? "";
    _activa = widget.mesa.activa ?? true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.mesa.id == null ? 'Nueva Mesa' : 'Editar Mesa',
          style: TextStyle(color: AppTheme.secondary),
        ),
        iconTheme: IconThemeData(color: AppTheme.secondary),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(8),
        child: Column(
          children: [
            tarjeta(
              Padding(
                padding: const EdgeInsets.all(8),
                child: Column(
                  children: [
                    AppTextField(
                      maxLength: 100,
                      etiqueta: 'Nombre',
                      controller: _nombre,
                      hintText: 'Nombre de la mesa',
                    ),
                    AppTextField(
                      maxLength: 100,
                      etiqueta: 'Clave',
                      controller: _clave,
                      hintText: 'Clave de la mesa',
                    ),
                    SwitchListTile(
                      activeColor: AppTheme.primary,
                      title: const Text(
                        "Activo",
                        style: TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 18),
                      ),
                      value: _activa,
                      onChanged: (bool value) {
                        setState(() {
                          _activa = value;
                        });
                      },
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(height: 15),
            boton("Guardar", () async {
              Provider.of<MesaViewModel>(context, listen: false)
                  .setIsLoading(true);

              widget.mesa.nombre = _nombre.text;
              widget.mesa.clave = _clave.text;
              widget.mesa.activa = _activa;

              if (widget.mesa.id == null || widget.mesa.id == 0) {
                await Provider.of<MesaViewModel>(context, listen: false)
                    .addMesa(widget.mesa);
              } else {
                await Provider.of<MesaViewModel>(context, listen: false)
                    .updateMesa(widget.mesa);
              }

              Provider.of<MesaViewModel>(context, listen: false)
                  .setIsLoading(false);

              if (context.mounted) {
                Navigator.pop(context);
              }
            })
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _nombre.dispose();
    _clave.dispose();
    super.dispose();
  }
}
