
import 'dart:async';

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

import '../../constants.dart';
import '../../generated/l10n.dart';
import '../../routes.dart';
import '../../utils/http_util.dart';
import '../../utils/utils.dart';
import '../../widgets/general/bottom_nav.dart';
import '../../widgets/general/breadcrumbs.dart';
import '../../widgets/general/navigationbar.dart';

class DesktopRenewLicense extends StatefulWidget {
  final int businessId;

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

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

}

class DesktopRenewLicenseState extends State<DesktopRenewLicense> {
  double sideSpace = 0;
  double mainSpace = 1200;

  TextEditingController groupNumberController = TextEditingController();

  bool canSubmit = false;

  @override
  Widget build(BuildContext context) {
    if (MediaQuery.of(context).size.width <= 1200) {
      mainSpace = MediaQuery.of(context).size.width;
      sideSpace = 0;
    } else {
      mainSpace = 1200;
      sideSpace = (MediaQuery.of(context).size.width - 1200) / 2;
    }

    Row row = Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [],
    );

    row.children.add(
      Expanded(
        child: Container(
          padding: EdgeInsets.only(left: 16.0, right: 16.0, top: 20, bottom: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: EdgeInsets.only(bottom: 4.0),
                child: Text(
                  S.of(context).renew_license,
                  style: TextStyle(
                    fontSize: 28,
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 4, bottom: 4),
                child: Text(
                  S.of(context).group_number_can_be_found,
                  style: TextStyle(
                    color: Colors.black45,
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 4),
                child: Image.asset(
                  'assets/images/group_number.png',
                  width: 400,
                ),
              ),
            ],
          ),
          decoration: BoxDecoration(
            border: Border(
              right: BorderSide(
                width: 1,
                color: Colors.black12,
              ),
            ),
          ),
        ),
      ),
    );
    row.children.add(
      Expanded(
        child: Container(
          padding: EdgeInsets.only(left: 16.0, right: 16.0, top: 20, bottom: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: EdgeInsets.only(bottom: 4,),
                child: Text(
                  S.of(context).group_number,
                  style: TextStyle(
                    fontSize: 28,
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 4,),
                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(() {});
                  },
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 20),
                child: ElevatedButton(
                  child: Text(
                    S.of(context).submit,
                  ),
                  onPressed: canSubmit ? () {
                    _submit();
                  } : null,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    return Scaffold(
      appBar: MiniNavigationBar(
        title: S.of(context).blog,
        back: true,
        breadCrumbs: [
          BreadCrumb(S.of(context).renew_license, null),
        ],
        breadCrumbHeight: Constants.BREADCRUMB_HEIGHT,
      ),
      body: SingleChildScrollView(
        child: Row(
          children: [
            Container(
              width: sideSpace,
            ),
            Expanded(child: row,),
            Container(
              width: sideSpace,
            ),
          ],
        ),
      ),
      bottomNavigationBar: BottomNav(),
    );
  }

  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);
    });
  }

}