import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:fcs/pages/main/util.dart';

typedef void ProfileCallback();

class TotalWeightEdit extends StatefulWidget {
  final double? totalWeight;

  const TotalWeightEdit({Key? key, this.totalWeight}) : super(key: key);
  @override
  _TotalWeightEditState createState() => _TotalWeightEditState();
}

class _TotalWeightEditState extends State<TotalWeightEdit> {
  final TextEditingController totalController = new TextEditingController();
  bool _loading = false;

  @override
  void initState() {
    super.initState();
    totalController.text = widget.totalWeight!.toStringAsFixed(2);
  }

  @override
  Widget build(BuildContext context) {
    final totalInputBox = InputText(
        labelTextKey: 'shipment.cargo.total',
        iconData: FontAwesomeIcons.weightHanging,
        textInputType: TextInputType.number,
        controller: totalController);

    final saveBtn =
        fcsButton(context, getLocalString(context, "btn.save"), callack: _save);

    return LocalProgress(
      inAsyncCall: _loading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: LocalText(
            context,
            "box.cargo_total_title",
            fontSize: 20,
            color: primaryColor,
          ),
          backgroundColor: Colors.white,
          shadowColor: Colors.transparent,
          leading: IconButton(
            icon: Icon(
              CupertinoIcons.back,
              size: 35,
              color: primaryColor,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        body: ListView(
          padding: EdgeInsets.all(18),
          children: <Widget>[totalInputBox, SizedBox(height: 30), saveBtn],
        ),
      ),
    );
  }

  _save() async {
    setState(() {
      _loading = true;
    });
    try {
      double total = double.parse(totalController.text, (s) => 0);
      Navigator.pop<double>(context, total);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _loading = false;
      });
    }
  }
}
