import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';

class AddDoctor extends StatefulWidget {
  const AddDoctor({Key? key}) : super(key: key);

  @override
  State<AddDoctor> createState() => _AddDoctorState();
}

class _AddDoctorState extends State<AddDoctor> {
  bool isPwdObscureText=true;
  bool isFirstAddButtonShow=false;
  bool isSecondAddButtonShow=false;
  bool is2ndPlaceOfPracticeControllerVisible=false;
  bool is3rdPlaceOfPracticeControllerVisible=false;
  bool isConfirmPwdObscureText=true;
  TextEditingController doctorIdController = TextEditingController();
  String? gender;
  List placeOfPractices=[];
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  bool isQrScannerVisible=false;
  String doctorId='';


  @override
  void initState() {

    super.initState();
  }



  Future<void> scanQR() async {
    String barcodeScanRes;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
          '#ff6666', 'Cancel', true, ScanMode.QR);
      print(barcodeScanRes);
      setState(() {
        doctorId=jsonDecode(barcodeScanRes)['doctorId'];
        doctorIdController.text=doctorId;
      });
    } on PlatformException {
      barcodeScanRes = 'Failed to get platform version.';
    }}



  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar:AppSettings.appBar('Add Doctor'),
        body: Stack(children: <Widget>[

          GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(new FocusNode());
            },
            child: SafeArea(
                child: SingleChildScrollView(
                    child: Padding(
                      padding: EdgeInsets.all(10),
                      child: Column(
                        children: <Widget>[
                          SizedBox(
                            height:MediaQuery.of(context).size.height * .02,
                          ),
                          Container(

                            child: Column(
                              children: [
                                IconButton(
                                  onPressed: (){
                                    scanQR();
                                  },
                                  icon: Icon(
                                    Icons.qr_code_scanner,
                                    color: primaryColor,
                                  ),
                                ),
                                Text('Scan Qr Code',style: TextStyle(fontSize: 12,color: secondaryColor),)
                              ],
                            )
                          ),

                          SizedBox(height:MediaQuery.of(context).size.height * .02,),
                          Container(
                            child: TextFormField(
                              cursorColor: greyColor,
                              controller: doctorIdController,
                              decoration: const InputDecoration(
                                prefixIcon: Icon(
                                  Icons.person,
                                  color: greyColor,
                                ),
                                border: OutlineInputBorder(
                                    borderSide: BorderSide(color: greyColor)),
                                focusedBorder: OutlineInputBorder(
                                  borderSide: BorderSide(color: greyColor),
                                ),
                                enabledBorder: OutlineInputBorder(
                                  borderSide: BorderSide(color: greyColor),
                                ),
                                labelText: 'Doctor Id',
                                labelStyle: TextStyle(
                                  color: greyColor, //<-- SEE HERE
                                ),
                              ),
                            ),
                          ),//name
                          SizedBox(height:MediaQuery.of(context).size.height * .02,),
                          Container(
                              width:double.infinity,
                              height: MediaQuery.of(context).size.height * .06,
                              child: ElevatedButton(
                                style: ElevatedButton.styleFrom(
                                  primary: primaryColor, // background
                                  onPrimary: Colors.white, // foreground
                                ),
                                onPressed: () async{
                                  if(doctorIdController.text!=''){
                                    AppSettings.preLoaderDialog(context);
                                    var payload = new Map<String, dynamic>();

                                    payload["doctorId"] =doctorIdController.text.toString();

                                    bool status = await AppSettings.addDoctor(payload);
                                    if(status){
                                      Navigator.of(context,rootNavigator: true).pop();
                                      AppSettings.longSuccessToast('Doctor added successfully');
                                      Navigator.pop(context);
                                    }
                                    else{
                                      Navigator.of(context,rootNavigator: true).pop();
                                      AppSettings.longFailedToast('There is a problem for adding doctor');
                                    }
                                  }
                                  else{
                                    AppSettings.longFailedToast('Please enter valid details');
                                  }

                                },
                                child: Text('Add Doctor'),
                              )
                          ),


                        ],
                      ),
                    )
                )),
          ),
        ]));
  }
}
