import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/lab/components/fixed_height_grid_delegate.dart';
import 'package:flyinsono/lab/components/lab_loading_wrapper.dart';
import 'package:flyinsono/lab/components/operate_button/mini_operate_button.dart';
import 'package:flyinsono/lab/pages/lab_data/controller.dart';
import 'package:flyinsono/lab/pages/lab_data/widgets/record_data_item.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'package:flyinsono/lab/components/operate_button/index.dart';
import 'package:flyinsono/lab/manager/page_manager.dart';
import 'package:flyinsono/lab/pages/lab_export/entitys/export_init_arguments.dart';
import 'package:flyinsono/lab/router/lab_route_names.dart';
import 'package:fis_common/index.dart';

class RecordList extends GetView<LabDataController> {
  const RecordList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabDataController>(
      id: LabDataController.recordListId,
      builder: (context) {
        return Container(
          height: 155,
          decoration: LabBoxDecoration.base,
          child: LabLoadingWrapper(
            decoration: LabBoxDecoration.base,
            loading: controller.isRecordListLoading,
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: _buildRecordData(),
            ),
          ),
        );
      },
    );
  }

  Widget _buildRecordData() {
    // if (controller.isRecordListLoading) {
    //   return LabCenterLoading();
    // }
    return Obx(() {
      final selectedIndex = controller.state.selectedRecordIndex;
      return GridView.builder(
        shrinkWrap: true,
        // itemCount: list.length,
        gridDelegate: FixedHeightGridDelegate(
          crossAxisCount: 4,
          crossAxisSpacing: 10,
          mainAxisSpacing: 10,
          height: 60, //48 dp of height
        ),
        itemCount: controller.recordList.length,
        itemBuilder: (BuildContext context, int index) {
          return _buildDataItem(index, selectedIndex);
        },
      );
    });
  }

  Widget _buildDataItem(int index, int selectedIndex) {
    return RecordDataDataItem(
      recordDataDescription: controller.recordList[index].description,
      recordDataCreateTime:
          controller.recordList[index].createTime.toString().substring(0, 10),
      isSelected: selectedIndex == index,
      imageCount: controller.recordList[index].imageCount,
      onClick: () {
        controller.clickRecordItem(index);
      },
      operationBuilder: () {
        return Row(
          mainAxisAlignment: MainAxisAlignment.end,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Flexible(
              child: MiniOperateButton(
                operateName: '批量分析',
                iconData: Icons.bar_chart_rounded,
                onClick: () {
                  var imageCount = controller.recordList[index].imageCount;
                  if (imageCount == 0) {
                    PromptBox.toast('该样本尚无图像');
                    return;
                  }
                  controller.enterBatchAnalysis();
                },
              ),
            ),
            if (FPlatform.isWindows || kDebugMode)
              Flexible(
                child: MiniOperateButton(
                  operateName: '导出',
                  iconData: Icons.file_upload_outlined,
                  onClick: () {
                    var imageCount = controller.recordList[index].imageCount;
                    if (imageCount == 0) {
                      PromptBox.toast('该样本尚无图像');
                      return;
                    }
                    PageManager.ins.openNewTab(
                      LabRouteNames.Export,
                      "数据导出",
                      args: LabExportInitArguments(
                        labExportPatientList: controller.selectedPatientList,
                        projectCode: controller.selectedDataSourceId,
                      ),
                    );
                  },
                ),
              ),
            Flexible(
              child: MiniOperateButton(
                operateName: '查看/编辑',
                iconData: Icons.drive_file_rename_outline_rounded,
                onClick: () {
                  var imageCount = controller.recordList[index].imageCount;
                  if (imageCount == 0) {
                    PromptBox.toast('该样本尚无图像');
                    return;
                  }
                  controller.editRecord();
                },
              ),
            ),
            Flexible(
              child: MiniOperateButton(
                operateName: '删除',
                iconData: Icons.delete_outline_rounded,
                onClick: () {
                  controller.deleteRecord();
                },
              ),
            ),
          ],
        );
      },
    );
  }
}
