import 'package:doctor_app_flutter/core/viewModel/patient_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/large_avatar.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/dr_app_embedded_error.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/material.dart';

import '../../../../config/shared_pref_kay.dart';
import '../../../../config/size_config.dart';
import '../../../../models/patient/lab_orders/lab_orders_req_model.dart';
import '../../../../models/patient/patiant_info_model.dart';
import '../../../../util/dr_app_shared_pref.dart';
import '../../../../widgets/shared/app_scaffold_widget.dart';
import '../../../../widgets/shared/app_texts_widget.dart';
import 'lab_result_secreen.dart';

DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

/* 
 *@author: Elham Rababah 
 *@Date:26/4/2020  
 *@param: 
 *@return:LabOrdersScreen
 *@desc:   LabOrdersScreen class
 */

class LabOrdersScreen extends StatefulWidget {
  @override
  _LabOrdersScreenState createState() => _LabOrdersScreenState();
}

class _LabOrdersScreenState extends State<LabOrdersScreen> {


  /* 
 *@author: Elham Rababah 
 *@Date:28/4/2020  
 *@param: context
 *@return:
 *@desc:   getLabResultOrders Function
 */
  getLabResultOrders(BuildContext context, PatientViewModel model) async {
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    String token = await sharedPref.getString(TOKEN);
    LabOrdersReqModel labOrdersReqModel = LabOrdersReqModel(
        patientID: patient.patientId,
        projectID: patient.projectId,
        tokenID: token,
        patientTypeID: patient.patientType??1,
        languageID: 2);

    model.getLabResultOrders(labOrdersReqModel.toJson());
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<PatientViewModel>(
      onModelReady: (model) => getLabResultOrders(context, model),
      builder: (_, model, w) =>
          AppScaffold(
            baseViewModel: model,
            appBarTitle: TranslationBase
                .of(context)
                .labOrders,
            body: model.patientLabResultOrdersList.length == 0
                ? DrAppEmbeddedError(
                error: TranslationBase
                    .of(context)
                    .errorNoLabOrders)
                : Container(
              margin: EdgeInsets.fromLTRB(
                  SizeConfig.realScreenWidth * 0.05,
                  0,
                  SizeConfig.realScreenWidth * 0.05,
                  0),
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 10),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(20.0),
                  ),
                ),
                child: ListView.builder(
                    itemCount:
                    model.patientLabResultOrdersList.length,
                    itemBuilder: (BuildContext context, int index) {
                      return InkWell(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) =>
                                  LabResult(
                                    labOrders: model
                                        .patientLabResultOrdersList[index],
                                  ),
                            ),
                          );
                        },
                        child: Container(
                          padding: EdgeInsets.all(10),
                          margin: EdgeInsets.all(10),
                          decoration: BoxDecoration(
                            borderRadius:
                            BorderRadius.all(Radius.circular(10)),
                            border: Border(
                              bottom: BorderSide(
                                  color: Colors.grey, width: 0.5),
                              top: BorderSide(
                                  color: Colors.grey, width: 0.5),
                              left: BorderSide(
                                  color: Colors.grey, width: 0.5),
                              right: BorderSide(
                                  color: Colors.grey, width: 0.5),
                            ),
                          ),
                          child: Column(
                            crossAxisAlignment:
                            CrossAxisAlignment.start,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  LargeAvatar(
                                    url: model
                                        .patientLabResultOrdersList[
                                    index]
                                        .doctorImageURL,
                                    name: model
                                        .patientLabResultOrdersList[
                                    index]
                                        .doctorName,
                                  ),
                                  Expanded(
                                    child: Padding(
                                      padding:
                                      const EdgeInsets.fromLTRB(
                                          8, 0, 0, 0),
                                      child: Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                        children: <Widget>[
                                          if(model
                                              .patientLabResultOrdersList[index]
                                              .doctorName != null )
                                          AppText(
                                            '${model
                                                .patientLabResultOrdersList[index]
                                                .doctorName}',
                                            fontSize: 1.7 *
                                                SizeConfig
                                                    .textMultiplier,
                                            fontWeight: FontWeight.w600,
                                          ),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          AppText(
                                              ' ${model
                                                  .patientLabResultOrdersList[index]
                                                  .projectName}',
                                              fontSize: 2 *
                                                  SizeConfig
                                                      .textMultiplier,
                                              color: Colors.grey[800]),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                            MainAxisAlignment.start,
                                            children: <Widget>[
                                              AppText(
                                                TranslationBase.of(context).invoiceNo,
                                                fontSize: 2 *
                                                    SizeConfig
                                                        .textMultiplier,
                                                color: Colors.grey[800],
                                              ),
                                              AppText(
                                                ' ${model
                                                    .patientLabResultOrdersList[index]
                                                    .invoiceNo}',
                                                fontSize: 2 *
                                                    SizeConfig
                                                        .textMultiplier,
                                                color: Colors.grey[800],
                                              ),
                                            ],
                                          ),SizedBox(
                                            height: 8,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                            MainAxisAlignment.start,
                                            children: <Widget>[
                                              AppText(
                                                TranslationBase.of(context).orderNo,
                                                fontSize: 2 *
                                                    SizeConfig
                                                        .textMultiplier,
                                                color: Colors.grey[800],
                                              ),
                                              AppText(
                                                ' ${model
                                                    .patientLabResultOrdersList[index]
                                                    .orderNo}',
                                                fontSize: 2 *
                                                    SizeConfig
                                                        .textMultiplier,
                                                color: Colors.grey[800],
                                              ),
                                            ],
                                          )
                                        ],
                                      ),
                                    ),
                                  )
                                ],
                              ),
                              SizedBox(
                                height: 3,
                              ),
                              Divider(
                                color: Colors.grey,
                              ),
                              SizedBox(
                                height: 3,
                              ),
                              Row(
                                children: <Widget>[
                                  Icon(
                                    EvaIcons.calendar,
                                    color: Colors.grey[700],
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Expanded(
                                    child: AppText(
                                      '${Helpers.getDate(model
                                          .patientLabResultOrdersList[index]
                                          .createdOn)}',
                                      fontSize: 2.0 *
                                          SizeConfig.textMultiplier,
                                    ),
                                  )
                                ],
                              )
                            ],
                          ),
                        ),
                      );
                    }),
              ),
            ),
          ),);
  }
}
