import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/AlHabibMedicalService/cmc_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.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 'package:provider/provider.dart';

class NewCMCStepOnePage extends StatefulWidget {
  final CMCInsertPresOrderRequestModel cMCInsertPresOrderRequestModel;
  final Function changePageViewIndex;
  final CMCViewModel model;

  const NewCMCStepOnePage(
      {Key key,
      this.cMCInsertPresOrderRequestModel,
      this.model,
      this.changePageViewIndex})
      : super(key: key);

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

class _NewCMCStepOnePageState extends State<NewCMCStepOnePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    return AppScaffold(
      isShowAppBar: false,
      baseViewModel: widget.model,
      body: SingleChildScrollView(
        physics: ScrollPhysics(),
        child: Container(
          margin: EdgeInsets.all(12),
          child: Center(
            child: Column(
              children: [
                FractionallySizedBox(
                  widthFactor: 0.94,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: 20,
                      ),
                      Texts(
                        TranslationBase.of(context).selectService,
                        textAlign: TextAlign.center,
                      ),
                      Column(
                        children:
                            widget.model.cmcAllServicesList.map((service) {
                          return Container(
                            margin: EdgeInsets.only(top: 15),
                            decoration: BoxDecoration(
                                border:
                                    Border.all(color: Colors.grey, width: 1),
                                borderRadius: BorderRadius.circular(12),
                                color: Theme.of(context).primaryColor),
                            child: Column(
                              children: [
                                Row(
                                  children: [
                                    Radio(
                                        value: service.serviceID,
                                        activeColor: Colors.red[800],
                                        onChanged: (newValue) async {
                                          PatientERCMCInsertServicesList
                                              patientERCMCInsertServicesList =
                                              new PatientERCMCInsertServicesList(
                                                  price: service.price,
                                                  serviceID: service.serviceID
                                                      .toString(),
                                                  selectedServiceName:
                                                      service.description,
                                                  selectedServiceNameAR:
                                                      service.descriptionN,
                                                  recordID: 1,
                                                  totalPrice:
                                                      service.totalPrice,
                                                  vAT: service.vAT);
                                          setState(() {
                                            widget
                                                .cMCInsertPresOrderRequestModel
                                                .patientERCMCInsertServicesList = [
                                              patientERCMCInsertServicesList
                                            ];
                                          });
                                          CMCGetItemsRequestModel
                                              cMCGetItemsRequestModel =
                                              new CMCGetItemsRequestModel(
                                                  checkupType: newValue);
                                          await widget.model.getCheckupItems(
                                              cMCGetItemsRequestModel:
                                                  cMCGetItemsRequestModel);
                                        },
                                        groupValue: widget
                                                    .cMCInsertPresOrderRequestModel
                                                    .patientERCMCInsertServicesList
                                                    .length >
                                                0
                                            ? int.parse(widget
                                                .cMCInsertPresOrderRequestModel
                                                .patientERCMCInsertServicesList[
                                                    0]
                                                .serviceID)
                                            : 1),
                                    Expanded(
                                      child: Padding(
                                        padding: const EdgeInsets.all(20.0),
                                        child: Texts(
                                          projectViewModel.isArabic
                                              ? service.descriptionN
                                              : service.description,
                                          fontSize: 15,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          );
                        }).toList(),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 30,
                ),
                Container(
                  color: Theme.of(context).primaryColor,
                  width: double.infinity,
                  child: Column(
                    children: [
                      Row(
                        children: [
                          Container(
                              margin: EdgeInsets.only(right: 10, left: 10),
                              child: Texts(
                                TranslationBase.of(context).coveredService,
                                fontWeight: FontWeight.bold,
                              ))
                        ],
                      ),
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: widget.model.checkupItems.map((item) {
                            return Center(
                              child: FractionallySizedBox(
                                widthFactor: 1,
                                child: Container(
                                  margin: EdgeInsets.only(top: 15),
                                  decoration: BoxDecoration(
                                      color: Theme.of(context).primaryColor),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(
                                        height: 12,
                                      ),
                                      Container(
                                        width: double.infinity,
                                        padding: EdgeInsets.only(
                                            left: 15, bottom: 5, top: 5),
                                        decoration: BoxDecoration(
                                            border: BorderDirectional(
                                                bottom: BorderSide(
                                                    style: BorderStyle.solid,
                                                    width: 0.5,
                                                    color: Colors.grey)),
                                            //borderRadius: ,
                                            color:
                                                Theme.of(context).primaryColor),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              margin: EdgeInsets.only(
                                                  right: 10, left: 10),
                                              child: Texts(item.itemName,
                                                  fontSize: 15,
                                                  fontWeight: FontWeight.bold),
                                            ),
                                          ],
                                        ),
                                      ),
                                      SizedBox(
                                        height: 12,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            );
                          }).toList()),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
      bottomSheet: Container(
        height: MediaQuery.of(context).size.height * 0.10,
        width: double.infinity,
        child: Column(
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width * 0.9,
              child: SecondaryButton(
                label: TranslationBase.of(context).next,
                textColor: Theme.of(context).backgroundColor,
                color: Colors.grey[800],
                onTap: () async {
                  if (widget.cMCInsertPresOrderRequestModel
                              .patientERCMCInsertServicesList.length !=
                          0 ||
                      widget.cMCInsertPresOrderRequestModel
                              .patientERCMCInsertServicesList ==
                          null) {
                    int index = widget.model.cmcAllServicesList.length;
                    PatientERCMCInsertServicesList
                        patientERCMCInsertServicesList =
                        new PatientERCMCInsertServicesList(
                            price: widget
                                .model.cmcAllServicesList[index - 1].price,
                            serviceID: widget
                                .model.cmcAllServicesList[index - 1].serviceID
                                .toString(),
                            selectedServiceName: widget.model
                                .cmcAllServicesList[index - 1].description,
                            selectedServiceNameAR: widget.model
                                .cmcAllServicesList[index - 1].descriptionN,
                            recordID: 1,
                            totalPrice: widget
                                .model.cmcAllServicesList[index - 1].totalPrice,
                            vAT:
                                widget.model.cmcAllServicesList[index - 1].vAT);

                    widget.cMCInsertPresOrderRequestModel
                        .patientERCMCInsertServicesList = [
                      patientERCMCInsertServicesList
                    ];
                    await widget.model.getCustomerInfo();
                    if (widget.model.state == ViewState.ErrorLocal) {
                      Utils.showErrorToast();
                    } else {
                      widget.changePageViewIndex(1);
                    }
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
