import 'package:hmg_patient_app/core/model/childvaccines/delete_baby_model.dart';
import 'package:hmg_patient_app/core/viewModels/child_vaccines/child_vaccines_view_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import '../add_newchild_page.dart';
import '../vaccinationtable_page.dart';

class ChildVaccinePage extends StatefulWidget {
  @override
  _ChildPageState createState() => _ChildPageState();
}

class _ChildPageState extends State<ChildVaccinePage> with SingleTickerProviderStateMixin {
  DeleteBaby deleteBaby = DeleteBaby();

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

    var checkedValue = true;
    return BaseView<ChildVaccinesViewModel>(
      onModelReady: (model) => model.getNewUserOrders(),
      builder: (_, model, widget) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).vaccination,
        baseViewModel: model,
        showNewAppBarTitle: true,
        showNewAppBar: true,
        body: Column(
          children: [
            Expanded(
              child: ListView.separated(
                itemBuilder: (context, index) {
                  Color selectedColor = model.babyInformationModelList[index].gender == 1 ? Color(0xFF5A282E) : Colors.white;
                  return InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        FadePage(
                          page: VaccinationTablePage(model.babyInformationModelList[index]),
                        ),
                      );
                    },
                    child: Container(
                      margin: EdgeInsets.only(left: 16, right: 16, top: index == 0 ? 16 : 0, bottom: index == model.babyInformationModelList.length - 1 ? 16 : 0),
                      decoration: BoxDecoration(
                        shape: BoxShape.rectangle,
                        borderRadius: BorderRadius.all(Radius.circular(12)),
                        color: Colors.white,
                        gradient: LinearGradient(
                          colors: model.babyInformationModelList[index].gender == 2
                              ? [
                                  Color(0xFFFDA4B0),
                                  Color(0xFFFBC8CC),
                                ]
                              : [
                                  Color(0xFF6EA8FF),
                                  Color(0xFF7AB8FD),
                                ],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                        ),
                      ),
                      padding: EdgeInsets.all(12),
                      //double.infinity,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Text(
                                  model.babyInformationModelList[index].babyName!.trim(),
                                  style: TextStyle(
                                    fontSize: 14,
                                    letterSpacing: -0.46,
                                    fontWeight: FontWeight.bold,
                                    color: selectedColor,
                                  ),
                                ),
                                Text(
                                  model.babyInformationModelList[index].genderDescription!,
                                  style: TextStyle(
                                    fontSize: 11,
                                    color: selectedColor,
                                  ),
                                ),
                                Row(
                                  children: [
                                    Text(
                                      TranslationBase.of(context).dob,
                                      style: TextStyle(
                                        fontSize: 11,
                                        color: selectedColor,
                                      ),
                                    ),
                                    Text(" " + DateUtil.getDayMonthYearDateFormatted(model.babyInformationModelList[index].dOB!),
                                        style: TextStyle(
                                          fontSize: 11,
                                          color: selectedColor,
                                        )),
                                  ],
                                ),
                                mHeight(20),
                              ],
                            ),
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Icon(
                                Icons.arrow_forward,
                                size: 18,
                              ),
                              mHeight(12),
                              InkWell(
                                onTap: () async {
                                  await model.deleteBabyOrders(newChild: deleteBaby);

                                  deleteBaby.babyID = model.babyInformationModelList[index].babyID;

                                  await model.deleteBabyOrders(newChild: deleteBaby);
                                  if (model.isDeleted) {
                                    AppToast.showSuccessToast(message: TranslationBase.of(context).recordDeleted);
                                    Navigator.pop(context, model.isDeleted);
                                  } else {
                                    //TODO handling error
                                  }
                                },
                                child: Container(
                                  decoration: containerRadius(Colors.grey.withOpacity(0.2), 2000),
                                  padding: EdgeInsets.only(left: 12, right: 12, top: 8, bottom: 8),
                                  child: Row(
                                    children: [
                                      Image.asset(
                                        'assets/images/new-design/garbage.png',
                                        width: 16,
                                        color: selectedColor,
                                      ),
                                      mWidth(8),
                                      Text(
                                        TranslationBase.of(context).deleteView,
                                        style: TextStyle(
                                          fontSize: 11,
                                          fontWeight: FontWeight.w600,
                                          letterSpacing: -0.33,
                                          color: selectedColor,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  );
                },
                separatorBuilder: (BuildContext context, int index) {
                  return mHeight(12);
                },
                itemCount: model.babyInformationModelList.length,
              ),
            ),
            Container(
              width: double.infinity,
              padding: EdgeInsets.all(16),
              color: Colors.white,
              child: DefaultButton(
                TranslationBase.of(context).addNewChild,
                () {
                  Navigator.push(
                    context,
                    FadePage(
                      page: AddNewChildPage(),
                    ),
                  ).then((value) {
                    if (value) model.getNewUserOrders();
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
