
import 'dart:async';

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

import '../../constants.dart';
import '../../generated/l10n.dart';
import '../../routes.dart';
import '../../store/store.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 DesktopRenewMiniOffice extends StatefulWidget {
  final Map<String, dynamic> data;

  const DesktopRenewMiniOffice(this.data, {Key? key}) : super(key: key);

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

}

class DesktopRenewMiniOfficeState extends State<DesktopRenewMiniOffice> {
  double sideSpace = 0;
  double mainSpace = 1200;

  @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: [],
    );

    Column col1 = Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(bottom: 16),
          child: Text(
            S.of(context).group_license_renewal,
            style: TextStyle(
              fontSize: 28,
            ),
          ),
        ),
      ],
    );
    col1.children.add(
      Utils.buildLine(S.of(context).group_number, widget.data['group']['name'], valueSize: 18),
    );
    col1.children.add(
      Utils.buildLine(S.of(context).expiration_date,
        Utils.utcDatetimeStringToLocalDatetimeString(context, widget.data['expiration_date']),
        valueSize: 18
      ),
    );
    col1.children.add(
      Utils.buildLine(S.of(context).after_renewed,
        Utils.utcDatetimeStringToLocalDatetimeString(context, widget.data['renewed_expiration_date']),
        valueSize: 18
      ),
    );
    col1.children.add(
      Utils.buildLine(S.of(context).renewal_fee, '\$${widget.data['renewal_fee']}', valueSize: 18),
    );
    col1.children.add(
      Utils.buildLine(S.of(context).tax, '\$${widget.data['tax']}', valueSize: 18),
    );
    col1.children.add(
      Utils.buildLine(S.of(context).total, '\$${widget.data['renewal_total']}', valueSize: 38),
    );

    Column col2 = Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(left: 16, right: 16),
          child: ElevatedButton(
            child: Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 8),
              child: Text(
                S.of(context).pay_amount_token(widget.data['renewal_total']),
                style: TextStyle(
                  fontSize: 20,
                ),
              ),
            ),
            onPressed: () {
              _submit();
            },
          ),
        ),
      ],
    );

    row.children.add(
      Expanded(
        child: Container(
          padding: EdgeInsets.only(top: 20, left: 16, right: 16, bottom: 20),
          child: col1,
          decoration: BoxDecoration(
            border: Border(
              right: BorderSide(
                width: 1,
                color: Colors.black12,
              ),
            ),
          ),
        ),
      )
    );
    row.children.add(
      Expanded(
        child: Container(
          padding: EdgeInsets.only(top: 20, left: 16, right: 16, bottom: 20),
          child: col2,
        ),
      )
    );

    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() {
    if (store.state.user == null) {
      Utils.requireLogin(context, returnUrl: '/renew-minioffice/${widget.data['group']['id']}');
      return;
    }
    HttpUtil.httpPost('v1/create-minioffice-renewal-invoice',
    (response) {
      Routes.router.navigateTo(context, '/paynow/${response.data['order_id']}', replace: true);
    },
      body: widget.data,
    ).onError((error, stackTrace) {
      Utils.showMessageDialog(context, error);
    });
  }

}