import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:pharmacy_mobile/tables/ref_medicines.dart';
import 'package:pharmacy_mobile/tables/stocks.dart';
import 'package:pharmacy_mobile/widgets/button_widget.dart';
import 'package:pharmacy_mobile/widgets/datepicker_widget.dart';
import 'package:pharmacy_mobile/widgets/dropdown_widget.dart';
import 'package:pharmacy_mobile/widgets/form_border_widget.dart';
import 'package:pharmacy_mobile/widgets/input_widget.dart';
import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
import 'package:pharmacy_mobile/widgets/text_widget.dart';
import 'package:pharmacy_mobile/widgets/title_widget.dart';

class AddStockPage extends StatefulWidget {
  const AddStockPage({super.key});

  @override
  State<AddStockPage> createState() => _AddStockPageState();
}

class _AddStockPageState extends State<AddStockPage> {
  final _formKey = GlobalKey<FormState>();
  final FocusNode _focusNode = FocusNode();
  final _refMedicines = RefMedicines();
  final _quantityController = TextEditingController();
  final _dateController = TextEditingController();
  final _stocks = Stocks();

  late List _medicineList = [];
  late String _selectedMedicine = '';
  late DateTime selectedDate = DateTime.now();

  void autoRun() async {
    _medicineList = await _refMedicines.getList();
  }

  void _updateMedicine(dynamic medicine) {
    _selectedMedicine = medicine;
  }

  void _saveStock() async {
    final stockNameUUID = await _refMedicines.getUUID(_selectedMedicine);
    final stockQuantity = _quantityController.text;
    final stockExpiration = _dateController.text;

    await _stocks.postStock(stockNameUUID, stockExpiration, stockQuantity);
  }

  @override
  void initState() {
    autoRun();
    super.initState();
  }

  @override
  void dispose() {
    _focusNode.dispose();
    _medicineList = [];
    _selectedMedicine = '';
    _quantityController.dispose();
    _dateController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageBackgroundWidget(
        child: Center(
          child: Column(
            children: [
              const Gap(96),
              const TitleWidget(firstTextSize: 20, secondTextSize: 32),
              const Gap(32),
              const TextWidget(text: 'Add Stock'),
              const Gap(16),
              FormBorderWidget(
                color: 'green',
                child: Form(
                  key: _formKey,
                  child: Center(
                    child: Column(
                      children: [
                        DropDownWidget(
                            label: 'Medicine Name',
                            list: _medicineList,
                            listTitle: 'medicine_name',
                            onChanged: _updateMedicine),
                        const Gap(16),
                        InputWidget(label: 'Quantity', controller: _quantityController),
                        const Gap(16),
                        DatePickerWidget(
                          label: 'Date Expiration',
                          controller: _dateController,
                          value: selectedDate,
                        ),
                        const Gap(16),
                        ButtonWidget(text: 'Add Stock', onPressed: _saveStock)
                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
