import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/viewModels/medical/ask_doctor_view_model.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/medical/balance/new_text_Field.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RequestTypePage extends StatefulWidget {
  final DoctorList? doctorList;

  RequestTypePage({Key? key, this.doctorList});

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

class _RequestTypePageState extends State<RequestTypePage> {
  String selected = "";
  int parameterCode = -1;
  String question = "";

  @override
  Widget build(BuildContext context) {
    return BaseView<AskDoctorViewModel>(
      // onModelReady: (model) => model.getQuestionTypes(),
      onModelReady: (model) => model.getCallRequestTypeLOVs(),
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).requestType,
        baseViewModel: model,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        body: SingleChildScrollView(
          child: Container(
            margin: EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ...List.generate(
                  model.askDoctorReqTypes.length,
                  (index) => Container(
                    width: double.maxFinite,
                    height: 55,
                    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.0)),
                    margin: EdgeInsets.all(8.0),
                    child: InkWell(
                      onTap: () {
                        setState(() {
                          selected = model.askDoctorReqTypes[index].description!;
                          parameterCode = model.askDoctorReqTypes[index].parameterCode!;
                        });
                      },
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(model.askDoctorReqTypes[index].description!),
                          ),
                          Radio(
                            value: model.askDoctorReqTypes[index].description,
                            groupValue: selected,
                            activeColor: Colors.red[800],
                            onChanged: (value) {
                              setState(() {
                                selected = value!;
                                parameterCode = model.askDoctorReqTypes[index].parameterCode!;
                              });
                            },
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                if (parameterCode == 6)
                  Container(
                    margin: EdgeInsets.only(left: 10, right: 10),
                    child: NewTextFields(
                      hintText: TranslationBase.of(context).questionHere,
                      minLines: 2,
                      maxLines: 15,
                      onChanged: (value) {
                        setState(() {
                          question = value;
                        });
                      },
                    ),
                  ),
                Container(
                  width: double.maxFinite,
                  height: MediaQuery.of(context).size.height * 0.2,
                ),
              ],
            ),
          ),
        ),
        bottomSheet: Container(
          width: double.maxFinite,
          height: 80.0,
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Container(
              width: MediaQuery.of(context).size.width * 0.8,
              child: DefaultButton(
                TranslationBase.of(context).submit,
                () => {
                  model.sendRequestLOV(doctorList: widget.doctorList!, requestType: parameterCode.toString(), remark: question).then((value) {
                    if (model.state != ViewState.ErrorLocal && model.state != ViewState.Error) {
                      Navigator.pop(context);
                      AppToast.showSuccessToast(message: TranslationBase.of(context).RRTRequestSuccess);
                    }
                  })
                },
                color: Color(0xffD02127),
                textColor: Colors.white,
                disabledColor: Color(0xffEAEAEA),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
