import 'package:diplomaticquarterapp/core/model/er/projectavgerwaitingtime.dart';
import 'package:diplomaticquarterapp/core/service/er/er_service.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../uitl/translations_delegate_base.dart';
import 'widgets/card_position.dart';

class NearestEr extends StatefulWidget {
  static const String _url = "assets/images/";

  int appointmentNo;
  int projectID;

  NearestEr({this.appointmentNo, this.projectID});

  @override
  _NearestErState createState() => _NearestErState();
}

class _NearestErState extends State<NearestEr> {
  List<ProjectAvgERWaitingTime> projectAvgERWaitingTimeModelList = List();

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) => getERList());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
    final double itemWidth = size.width / 2;

    return AppScaffold(
      isShowAppBar: true,
      isShowDecPage: false,
      appBarTitle: TranslationBase.of(context).NearestEr,
      body: projectAvgERWaitingTimeModelList.length > 0
          ? Container(
              child: ListView(
                children: <Widget>[
                  Text("${TranslationBase.of(context).NearestErDesc}",
                      textAlign: TextAlign.center, style: TextStyle(fontSize: 18.0, letterSpacing: 1.0, fontWeight: FontWeight.w900, color: new Color(0xFF60686b))),
                  Container(
                    margin: EdgeInsets.fromLTRB(2.0, 10.0, 0.0, 10.0),
                    child: GridView.count(
                      crossAxisCount: 2,
                      childAspectRatio: (itemWidth / itemWidth),
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10,
                      controller: new ScrollController(keepScrollOffset: false),
                      shrinkWrap: true,
                      padding: const EdgeInsets.all(4.0),
                      children: List.generate(7, (index) {
                        return Container(
                          child: CardPosition(
                            waitingTime: projectAvgERWaitingTimeModelList[index].avgTimeInHHMM.toString(),
                            text: projectAvgERWaitingTimeModelList[index].projectName.toString(),
                            image: 'assets/images/new-design/find_us_icon.png',
                            subText: '${projectAvgERWaitingTimeModelList[index].distanceInKilometers ?? 0.0} ${TranslationBase.of(context).km_}',
                            type: projectAvgERWaitingTimeModelList[index].iD.toString(),
                            telephone: projectAvgERWaitingTimeModelList[index].phoneNumber.toString(),
                            networkImage: projectAvgERWaitingTimeModelList[index].projectImageURL.toString(),
                            latitude: projectAvgERWaitingTimeModelList[index].latitude,
                            longitude: projectAvgERWaitingTimeModelList[index].longitude,
                            projectname: projectAvgERWaitingTimeModelList[index].projectName,
                            cardSize: itemWidth,
                          ),
                        );
                      }),
                    ),
                  ),
                ],
              ),
            )
          : Center(
              child: Texts('No Data'),
            ),
    );
  }

  getERList() async {
    GifLoaderDialogUtils.showMyDialog(context);
    ErService service = new ErService();
    service.getProjectAvgERWaitingTimeOrders().then((response) {
      if (response['MessageStatus'] == 1) {
        setState(() {
          projectAvgERWaitingTimeModelList.clear();
          response['List_ProjectAvgERWaitingTime'].forEach((vital) {
            projectAvgERWaitingTimeModelList.add(ProjectAvgERWaitingTime.fromJson(vital));
          });
        });
        GifLoaderDialogUtils.hideDialog(context);
      } else {}
    }).catchError((err) {
      print(err);
      GifLoaderDialogUtils.hideDialog(context);
    });
  }
}
