
import 'dart:async';

import 'package:fluro/fluro.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../../generated/l10n.dart';
import '../../routes.dart';
import '../../utils/http_util.dart';
import '../../utils/utils.dart';

class MobileRenewLicense extends StatefulWidget {
  final int businessId;

  const MobileRenewLicense(this.businessId, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MobileRenewLicenseState();
  }

}

class MobileRenewLicenseState extends State<MobileRenewLicense> {

  TextEditingController groupNumberController = TextEditingController();
  bool canSubmit = false;

  @override
  Widget build(BuildContext context) {
    Column col = Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [],
    );

    col.children.add(
      Container(
        padding: EdgeInsets.only(top: 20, left: 16, right: 16, bottom: 10,),
        child: Text(
          S.of(context).renew_license,
          style: TextStyle(
            fontSize: 28,
          ),
        ),
      ),
    );
    col.children.add(
      Container(
        padding: EdgeInsets.only(top: 0, left: 16, right: 16, bottom: 10),
        child: Text(
          S.of(context).group_number_can_be_found,
          style: TextStyle(
            color: Colors.black45,
          ),
        ),
      )
    );
    col.children.add(
      Container(
        padding: EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 10),
        child: Image.asset('assets/images/group_number.png',),
      ),
    );
    col.children.add(
      Container(
        padding: EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 10),
        child: Text(
          S.of(context).group_number,
          style: TextStyle(
            fontSize: 28,
          ),
        ),
      )
    );
    col.children.add(
      Container(
        padding: EdgeInsets.only(left: 16, right: 16, bottom: 0),
        child: TextFormField(
          controller: groupNumberController,
          decoration: new InputDecoration(
            enabledBorder: UnderlineInputBorder(
              borderSide: BorderSide(
                color: Colors.black12,
              ),
            ),
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(
                color: Colors.blue,
              ),
            ),
            labelText: S.of(context).group_number,
          ),
          style: TextStyle(
              fontSize: 18.0
          ),
          autofocus: true,
          validator: (String? value) {
            if (value.trim().isEmpty) {
              return S.of(context).please_enter_group_number;
            }
            return null;
          },
          onChanged: (string) {
            if (string.isNotEmpty) {
              canSubmit = true;
            } else {
              canSubmit = false;
            }
            setState(() {});
          },
        ),
      )
    );
    col.children.add(Container(
      padding: EdgeInsets.only(top: 20, left: 16, right: 16, bottom: 20),
      child: ElevatedButton(
        child: Text(
          S.of(context).submit,
        ),
        onPressed: canSubmit ? () {
          _submit();
        } : null,
      ),
    ));

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: (){
            Navigator.of(context).pop();
          },
        ),
        title: Text(S.of(context).renew_license),
        backgroundColor: Theme.of(context).primaryColor,
      ),
      body: SingleChildScrollView(
        child: col,
      ),
    );
  }

  void _submit() {
    HttpUtil.httpPost('v1/get-license-renewal',
          (response) {
        Routes.router.navigateTo(context, '/renew-minioffice/${response.data['id']}', replace: true);
      },
      body: {
        'group_name': groupNumberController.text.trim(),
      },
      isFormData: true,
    ).onError((error, stackTrace) {
      Utils.showMessageDialog(context, error);
    });
  }
}