import 'package:hmg_patient_app/config/size_config.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/brand_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/input/text_field.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_pharmacy_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import '../Blood/new_text_Field.dart';

class SearchBrandsPage extends StatefulWidget {
  @override
  _SearchBrandsPageState createState() => _SearchBrandsPageState();
}

class _SearchBrandsPageState extends State<SearchBrandsPage> {
  final textController = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  String msg = '';

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectProvider = Provider.of(context);
    return BaseView<BrandsViewModel>(
      onModelReady: (model) => model!.searchProducts(productName: ''),
      builder: (BuildContext context, model, Widget? child) =>
          PharmacyAppScaffold(
        appBarTitle: TranslationBase.of(context).search,
        isBottomBar: false,
        isShowAppBar: true,
        backgroundColor: Colors.white,
        isShowDecPage: false,
        //baseViewModel: model,
        body: SingleChildScrollView(
          child: Container(
            height: SizeConfig.screenHeight,
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Row(
                    children: [
                      Container(
                        width: MediaQuery.of(context).size.width * 0.79,
                        child: Form(
                          key: _formKey,
                          child: NewTextFields(
                            autoFocus: true,
                            hintText: TranslationBase.of(context).search,
                            fontSize: 19.0,
                            prefixIcon: Icon(Icons.search),
                            inputAction: TextInputAction.search,
                            onSaved: (value) {
                              //searchMedicine(model, context);
                            },
                            onSubmit: (value) {
                              searchMedicine(model, context);
                              msg = TranslationBase.of(context).noSearchResultFound;
                            },
                            controller: textController,
//                            validator: (value) {
//                              if (value.isEmpty) {
//                                return 'please Enter Product Name';
//                              }
//                              return null;
//                            },
                          ),
                        ),
                      ),
                      SizedBox(
                        width: 10.0,
                      ),
                      InkWell(
                        child: Texts(
                          TranslationBase.of(context).cancel,
                          fontSize: 17.0,
                          fontWeight: FontWeight.w500,
                        ),
                        onTap: () {
                          Navigator.pop(context);
                        },
                      ),

                      // child: Container(
                      //   child: Button(
                      //       backgroundColor: Colors.green,
                      //       loading: model.state == ViewState.BusyLocal,
                      //       label: 'Search',
                      //       onTap: () {
                      //         searchMedicine(model, context);
                      //       }),
                      //   width: MediaQuery.of(context).size.width * 0.09,
                      // ),
                    ],
                  ),
                ),
                model.searchList.length == 0
                    ? Container(
                        child: Text(
                            TranslationBase.of(context).noData + model.searchList.length.toString()),
                      )
                    : Expanded(
                        child: Container(
                          child: ListView.builder(
                            itemCount: model.searchList.length,
                            itemBuilder: (BuildContext ctx, index) {
                              return Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: Container(
                                        child: Text( projectProvider.isArabic!
                                            ? model.searchList[index].namen!
                                          :model.searchList[index].name!,
                                          style: TextStyle(fontSize: 20),
                                        ),
                                      ),
                                    ),
                                    Divider(height: 1, color: Colors.grey)
                                  ],
                                ),
                              );
                            },
                          ),
                        ),
                      ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  searchMedicine(model, BuildContext context) {
    Utils.hideKeyboard(context);
    if (_formKey.currentState!.validate())
      model.searchProducts(productName: textController.text);
  }
}
