import 'package:flutter/material.dart';
import 'package:localstore/localstore.dart';
import 'package:quiosco_soler_2/quiosco/models/db_estante_model.dart';
import 'package:quiosco_soler_2/quiosco/widgets/search_element.dart';

class GenerateItems extends StatefulWidget {
  final List<DbEstantes> findedItems;

  const GenerateItems({
    Key? key,
    required this.findedItems,
  }) : super(key: key);

  @override
  State<GenerateItems> createState() => _GenerateItemsState();
}

class _GenerateItemsState extends State<GenerateItems> {
  final _db = Localstore.instance;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: widget.findedItems.length,
      itemBuilder: (context, index) => TextButton(
        style: TextButton.styleFrom(
          padding: EdgeInsets.zero,
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          minimumSize: const Size(0, 0),
        ),
        child: SearchElement(
          image: widget.findedItems[index].idim!,
          name: widget.findedItems[index].esta!,
          date: widget.findedItems[index].fecr!,
        ),
        onPressed: () async{
          final items = await _db.collection('quiosco').get();
          final keys = items!.keys;
          final key = keys.last;

          final info = items[key];
          final id = key.replaceAll('/quiosco/', '');

          info['ides'] = widget.findedItems[index].ides;
          info['esta'] = widget.findedItems[index].esta;

          _db.collection('quiosco').doc(id).set(info);

          await Navigator.pushNamed(context, 'secc');

          final infoAct = {
            'user':        info['user'],
            'accessToken': info['accessToken'],
            'dinum':       info['dinum'],
          };

          _db.collection('quiosco').doc(id).set(infoAct);
        },
      ),
    );
  }
}
