import 'package:fcs/domain/entities/shipment.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/shipment/model/shipment_model.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/local_button.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:flutter_icons/flutter_icons.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import 'widgets.dart';

class ShipmentConfirm extends StatefulWidget {
  final Shipment shipment;
  ShipmentConfirm({this.shipment});

  @override
  _ShipmentConfirmState createState() => _ShipmentConfirmState();
}

class _ShipmentConfirmState extends State<ShipmentConfirm> {
  var dateFormatter = new DateFormat('dd MMM yyyy');
  var timeFormatter = new DateFormat('jm');
  TextEditingController _handlingFee = new TextEditingController();

  Shipment _shipment;
  bool _isLoading = false;
  var now = new DateTime.now();

  @override
  void initState() {
    super.initState();

    _shipment = widget.shipment;
    _handlingFee.text = _shipment.handlingFee?.toString() ?? "0";
  }

  @override
  Widget build(BuildContext context) {
    final shipmentNumberBox = getShipmentNumberStatus(context, _shipment);

    final handlingFeeBox = InputText(
      labelTextKey: "shipment.handling.fee",
      controller: _handlingFee,
      iconData: FontAwesome.truck,
    );
    final confirmbtn = LocalButton(
      textKey: "shipment.confirm.btn",
      callBack: _save,
    );
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(
              CupertinoIcons.back,
              color: primaryColor,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
          shadowColor: Colors.transparent,
          backgroundColor: Colors.white,
          title: LocalText(
            context,
            "shipment.confirm.menuitem",
            fontSize: 20,
            color: primaryColor,
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(10.0),
          child: ListView(
            children: <Widget>[
              Center(child: shipmentNumberBox),
              SizedBox(
                height: 10,
              ),
              SizedBox(
                height: 10,
              ),
              handlingFeeBox,
              confirmbtn,
            ],
          ),
        ),
      ),
    );
  }

  _save() async {
    _shipment.handlingFee = double.tryParse(_handlingFee.text) ?? 0;
    setState(() {
      _isLoading = true;
    });
    try {
      ShipmentModel shipmentModel =
          Provider.of<ShipmentModel>(context, listen: false);
      await shipmentModel.confirmShipment(_shipment);
      Navigator.pop(context, true);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
