import 'dart:ui';

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

import '../../congifs.dart';
import '../../utils/dialog_utils.dart';
import 'sync_logic.dart';

class SyncPage extends StatelessWidget {
  SyncPage({Key? key}) : super(key: key);
  final logic = Get.find<SyncLogic>();
  final state = Get.find<SyncLogic>().state;

  @override
  Widget build(BuildContext context) {
    getSyncItemList();

    return Scaffold(
      appBar: AppBar(
        title: const Text('数据同步',
          style: TextStyle(
            color: Colors.white,
          ),),
        backgroundColor: Colors.blue,
        centerTitle: true,
        actions: [
          InkWell(
            child: const Icon(
              Icons.save,
              color: Colors.red,
            ),
            onTap: () {},
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: GetBuilder<SyncLogic>(builder: (logic) {
              return ListView.builder(
                shrinkWrap: true,
                itemCount: state.items.length,
                itemBuilder: (context, index) {
                  return state.items[index];
                },
              );
            }),
          ),
          Row(
            children: [
              const SizedBox(width: 10),
              Expanded(
                child: Config.isOnLine
                    ? TextButton(
                        style: ButtonStyle(
                          padding: MaterialStateProperty.all(const EdgeInsets.all(10)),
                          foregroundColor: MaterialStateProperty.all(Colors.white),
                          backgroundColor: MaterialStateProperty.all(Colors.blue),
                        ),
                        onPressed: () => {
                          DialogUtils.showLoadingDialog('正在同步数据'),
                          state.isCattleResumeCompleteSync = false,
                          state.isPerformanceCompleteSync = false,
                          state.isWeightManCompleteSync = false,
                          state.isMedicalRecordCompleteSync = false,
                          state.isImmuneBatchCompleteSync = false,
                          state.isImmuneBatchDetailCompleteSync = false,
                          state.isBreedingRecordCompleteSync = false,
                          state.isBreedingRecordDetailCompleteSync = false,
                          logic.syncData(),
                        },
                        child: const Text('一键同步'),
                      )
                    : const SizedBox(),
              ),
              const SizedBox(width: 10),
            ],
          ),
          const SizedBox(height: 10),
        ],
      ),
    );
  }

  void getSyncItemList() {
    List<Widget> list = [];
    for (int i = 0; i < state.dataList.length; i++) {
      list.add(ListTile(
        title: Text('${state.dataList[i].title}-${state.dataList[i].allCount}'),
        subtitle: GetBuilder<SyncLogic>(builder: (logic) {
          return
              //   Text(
              //   '待上传:${state.dataList[i].count}, 错误数量:${state.dataList[i].errorCount},更新时间:${state.dataList[i].updateTime ?? ''}',
              //   maxLines: 1,
              //   overflow: TextOverflow.ellipsis,
              // )
              RichText(
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            text: TextSpan(
              children: [
                TextSpan(
                  text: '待上传:${state.dataList[i].count}',
                  style: const TextStyle(color: Colors.green),
                ),
                TextSpan(
                  text: '  错误数量:${state.dataList[i].errorCount}',
                  style: const TextStyle(color: Colors.red),
                ),
                TextSpan(
                  text: '  更新时间:${state.dataList[i].updateTime ?? ''}',
                  style: const TextStyle(color: Colors.black54),
                ),
              ],
            ),
          );
        }),
      ));
      list.add(SizedBox(
        height: 1,
        child: Container(
          height: 1,
          color: Colors.black12,
        ),
      ));
    }
    state.items = list;
    logic.update();
  }
}
