import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

import '../../../../app/themes/app_text.dart';
import './log_data_view_model.dart';

class LogDataView extends StatelessWidget {
  const LogDataView({super.key});

  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder<LogDataViewModel>.reactive(
      viewModelBuilder: () => LogDataViewModel(),
      onViewModelReady: (LogDataViewModel model) async {
        await model.init();
      },
      fireOnViewModelReadyOnce: true,
      createNewViewModelOnInsert: true,
      builder: (
        BuildContext context,
        LogDataViewModel model,
        Widget? child,
      ) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.all(30),
            child: model.isBusy
                ? const Center(
                    child: CircularProgressIndicator(),
                  )
                : SingleChildScrollView(
                    child: Table(
                      border: TableBorder.all(),
                      children: [
                        const TableRow(
                          children: [
                            TableCell(
                              child: Center(
                                child: Text(
                                  'Waktu',
                                  style: boldTextStyle,
                                ),
                              ),
                            ),
                            TableCell(
                              child: Center(
                                child: Text(
                                  'Status',
                                  style: boldTextStyle,
                                ),
                              ),
                            ),
                            TableCell(
                              child: Center(
                                child: Text(
                                  'Download',
                                  style: boldTextStyle,
                                ),
                              ),
                            ),
                            TableCell(
                              child: Center(
                                child: Text(
                                  'Upload',
                                  style: boldTextStyle,
                                ),
                              ),
                            ),
                          ],
                        ),
                        ...model.otherFunction.listDataOnt.isEmpty
                            ? [
                                TableRow(
                                  children: List.generate(
                                    4, // Ensure the same number of columns
                                    (index) => TableCell(
                                      child: Padding(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 10),
                                        child: Text(
                                          "Loading...",
                                          style: regularTextStyle,
                                          textAlign: TextAlign.center,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ]
                            : model.otherFunction.listDataOnt
                                .map((data) => TableRow(
                                      children: [
                                        TableCell(
                                          child: Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 10),
                                            child: Text(
                                              data.getFormattedDate(),
                                              style: regularTextStyle,
                                              textAlign: TextAlign.center,
                                            ),
                                          ),
                                        ),
                                        TableCell(
                                          child: Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 10),
                                            child: Text(
                                              data.status,
                                              style: regularTextStyle,
                                              textAlign: TextAlign.center,
                                            ),
                                          ),
                                        ),
                                        TableCell(
                                          child: Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 10),
                                            child: Text(
                                              data.download == "-"
                                                  ? "-"
                                                  : data.download + " Mbps",
                                              style: regularTextStyle,
                                              textAlign: TextAlign.center,
                                            ),
                                          ),
                                        ),
                                        TableCell(
                                          child: Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 10),
                                            child: Text(
                                              data.upload == "-"
                                                  ? "-"
                                                  : data.upload + " Mbps",
                                              style: regularTextStyle,
                                              textAlign: TextAlign.center,
                                            ),
                                          ),
                                        ),
                                      ],
                                    ))
                                .toList(),
                      ],
                    ),
                  ),
          ),
        );
      },
    );
  }
}
