import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/ask_doctor_view_model.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/balance/new_text_Field.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/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.getCallRequestTypeLOVs(),
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).requestType,
        baseViewModel: model,
        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: MediaQuery.of(context).size.height * 0.1,
          height: 80.0,
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: SecondaryButton(
              label: TranslationBase.of(context).submit,
              loading: model.state == ViewState.BusyLocal,
              textColor: Colors.white,
              onTap: () {
                model
                    .sendRequestLOV(
                        doctorList: widget.doctorList,
                        requestType: parameterCode.toString(),
                        remark: question)
                    .then((value) {
                  if (model.state != ViewState.ErrorLocal ||
                      model.state != ViewState.Error) {
                    Navigator.pop(context);
                  }
                });
              },
              color: Colors.grey[800],
            ),
          ),
        ),
      ),
    );
  }
}
