import 'package:diplomaticquarterapp/core/service/medical/labs_service.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/input/text_field.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class PassportUpdatePage extends StatefulWidget {
  @override
  _PassportUpdatePageState createState() => _PassportUpdatePageState();
}

class _PassportUpdatePageState extends State<PassportUpdatePage> {
  TextEditingController passportNumber = new TextEditingController();
  bool _isButtonDisabled;

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) => getPassportNumber());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).passportNumber,
      isShowAppBar: true,
      isBottomBar: true,
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                margin: EdgeInsets.all(45.0),
                child: Text(TranslationBase.of(context).enterPassportNumber, textAlign: TextAlign.center, style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold, color: Colors.black)),
              ),
              Container(
                margin: EdgeInsets.only(top: 5.0, bottom: 5.0),
                child: SvgPicture.asset("assets/images/new-design/passport.svg", width: 250.0, fit: BoxFit.fill),
              ),
              Container(
                  margin: EdgeInsets.only(left: 50.0, right: 50.0, top: 25.0),
                  child: TextFields(
                    keyboardType: TextInputType.text,
                    fontWeight: FontWeight.normal,
                    controller: passportNumber,
                    onChanged: (value) => {_onPassportTextChanged(value)},
                    padding: EdgeInsets.only(top: 20, bottom: 20, left: 10, right: 10),
                    hintText: TranslationBase.of(context).passportNumber,
                  ))
            ],
          ),
        ),
      ),
      bottomSheet: Container(
        margin: EdgeInsets.only(top: 10.0, left: 30.0, right: 30.0, bottom: 10.0),
        child: ButtonTheme(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          minWidth: MediaQuery.of(context).size.width,
          height: 45.0,
          child: RaisedButton(
            color: new Color(0xFFc5272d),
            textColor: Colors.white,
            disabledTextColor: Colors.white,
            disabledColor: Colors.red[300],
            onPressed: () {
              if (_isButtonDisabled == false)
                updatePassportNumber();
              else
                AppToast.showErrorToast(message: TranslationBase.of(context).validPassportNumber);
            },
            child: Text(TranslationBase.of(context).submit, style: TextStyle(fontSize: 18.0)),
          ),
        ),
      ),
    );
  }

  _onPassportTextChanged(content) {
    if (content.length >= 1) {
      setState(() {
        _isButtonDisabled = false;
      });
    } else {
      setState(() {
        _isButtonDisabled = true;
      });
    }
  }

  void updatePassportNumber() {
    LabsService service = new LabsService();
    GifLoaderDialogUtils.showMyDialog(context);

    service.updateCovidPassportNumber(passportNumber.text).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      Navigator.of(context).pop(true);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  void getPassportNumber() {
    LabsService service = new LabsService();
    GifLoaderDialogUtils.showMyDialog(context);

    service.getCovidPassportNumber().then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      print(res['Covid19_Certificate_GetPassportList'][0]['PassportNo']);
      passportNumber.text = res['Covid19_Certificate_GetPassportList'][0]['PassportNo'];
      if(res['Covid19_Certificate_GetPassportList'][0]['PassportNo'] != "") {
        _isButtonDisabled = false;
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }
}
