import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/brand_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacy_categorise_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/input/text_field.dart';
import 'package:diplomaticquarterapp/widgets/others/StarRating.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_pharmacy_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/network_base_view.dart';
import 'package:flutter/material.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) {
    return BaseView<BrandsViewModel>(
      onModelReady: (model) => model.searchProducts(),
      builder: (BuildContext context, model, Widget child) =>
          PharmacyAppScaffold(
        appBarTitle: '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: TextFields(
                            autoFocus: true,
                            hintText: 'Search',
                            fontSize: 19.0,
                            prefixIcon: Icon(Icons.search),
                            inputAction: TextInputAction.search,
                            onSaved: (value) {
                              //searchMedicine(model, context);
                            },
                            onSubmit: (value) {
                              searchMedicine(model, context);
                              msg = 'No Result Found';
                            },
                            controller: textController,
                            validator: (value) {
                              if (value.isEmpty) {
                                return 'please Enter Product Name';
                              }
                              return null;
                            },
                          ),
                        ),
                      ),
                      SizedBox(
                        width: 10.0,
                      ),
                      InkWell(
                        child: Texts(
                          '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(
                            'no data' + 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(
                                        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);
  }
}
