import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/DischargedPatientViewModel.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.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:doctor_app_flutter/widgets/shared/text_fields/app_text_field_custom_serach.dart';
import 'package:flutter/material.dart';

import '../../routes.dart';
import '../../widgets/shared/errors/error_message.dart';

class DischargedPatient extends StatefulWidget {
  final bool isVidaPlusProject;

  const DischargedPatient({super.key, required this.isVidaPlusProject});
  @override
  _DischargedPatientState createState() => _DischargedPatientState();
}

class _DischargedPatientState extends State<DischargedPatient> {
  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BaseView<DischargedPatientViewModel>(
        onModelReady: (model) => model.getDischargedPatient(),
        builder: (_, model, w) => AppScaffold(
              //appBarTitle: 'Discharged Patient',
              //subtitle: "Last Three Months",
              backgroundColor: Colors.grey[200]!,
              isShowAppBar: false,
              baseViewModel: model,
              body: model.myDischargedPatient.isEmpty
                  ? Center(
                      child: Container(
                      child: ErrorMessage(
                        error: TranslationBase.of(context).referralEmptyMsg,
                      ),
                    ))
                  : Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          SizedBox(
                            height: 12,
                          ),
                          AppTextFieldCustomSearch(
                            searchController: _controller,
                            onChangeFun: (value) {
                              model.searchData(value);
                            },
                            marginTop: 0,
                            suffixIcon: IconButton(
                              icon: Icon(
                                DoctorApp.filter_1,
                                color: Colors.black,
                              ),
                              iconSize: 20,
                              onPressed: () {},
                            ),
                            onFieldSubmitted: () {},
                          ),
                          SizedBox(
                            height: 5,
                          ),
                          Expanded(
                            child: SingleChildScrollView(
                              child: Column(
                                children: [
                                  ...List.generate(
                                      model.filterData.length,
                                      (index) => InkWell(
                                            onTap: () {
                                              Navigator.of(context).pushNamed(PATIENTS_PROFILE,
                                                  arguments: {"patient": model.filterData[index], "patientType": "1", "arrivalType":"1", "isSearch": false, "isInpatient": true, "isDischargedPatient": true});
                                            },
                                            child: Container(
                                              width: double.maxFinite,
                                              margin: EdgeInsets.all(8),
                                              padding: EdgeInsets.only(left: 0, right: 5, bottom: 5, top: 5),
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.circular(15),
                                                color: Colors.white,
                                              ),
                                              child: Column(
                                                children: [
                                                  Padding(
                                                      padding: EdgeInsets.only(left: 12.0),
                                                      child: Row(
                                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                        children: [
                                                          Row(children: [
                                                            Container(
                                                              width: 170,
                                                              child: AppText(
                                                                (Utils.capitalize(model.filterData[index].firstName) + " " + Utils.capitalize(model.filterData[index].lastName)),
                                                                fontSize: 16,
                                                                fontWeight: FontWeight.bold,
                                                                fontFamily: 'Poppins',
                                                                textOverflow: TextOverflow.ellipsis,
                                                              ),
                                                            ),
                                                            model.filterData[index].gender == 1
                                                                ? Icon(
                                                                    DoctorApp.male_2,
                                                                    color: Colors.blue,
                                                                  )
                                                                : Icon(
                                                                    DoctorApp.female_1,
                                                                    color: Colors.pink,
                                                                  ),
                                                          ]),
                                                          Row(
                                                            children: [
                                                              AppText(
                                                                model.filterData[index].nationalityName != null
                                                                    ? model.filterData[index].nationalityName!.trim()
                                                                    : model.filterData[index].nationality != null
                                                                        ? model.filterData[index].nationality!.trim()
                                                                        : model.filterData[index].nationalityId != null
                                                                            ? model.filterData[index].nationalityId!
                                                                            : "",
                                                                fontWeight: FontWeight.bold,
                                                                fontSize: 14,
                                                                textOverflow: TextOverflow.ellipsis,
                                                              ),
                                                              model.filterData[index].nationality != null || model.filterData[index].nationalityId != null
                                                                  ? ClipRRect(
                                                                      borderRadius: BorderRadius.circular(20.0),
                                                                      child: Image.network(
                                                                        model.filterData[index].nationalityFlagURL != null ? model.filterData[index].nationalityFlagURL! : '',
                                                                        height: 25,
                                                                        width: 30,
                                                                        errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
                                                                          return AppText(
                                                                            '',
                                                                            fontSize: 10,
                                                                          );
                                                                        },
                                                                      ))
                                                                  : SizedBox()
                                                            ],
                                                          )
                                                        ],
                                                      )),
                                                  Row(
                                                    children: <Widget>[
                                                      Column(
                                                        mainAxisAlignment: MainAxisAlignment.start,
                                                        children: <Widget>[
                                                          Padding(
                                                            padding: EdgeInsets.only(left: 12.0),
                                                            child: Container(
                                                              width: 60,
                                                              height: 60,
                                                              child: Image.asset(
                                                                model.filterData[index].gender == 1 ? 'assets/images/male_avatar.png' : 'assets/images/female_avatar.png',
                                                                fit: BoxFit.cover,
                                                              ),
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                      SizedBox(
                                                        width: 10,
                                                      ),
                                                      Expanded(
                                                        child: Column(
                                                          crossAxisAlignment: CrossAxisAlignment.start,
                                                          children: [
                                                            Container(
                                                              child: RichText(
                                                                text: new TextSpan(
                                                                  style: new TextStyle(fontSize: 2.0 * SizeConfig.textMultiplier!, color: Colors.black),
                                                                  children: <TextSpan>[
                                                                    new TextSpan(text: TranslationBase.of(context).fileNumber, style: TextStyle(fontSize: 14, fontFamily: 'Poppins')),
                                                                    new TextSpan(
                                                                        text: model.filterData[index].patientId.toString(),
                                                                        style: TextStyle(fontWeight: FontWeight.w700, fontFamily: 'Poppins', fontSize: 15)),
                                                                  ],
                                                                ),
                                                              ),
                                                            ),
                                                            Container(
                                                              child: RichText(
                                                                text: new TextSpan(
                                                                  style: new TextStyle(
                                                                    fontSize: 2.0 * SizeConfig.textMultiplier!,
                                                                    color: Colors.black,
                                                                    fontFamily: 'Poppins',
                                                                  ),
                                                                  children: <TextSpan>[
                                                                    new TextSpan(
                                                                        text: model.filterData[index].admissionDate == null ? "" : TranslationBase.of(context).admissionDate + " : ",
                                                                        style: TextStyle(fontSize: 14)),
                                                                    new TextSpan(
                                                                        text: model.filterData[index].admissionDate == null
                                                                            ? ""
                                                                            : "${AppDateUtils.convertDateFromServerFormat(model.filterData[index].admissionDate.toString(), 'yyyy-MM-dd')}",
                                                                        style: TextStyle(fontWeight: FontWeight.w700, fontSize: 15)),
                                                                  ],
                                                                ),
                                                              ),
                                                            ),
                                                            Container(
                                                              child: RichText(
                                                                text: new TextSpan(
                                                                  style: new TextStyle(
                                                                    fontSize: 2.0 * SizeConfig.textMultiplier!,
                                                                    color: Colors.black,
                                                                    fontFamily: 'Poppins',
                                                                  ),
                                                                  children: <TextSpan>[
                                                                    new TextSpan(text: model.filterData[index].dischargeDate == null ? "" : "Discharge Date : ", style: TextStyle(fontSize: 14)),
                                                                    new TextSpan(
                                                                        text: model.filterData[index].dischargeDate == null
                                                                            ? ""
                                                                            : "${AppDateUtils.convertDateFromServerFormat(model.filterData[index].dischargeDate.toString(), 'yyyy-MM-dd')}",
                                                                        style: TextStyle(fontWeight: FontWeight.w700, fontSize: 15)),
                                                                  ],
                                                                ),
                                                              ),
                                                            ),
                                                            Row(
                                                              children: [
                                                                AppText(
                                                                  "${TranslationBase.of(context).numOfDays}: ",
                                                                  fontSize: 14,
                                                                  fontWeight: FontWeight.w300,
                                                                ),
                                                                AppText(
                                                                    "${AppDateUtils.convertStringToDate(model.filterData[index].dischargeDate!).difference(AppDateUtils.getDateTimeFromServerFormat(model.filterData[index].admissionDate!)).inDays + 1}",
                                                                    fontSize: 15,
                                                                    fontWeight: FontWeight.w700),
                                                              ],
                                                            ),
                                                          ],
                                                        ),
                                                      ),
                                                      Icon(
                                                        Icons.arrow_forward,
                                                        size: 24,
                                                      ),
                                                    ],
                                                  )
                                                ],
                                              ),
                                            ),
                                          )),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
            ));
  }
}
