import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/appletAPI.m.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/scroll/refreshIndicator.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/mobile/names.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/search_field.dart';
import 'package:flyinsono/views/wechat_mini_program/wx_public_widgets/wx_scaffold.dart';

import 'index.dart';

// 分时诊断列表
class WxDiagnosisPage extends FisView<WxDiagnosisController> {
  const WxDiagnosisPage({Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FisBuilder<WxDiagnosisController>(
      builder: (_) {
        return WxScaffold(
          body: FColumn(
            // 此处反向构建列表，为了让搜索栏阴影后渲染，否则阴影会被列表项遮挡
            verticalDirection: VerticalDirection.up,
            children: [
              FExpanded(
                child: _buildListView(),
              ),
              _buildHeadBar(),
            ],
          ),
          title: "分时诊断",
          parentRoute: MobileRouteNames.Wechat.MiniProgramHome,
        );
      },
    );
  }

  /// 构建诊断列表卡片
  FWidget _buildDiagnosisItem(
      {required int index,
      required RemedicalRecordInfo info,
      required VoidCallback onTap}) {
    return FInkWell(
      onTap: onTap,
      child: FContainer(
        margin: EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: Colors.grey[200],
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.2),
              blurRadius: 10,
              spreadRadius: 0,
              offset: Offset(1, 1), // changes position of shadow
            ),
          ],
        ),
        child: FColumn(
          children: [
            FRow(
              children: [
                FContainer(
                  margin: EdgeInsets.only(left: 10, top: 10),
                  child: FText(
                    '创建时间',
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.grey[600],
                    ),
                  ),
                ),
                FContainer(
                  margin: EdgeInsets.only(left: 10, top: 10),
                  child: FText(
                    // '2021-08-01 19:30',
                    info.createTime.toString().substring(0, 16),
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.grey[600],
                    ),
                  ),
                ),
                FExpanded(child: FContainer()),
                FContainer(
                  margin: EdgeInsets.only(right: 10, top: 10),
                  child: FText(
                    info.reportUploadStatus ? '报告已上传' : '报告未上传',
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.grey[600],
                    ),
                  ),
                ),
              ],
            ),
            FRow(
              children: [
                FExpanded(
                  child: FColumn(
                    children: [
                      FRow(
                        children: [
                          FExpanded(
                            child: FColumn(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                FContainer(
                                  margin: EdgeInsets.only(left: 10, top: 8),
                                  child: FText(
                                    info.patientName ?? '',
                                    style: TextStyle(
                                      fontSize: 18,
                                      fontWeight: FontWeight.bold,
                                      color: Color.fromARGB(255, 77, 77, 77),
                                    ),
                                  ),
                                ),
                                FContainer(
                                  margin: EdgeInsets.only(top: 4),
                                  child: FRow(
                                    children: [
                                      FContainer(
                                        margin: EdgeInsets.only(left: 10),
                                        child: FText(
                                          'ID:',
                                          style: TextStyle(
                                            fontSize: 14,
                                            color:
                                                Color.fromARGB(255, 77, 77, 77),
                                          ),
                                        ),
                                      ),
                                      FContainer(
                                        margin: EdgeInsets.only(left: 10),
                                        child: FText(
                                          info.patientId ?? '',
                                          style: TextStyle(
                                            fontSize: 14,
                                            fontWeight: FontWeight.bold,
                                            color:
                                                Color.fromARGB(255, 77, 77, 77),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                          FRow(
                            children: [
                              FContainer(
                                margin: EdgeInsets.only(right: 5),
                                width: 50,
                                height: 50,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8),
                                ),
                                child: FCenter(
                                  child: FInkWell(
                                    onTap: () {
                                      controller.handleOpenImageList(index);
                                      print('点击查看图像列表');
                                    },
                                    child: FIcon(
                                      FIcons.scan_image,
                                      size: 40,
                                      color: Color.fromARGB(255, 77, 77, 77),
                                    ),
                                  ),
                                ),
                              ),
                              FContainer(
                                width: 50,
                                height: 50,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8),
                                ),
                                child: FCenter(
                                  child: FInkWell(
                                    onTap: () {
                                      controller.handleOpenReportList(index);
                                      print('点击查看报告列表');
                                    },
                                    child: FIcon(
                                      FIcons.report_a4,
                                      size: 40,
                                      color: Color.fromARGB(255, 77, 77, 77),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          )
                        ],
                      ),
                      FContainer(
                        margin: EdgeInsets.only(top: 4, bottom: 10),
                        child: FRow(
                          children: [
                            FContainer(
                              margin: EdgeInsets.only(left: 10),
                              child: FText(
                                '来自于:',
                                style: TextStyle(
                                  fontSize: 14,
                                  color: Color.fromARGB(255, 77, 77, 77),
                                ),
                              ),
                            ),
                            FContainer(
                              margin: EdgeInsets.only(left: 10),
                              child: FText(
                                info.organizationDesc ?? '',
                                style: TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.bold,
                                  color: Color.fromARGB(255, 77, 77, 77),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  /// 构建顶部（搜索+新建）
  FWidget _buildHeadBar() {
    return FContainer(
      height: 60,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Color.fromARGB(255, 247, 247, 247),
            Color.fromARGB(255, 241, 241, 241),
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.3),
            blurRadius: 5,
            spreadRadius: 1,
            offset: Offset(0, 1), // changes position of shadow
          ),
        ],
      ),
      child: SearchField(
        textEditingController: controller.searchBarTextController,
        onSearch: controller.handleSearch,
      ),
    );
  }

  // 卡片列表视图
  FWidget _buildListView() {
    return FRefreshIndicator(
      // key: controller.refreshIndicatorKey,
      onRefresh: controller.refreshList,
      child: FObx(
        () => controller.state.diagnosisList.isEmpty
            ? _buildLoadResult()
            : FListView.builder(
                padding: EdgeInsets.symmetric(vertical: 10),
                controller: controller.listViewScrollController,
                itemCount: controller.state.diagnosisList.length,
                itemBuilder: (context, index) {
                  /// 是否是已加载的最后一个
                  final isLoadedLast =
                      index == controller.state.diagnosisList.length - 1;
                  final isLast = index == controller.totalCount - 1;
                  final card = _buildDiagnosisItem(
                    index: index,
                    info: controller.state.diagnosisList[index],
                    onTap: () => controller.handleShowDetail(index),
                  );
                  if (!isLoadedLast) {
                    return card;
                  } else {
                    return FColumn(children: [
                      card,
                      FContainer(
                        height: 50,
                        child: FCenter(
                          child: FText(
                            isLast ? "没有更多数据" : i18nBook.common.loading.t,
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      )
                    ]);
                  }
                },
              ),
      ),
    );
  }

  FWidget _buildLoadResult() {
    return FCenter(child: FText("暂无数据"));
  }
}
