import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:hmg_patient_app/core/viewModels/AlHabibMedicalService/cmc_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/AlHabibMedicalService/ComprehensiveMedicalCheckup/NewCMC/new_cmc_step_three_page.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.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/dragable_sheet.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/photo_view_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

import 'new_cmc_step_tow_page.dart';

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

  final double latitude;
  final double longitude;

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

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

class _NewCMCStepOnePageState extends State<NewCMCStepOnePage> {
  int selectedItem = 0;

  @override
  void initState() {
    super.initState();
  }

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

    return AppScaffold(
      isShowAppBar: false,
      baseViewModel: widget.model,
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              physics: BouncingScrollPhysics(),
              padding: EdgeInsets.all(21),
              child: Column(
                children: [
                  ListView.separated(
                      physics: NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemBuilder: (context, index) {
                        return Row(
                          children: [
                            Radio(
                                value: num.tryParse(widget.model.cmcAllServicesList[index].serviceID!),
                                activeColor: Colors.red[800],
                                onChanged: (newValue) async {
                                  selectedItem = index;
                                  PatientERCMCInsertServicesList patientERCMCInsertServicesList = PatientERCMCInsertServicesList(
                                      price: widget.model.cmcAllServicesList[index].price,
                                      serviceID: widget.model.cmcAllServicesList[index].serviceID,
                                      selectedServiceName: widget.model.cmcAllServicesList[index].text,
                                      selectedServiceNameAR: widget.model.cmcAllServicesList[index].textN,
                                      recordID: 1,
                                      totalPrice: widget.model.cmcAllServicesList[index].priceTotal,
                                      vAT: widget.model.cmcAllServicesList[index].priceVAT);
                                  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.only(
                                  left: 10,
                                  right: 10,
                                  top: 20,
                                  bottom: 20,
                                ),
                                child: Text(
                                  projectViewModel.isArabic ? widget.model.cmcAllServicesList[index].textN! : widget.model.cmcAllServicesList[index].text!,
                                  style: TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.w600,
                                    letterSpacing: -0.45,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        );
                      },
                      separatorBuilder: (context, index) {
                        return mDivider(Colors.grey);
                      },
                      itemCount: widget.model.cmcAllServicesList.length),
                  Stack(
                    children: [
                      Image.asset(
                        projectViewModel.isArabic ? "assets/images/cc_ar.png" : "assets/images/cc_en.png",
                        width: double.infinity,
                      ),
                      Align(
                        alignment: Alignment.topRight,
                        child: Container(
                          decoration: containerColorRadiusBorder(
                            Colors.black.withOpacity(0.2),
                            1000,
                            Colors.white,
                          ),
                          margin: EdgeInsets.all(12),
                          child: IconButton(
                            icon: SvgPicture.asset(
                              "assets/images/new/ic_zoom.svg",
                              color: Colors.white,
                            ),
                            padding: EdgeInsets.all(12),
                            onPressed: () {
                              showDraggableDialog(context, PhotoViewPage(projectViewModel.isArabic ? "assets/images/cc_ar.png" : "assets/images/cc_en.png"));
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
          Container(
            color: Colors.white,
            padding: EdgeInsets.only(top: 16, bottom: 16, right: 21, left: 21),
            child: DefaultButton(
              TranslationBase.of(context).next,
              () 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[selectedItem].price,
                    serviceID: widget.model.cmcAllServicesList[selectedItem].serviceID.toString(),
                    selectedServiceName: widget.model.cmcAllServicesList[selectedItem].text,
                    selectedServiceNameAR: widget.model.cmcAllServicesList[selectedItem].textN,
                    recordID: 1,
                    totalPrice: widget.model.cmcAllServicesList[selectedItem].priceTotal,
                    vAT: widget.model.cmcAllServicesList[selectedItem].priceVAT,
                  );

                  widget.cMCInsertPresOrderRequestModel.patientID = projectViewModel.user!.patientID;
                  widget.cMCInsertPresOrderRequestModel.patientOutSA = projectViewModel.user!.outSA;

                  widget.cMCInsertPresOrderRequestModel.patientERCMCInsertServicesList = [patientERCMCInsertServicesList];
                  navigateTo(
                    context,
                    NewCMCStepThreePage(
                      cmcInsertPresOrderRequestModel: widget.cMCInsertPresOrderRequestModel,
                      model: widget.model,
                    ),
                  );
                  // await widget.model.getCustomerInfo();
                  if (widget.model.state == ViewState.ErrorLocal) {
                    Utils.showErrorToast();
                  } else {
                    // navigateTo(
                    //   context,
                    //   NewCMCStepTowPage(
                    //     longitude: widget.longitude,
                    //     latitude: widget.latitude,
                    //     cmcInsertPresOrderRequestModel: widget.cMCInsertPresOrderRequestModel,
                    //     model: widget.model,
                    //   ),
                    // );
                  }
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}
