import 'dart:convert';
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:storemonitor/Screens/Constants.dart';
import 'package:storemonitor/Services/RemoteData.dart';
import '../Models/ListItem.dart';

class CheckList extends StatefulWidget {
  final String message;
  final List<ListItem> list = [];
  CheckList({super.key, required this.message});
  @override
  State<CheckList> createState() => _CheckList();
}

class _CheckList extends State<CheckList> {
  String? store;
  final TextEditingController one = TextEditingController();
  final TextEditingController two = TextEditingController();
  final TextEditingController three = TextEditingController();
  final TextEditingController four = TextEditingController();
  final TextEditingController five = TextEditingController();
  final TextEditingController six = TextEditingController();
  final TextEditingController seven = TextEditingController();
  final TextEditingController eight = TextEditingController();
  final TextEditingController nine = TextEditingController();
  final TextEditingController ten = TextEditingController();
  final TextEditingController eleven = TextEditingController();
  final TextEditingController tweleve = TextEditingController();
  final TextEditingController _dateTime = TextEditingController();
  DateTime _selectedDate = DateTime.now();

  @override
  void initState() {
    super.initState();
    store = widget.message;
    _selectedDate = DateTime.now();
    _dateTime.text = DateFormat('yyyy/MM/dd').format(_selectedDate);
  }

  Future<void> _selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
        context: context,
        initialDate: _selectedDate,
        firstDate: DateTime(1900),
        lastDate: DateTime.now(),
        builder: (context, child) {
          return Theme(
            data: Theme.of(context).copyWith(
              colorScheme: const ColorScheme.light(
                primary: Color.fromARGB(255, 125, 50, 253),
                onPrimary: Colors.white,
                onSurface: Colors.black,
              ),
              textButtonTheme: TextButtonThemeData(
                style: TextButton.styleFrom(
                  foregroundColor: const Color.fromARGB(255, 125, 50, 253),
                ),
              ),
            ),
            child: child!,
          );
        });
    setState(() {
      _selectedDate = picked!;
      _dateTime.text =
          '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'FootFall Verification',
          style: Constants.fontStyle,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: Constants.getReturn(),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: const EdgeInsets.all(15.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              TextFormField(
                controller: _dateTime,
                onTap: () {
                  //print('Date Picker');
                },
                decoration: InputDecoration(
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 15, horizontal: 10),
                  isDense: true,
                  hintText: 'Select date',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  suffixIcon: IconButton(
                    icon: Image.asset(
                      'assets/images/date_icon.png',
                      width: 24,
                      height: 24,
                    ),
                    onPressed: () {
                      _selectDate(context);
                    },
                  ),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    10-11:   ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: one,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    11-12:   ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: two,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    12-01:  ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: three,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    01-02: ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: four,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    02-03:',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: five,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    03-04:',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: six,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    04-05:',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: seven,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    05-06:',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60, // Adjust the width as needed
                    child: TextFormField(
                      controller: eight,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    06-07: ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60,
                    child: TextFormField(
                      controller: nine,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    07-08: ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60,
                    child: TextFormField(
                      controller: ten,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    08-09: ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60,
                    child: TextFormField(
                      controller: eleven,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text(
                    '    09-10:  ',
                    style: Constants.headerlistitems,
                  ),
                  const SizedBox(width: 8),
                  SizedBox(
                    width: 60,
                    child: TextFormField(
                      controller: tweleve,
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(4)],
                      decoration: const InputDecoration(
                        hintText: '0',
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 15,
              ),
              Container(
                decoration: BoxDecoration(
                  gradient: const LinearGradient(
                    colors: [Color(0xFF119887), Color(0xFF2bca83)],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ),
                  borderRadius: BorderRadius.circular(5),
                ),
                child: ElevatedButton(
                  onPressed: () async {
                    if (one.text.isNotEmpty ||
                        two.text.isNotEmpty ||
                        three.text.isNotEmpty ||
                        four.text.isNotEmpty ||
                        five.text.isNotEmpty ||
                        six.text.isNotEmpty ||
                        seven.text.isNotEmpty ||
                        eight.text.isNotEmpty ||
                        nine.text.isNotEmpty ||
                        ten.text.isNotEmpty ||
                        eleven.text.isNotEmpty ||
                        tweleve.text.isNotEmpty) {
                      bool isInternetConnected =
                          await Constants.checkInternetConnectivity();
                      if (isInternetConnected) {
                        showDialog(
                            context: context,
                            builder: (context) {
                              return const Center(
                                  child: CircularProgressIndicator(
                                color: Color.fromARGB(255, 173, 57, 230),
                              ));
                            });
                        List<Map<String, dynamic>> jsonArray = [
                          {
                            'hour': '10-11',
                            'count': one.text,
                          },
                          {
                            'hour': '11-12',
                            'count': two.text,
                          },
                          {
                            'hour': '12-01',
                            'count': three.text,
                          },
                          {
                            'hour': '01-02',
                            'count': four.text,
                          },
                          {
                            'hour': '02-03',
                            'count': five.text,
                          },
                          {
                            'hour': '03-04',
                            'count': six.text,
                          },
                          {
                            'hour': '04-05',
                            'count': seven.text,
                          },
                          {
                            'hour': '05-06',
                            'count': eight.text,
                          },
                          {
                            'hour': '06-07',
                            'count': nine.text,
                          },
                          {
                            'hour': '07-08',
                            'count': ten.text,
                          },
                          {
                            'hour': '08-09',
                            'count': eleven.text,
                          },
                          {
                            'hour': '09-10',
                            'count': tweleve.text,
                          },
                        ];

                        String jsonString = jsonEncode(jsonArray);
                        var res = await RemoteData().getFootFallVerify(
                            store!,
                            jsonString,
                            '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}');
                        Map<String, dynamic> responseMap =
                            jsonDecode(res!.body);
                        if (responseMap['status'] == 'Success') {
                          Navigator.pop(context);
                          setState(() {
                            one.text = '';
                            two.text = '';
                            three.text = '';
                            four.text = '';
                            five.text = '';
                            six.text = '';
                            seven.text = '';
                            eight.text = '';
                            nine.text = '';
                            ten.text = '';
                            eleven.text = '';
                            tweleve.text = '';
                          });
                        } else {
                          Navigator.pop(context);
                        }
                      } else {
                        Constants('CheckList', 'Enter Atleast one item').showAlertDialog(context, false);
                      }
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.transparent,
                    elevation: 0,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                  child: const SizedBox(
                    height: 50,
                    child: Center(
                      child: Text(
                        'Submit',
                        style: Constants.buttonsfontStyle,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
