import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/invoice/model/invoice_model.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:provider/provider.dart';

import 'package:flutter/material.dart';
import 'invoice_editor.dart';
import 'invoice_list_row.dart';

class InvoiceList extends StatefulWidget {
  @override
  _InvoiceListState createState() => _InvoiceListState();
}

class _InvoiceListState extends State<InvoiceList> {
  bool _isLoading = false;

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

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var owner = true;

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: DefaultTabController(
        length: 2,
        child: Scaffold(
            appBar: AppBar(
              centerTitle: true,
              leading: new IconButton(
                icon: new Icon(Icons.close),
                onPressed: () => Navigator.of(context).pop(),
              ),
              backgroundColor: primaryColor,
              title: LocalText(context, 'invoices.title',
                  color: Colors.white, fontSize: 20),
              actions: <Widget>[
                IconButton(
                  icon: Icon(
                    Icons.search,
                    color: Colors.white,
                  ),
                  iconSize: 30,
                  // onPressed: () => showPlacesSearch(context),
                ),
              ],
              bottom: TabBar(
                unselectedLabelColor: Colors.grey,
                tabs: [
                  Tab(text: "Pending"),
                  Tab(text: "Paid"),
                ],
              ),
            ),
            floatingActionButton: owner
                ? FloatingActionButton.extended(
                    onPressed: () {
                      _newInvoice();
                    },
                    icon: Icon(Icons.add),
                    label:
                        Text(AppTranslations.of(context).text("invoices.add")),
                    backgroundColor: primaryColor,
                  )
                : null,
            body: TabBarView(
              children: [_pending(), _paided()],
            )),
      ),
    );
  }

  _newInvoice() {
    Navigator.of(context).push(BottomUpPageRoute(InvoiceEditor()));
  }

  Widget _pending() {
    var invoiceModel = Provider.of<InvoiceModel>(context);
    return Column(
      children: <Widget>[
        Expanded(
          child: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: invoiceModel.pending.length,
              itemBuilder: (BuildContext context, int index) {
                return InvoiceListRow(invoice: invoiceModel.pending[index]);
              }),
        ),
      ],
    );
  }

  Widget _paided() {
    var invoiceModel = Provider.of<InvoiceModel>(context);
    return Column(
      children: <Widget>[
        Expanded(
          child: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: invoiceModel.paided.length,
              itemBuilder: (BuildContext context, int index) {
                return InvoiceListRow(invoice: invoiceModel.paided[index]);
              }),
        ),
      ],
    );
  }
}
