import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/staff/model/staff_model.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 'staff_editor.dart';

class StaffList extends StatefulWidget {
  @override
  _StaffListState createState() => _StaffListState();
}

class _StaffListState extends State<StaffList> {
  var dateFormatter = new DateFormat('dd MMM yyyy - hh:mm:ss a');
  final double dotSize = 15.0;
  bool _isLoading = false;

  @override
  Widget build(BuildContext context) {
    StaffModel staffModel = Provider.of<StaffModel>(context);

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          title: LocalText(
            context,
            'staff.list.title',
            color: Colors.white,
            fontSize: 20,
          ),
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.of(context)
                .push(CupertinoPageRoute(builder: (context) => StaffEditor()));
          },
          icon: Icon(Icons.add),
          label: LocalText(context, "staff.new", color: Colors.white),
          backgroundColor: primaryColor,
        ),
        body: new ListView.separated(
            separatorBuilder: (context, index) => Divider(
                  color: Colors.black,
                  height: 1,
                ),
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: staffModel.employees.length,
            itemBuilder: (BuildContext context, int index) {
              User user = staffModel.employees[index];
              return _item(user);
            }),
      ),
    );
  }

  Widget _item(User user) {
    return InkWell(
      onTap: () {
        Navigator.of(context).push(
            CupertinoPageRoute(builder: (context) => StaffEditor(staff: user)));
      },
      child: Row(
        children: <Widget>[
          Expanded(
            child: new Padding(
              padding: const EdgeInsets.symmetric(vertical: 10.0),
              child: new Row(
                children: <Widget>[
                  new Padding(
                    padding: new EdgeInsets.symmetric(
                        horizontal: 32.0 - dotSize / 2),
                    child: Icon(
                      MaterialCommunityIcons.worker,
                      color: primaryColor,
                      size: 40,
                    ),
                  ),
                  new Expanded(
                    child: new Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        new Text(
                          user.name,
                          style: new TextStyle(fontSize: 15.0),
                        ),
                        Padding(
                          padding: const EdgeInsets.only(top: 8.0),
                          child: new Text(
                            user.phoneNumber,
                            style: new TextStyle(
                                fontSize: 15.0, color: Colors.grey),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
