import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/models/response.dart';
import 'package:aman_kassa_flutter/core/models/user.dart';
import 'package:aman_kassa_flutter/core/services/ApiService.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class InfoKkmView extends StatefulWidget {
  InfoKkmView();

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

class _InfoKkmViewState extends State<InfoKkmView> {
  DateFormat dateFormat = DateFormat("dd.MM.yyyy HH:mm:ss");
  ApiService _apiService = locator<ApiService>();
  List<dynamic> data = [];
  bool loading = false;
  @override
  void initState() {
    super.initState();
    load();
  }

  load() async {
    setState(() {
      loading = true;
    });
    User user = Redux.store!.state.userState!.user!;
    Response<dynamic> response = await _apiService.infoKkm(user.token!);
    if (response.operation) {
      List<dynamic> list = [];
      for (var key in response.body.keys) {
        switch (key) {
          case 'sn':
            list.add({'key': 'Серийный номер', 'value': response.body[key]});
            break;
          case 'name':
            list.add(
                {'key': 'Наименование компании', 'value': response.body[key]});
            break;
          case 'biniin':
            list.add({'key': 'ИИН/БИН', 'value': response.body[key]});
            break;
          case 'address':
            list.add({'key': 'Адрес', 'value': response.body[key]});
            break;
          case 'kkmreg':
            list.add({
              'key': 'Регистрационный номер в органах НК',
              'value': response.body[key]
            });
            break;
          default:
        }
      }
      setState(() {
        data = list;
      });
    }
    setState(() {
      loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Информация о ККМ'),
      ),
      body: loading
          ? Container(child: Center(child: CircularProgressIndicator()))
          : SingleChildScrollView(
              child: Container(
                margin: const EdgeInsets.all(8.0),
                child: Table(
                  //defaultVerticalAlignment: TableCellVerticalAlignment.top,
                  children: data.map((e) {
                    return this.buildRow(e['key'], e['value']);
                  }).toList(),
                ),
              ),
            ),
    );
  }

  TableRow buildRow(String key, String value) {
    return TableRow(
      children: <Widget>[
        Container(
          child: Text(
            '$key :',
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 15.0,
            ),
          ),
          margin: EdgeInsets.only(bottom: 8.0),
        ),
        Container(
          child: Text(
            value,
            style: TextStyle(fontSize: 15.0),
          ),
          margin: EdgeInsets.only(bottom: 8.0),
        ),
      ],
    );
  }
}
