// ignore_for_file: must_be_immutable

import 'package:conalep_pos/models/models.dart';
import 'package:conalep_pos/views/categoria_producto/categoria_producto_screen.dart';
import 'package:conalep_pos/views/corte_caja/corte_caja_screen.dart';
import 'package:conalep_pos/views/mesa/mesa_screen.dart';
import 'package:conalep_pos/views/pedido/pedido_screen.dart';
import 'package:conalep_pos/views/pedido_mesa/pedido_mesa_screen.dart';
import 'package:conalep_pos/views/producto/producto_screen.dart';
import 'package:conalep_pos/views/venta/venta_screen.dart';
import 'package:flutter/material.dart';
import '../models/usuario_model.dart';
import 'package:provider/provider.dart';
import '../themes/themes.dart';
import '../viewmodels/login_view_model.dart';
import '../views/descuento/descuento_screen.dart';
import '../views/variable/variable_screen.dart';
import 'widgets_components.dart';
import 'copia_db.dart';

class AppDrawer extends StatefulWidget {
  @override
  _AppDrawerState createState() => _AppDrawerState();
}

class _AppDrawerState extends State<AppDrawer> {
  int _versionTapCount = 0;

  Future<bool> _showExitConfirmationDialog(BuildContext context) async {
    bool shouldPop = false;
    await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        surfaceTintColor: AppTheme.secondary,
        title: const Text('¿Cerrar sesión?'),
        content: const Text('¿Estás seguro de que quieres cerrar la sesión?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text('Cancelar', style: TextStyle(color: Colors.red)),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              Navigator.pop(context);
              Provider.of<LoginViewModel>(context, listen: false).logOut();
              Navigator.of(context)
                  .pushNamedAndRemoveUntil('main', (route) => false);
            },
            child: const Text('Aceptar'),
          ),
        ],
      ),
    );
    return shouldPop;
  }

  @override
  Widget build(BuildContext context) {
    String? nombre = Provider.of<LoginViewModel>(context).nombre.toString();
    String? correo = Provider.of<LoginViewModel>(context).correo.toString();
    //final avm = Provider.of<AdministracionViewModel>(context);
    //List<String> permisos = avm.lospermisos;
    return Drawer(
      surfaceTintColor: Colors.white,
      backgroundColor: Colors.white,
      child: Column(
        children: [
          Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: AppTheme.primary,
            ),
            padding: EdgeInsets.only(
              top: MediaQuery.of(context).padding.top,
            ),
            child: const Column(
              children: [
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Image(
                    image: AssetImage('assets/icono-BN.png'),
                    height: 150,
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            ),
          ),
          //HEADER
          Expanded(
              child: ListView(children: [
            // ListTile(
            //   leading: circulo(const Icon(Icons.lunch_dining)),
            //   title: const Text('Inicio'),
            //   onTap: () => {
            //     Navigator.pop(context),
            //     Navigator.of(context).push(
            //       MaterialPageRoute(
            //         builder: (context) => const HomeScreen(),
            //       ),
            //     ),
            //   },
            // ),
            ListTile(
              leading: circulo(const Icon(Icons.restaurant_menu)),
              title: const Text('Pedidos'),
              onTap: () => {
                Navigator.pop(context),
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const PedidoScreen(),
                  ),
                ),
              },
            ),
            ListTile(
              leading: circulo(const Icon(Icons.restaurant_menu)),
              title: const Text('Pedidos Mesa'),
              onTap: () => {
                Navigator.pop(context),
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const PedidoMesaScreen(),
                  ),
                ),
              },
            ),
            ListTile(
              leading: circulo(const Icon(Icons.menu_book_rounded)),
              title: const Text('Productos'),
              onTap: () => {
                Navigator.pop(context),
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => ProductoScreen(),
                  ),
                ),
              },
            ),
            ListTile(
              leading: circulo(const Icon(Icons.format_list_bulleted_rounded)),
              title: const Text('Categoría Producto'),
              onTap: () => {
                Navigator.pop(context),
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => CategoriaProductoScreen(),
                  ),
                ),
              },
            ),
            ListTile(
              leading: circulo(const Icon(Icons.receipt_long_outlined)),
              title: const Text('Pedidos Por Día'),
              onTap: () => {
                Navigator.pop(context),
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => VentaScreen(),
                  ),
                ),
              },
            ),
            // ListTile(
            //   leading: circulo(const Icon(Icons.point_of_sale_rounded)),
            //   title: const Text('Corte De Caja'),
            //   onTap: () => {
            //     Navigator.pop(context),
            //     Navigator.of(context).push(
            //       MaterialPageRoute(
            //         builder: (context) => CorteCajaScreen(),
            //       ),
            //     ),
            //   },
            // ),
            ExpansionTile(
              leading: circulo(const Icon(Icons.admin_panel_settings)),
              title: const Text('Administración'),
              children: [
                ListTile(
                  leading: circulo(const Icon(Icons.discount)),
                  title: const Text('Descuentos'),
                  onTap: () => {
                    Navigator.pop(context),
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => DescuentoScreen(),
                      ),
                    ),
                  },
                ),
                ListTile(
                  leading: circulo(const Icon(Icons.discount)),
                  title: const Text('Variables'),
                  onTap: () => {
                    Navigator.pop(context),
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => VariablesScreen(),
                      ),
                    ),
                  },
                ),
                ListTile(
                  leading: circulo(const Icon(Icons.table_restaurant)),
                  title: const Text('Mesas'),
                  onTap: () => {
                    Navigator.pop(context),
                    Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => MesasScreen(),
                      ),
                    ),
                  },
                ),
              ],
            ),
            // ListTile(
            //   leading: const Icon(Icons.logout),
            //   title: const Text('Cerrar sesión'),
            //   onTap: () {
            //     _showExitConfirmationDialog(context);
            //   },
            // ),
          ])),
          GestureDetector(
            onTap: () {
              _versionTapCount++;
              if (_versionTapCount == 5) {
                copyDatabase(context);
                _versionTapCount = 0;
              }
            },
            child: const Padding(
              padding: EdgeInsets.only(bottom: 10),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Text(
                  'v1.24.10.03',
                  style: TextStyle(fontWeight: FontWeight.w300),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
