//import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/models/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/procedures/add-procedure-form.dart';
import 'package:doctor_app_flutter/screens/procedures/update-procedure.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/patient-profile-header-new-design-app-bar.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';

import 'ProcedureCard.dart';

class ProcedureScreen extends StatelessWidget {
  String doctorNameP;

  void initState() async {
    Map profile = await sharedPref.getObj(DOCTOR_PROFILE);
    DoctorProfileModel doctorProfile = DoctorProfileModel.fromJson(profile);
    doctorNameP = doctorProfile.doctorName;
  }

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    String patientType = routeArgs['patientType'];
    String arrivalType = routeArgs['arrivalType'];

    return BaseView<ProcedureViewModel>(
      onModelReady: (model) =>
          model.getProcedure(mrn: patient.patientId, patientType: patientType),
      builder: (BuildContext context, ProcedureViewModel model, Widget child) =>
          AppScaffold(
        isShowAppBar: true,
        backgroundColor: Colors.grey[100],
        baseViewModel: model,
        appBar: PatientProfileHeaderNewDesignAppBar(
            patient, arrivalType ?? '0', patientType),
        body: SingleChildScrollView(
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 12,
                ),
                if (model.procedureList.length == 0 &&
                    patient.patientStatusType != 43)
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        AppText(
                          'Order Test or',
                          style: "caption2",
                          color: Colors.black,
                          fontSize: 13,
                        ),
                        AppText(
                          'Procedure',
                          bold: true,
                          fontSize: 22,
                        ),
                      ],
                    ),
                  ),
                if (patient.patientStatusType != null &&
                    patient.patientStatusType == 43)
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        AppText(
                          'Order Test or',
                          style: "caption2",
                          color: Colors.black,
                          fontSize: 13,
                        ),
                        AppText(
                          'Procedure',
                          bold: true,
                          fontSize: 22,
                        ),
                      ],
                    ),
                  ),
                if (patient.patientStatusType != null &&
                    patient.patientStatusType == 43)
                  InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => AddSelectedProcedure(
                                  patient: patient,
                                  model: model,
                                )),
                      );
                    },
                    child: Container(
                      width: double.maxFinite,
                      height: 140,
                      margin: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        color: Colors.grey[300],
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Center(
                        child: Container(
                          height: 90,
                          child: Column(
                            children: [
                              Container(
                                height: 40,
                                width: 40,
                                decoration: BoxDecoration(
                                  color: Colors.grey[600],
                                  borderRadius: BorderRadius.circular(10),
                                ),
                                child: Center(
                                  child: Icon(
                                    Icons.add,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              AppText(
                                'Add More Procedure',
                                color: Colors.grey[600],
                                fontWeight: FontWeight.w600,
                              )
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                if (model.procedureList.isNotEmpty)
                  ...List.generate(
                    model.procedureList[0].rowcount,
                    (index) => ProcedureCard(
                      categoryID:
                          model.procedureList[0].entityList[index].categoryID,
                      entityList: model.procedureList[0].entityList[index],
                      onTap: () {
                        if (model.procedureList[0].entityList[index].categoryID ==
                                2 ||
                            model.procedureList[0].entityList[index].categoryID == 4)
                          updateProcedureForm(context,
                              model: model,
                              patient: patient,
                              remarks: model
                                  .procedureList[0].entityList[index].remarks,
                              orderType: model
                                  .procedureList[0].entityList[index].orderType
                                  .toString(),
                              orderNo: model
                                  .procedureList[0].entityList[index].orderNo,
                              procedureName: model.procedureList[0]
                                  .entityList[index].procedureName,
                              categoreId: model
                                  .procedureList[0].entityList[index].categoryID
                                  .toString(),
                              procedureId: model.procedureList[0]
                                  .entityList[index].procedureId,
                              limetNo: model.procedureList[0].entityList[index]
                                  .lineItemNo);
                        // } else
                        //   Helpers.showErrorToast(
                        //       'You Cant Update This Procedure');
                      },
                      patient: patient,
                      doctorName: doctorNameP,
                    ),
                  ),
                if (model.state == ViewState.ErrorLocal || (model.procedureList.isNotEmpty && model.procedureList[0].entityList.isEmpty))

                  Center(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        SizedBox(
                          height: 100,
                        ),
                        Image.asset('assets/images/no-data.png'),
                        Padding(
                          padding: const EdgeInsets.all(22.0),
                          child: AppText(model.procedureList.isEmpty ? model.error: 'No Procedure Found '),
                        )
                      ],
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
