import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vitalapp/components/appbar.dart';
import 'package:vitalapp/consts/styles.dart';
import 'package:vitalapp/pages/check/health_check_record/controller.dart';
import 'package:vitalapp/pages/patient/list/widgets/status.dart';
import 'package:vitalapp/pages/widgets/record_common_item.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: HealthCheckRecordController(),
        id: "HealCheckRecord",
        builder: (_) {
          return Scaffold(
            backgroundColor: const Color.fromRGBO(238, 238, 238, 1),
            appBar: VAppBar(
              titleWidget: const Text('体检记录'),
            ),
            body: Stack(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _buildDiagram(),
                    _buildListView(),
                  ],
                )
              ],
            ),
          );
        });
  }

  Widget _buildDiagram() {
    return Expanded(
      flex: 1,
      child: Padding(
        padding: const EdgeInsets.all(16.0).copyWith(right: 0),
        child: Container(
          // color: Colors.white,
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(
              color: Colors.white,
            ),
            borderRadius: GlobalStyles.borderRadius,
          ),
          child: Image.asset(
            'assets/images/exam/normalMeasurementChart.png',
            height: double.infinity,
            fit: BoxFit.fitWidth,
          ),
        ),
      ),
    );
  }

  Widget _buildListView() {
    return Expanded(
        flex: 2,
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: RefreshIndicator(
              child: Obx(
                () {
                  final list = controller.state.examRecordDTOList;
                  final children = <Widget>[];
                  for (var examRecordDTO in list) {
                    var index = list.indexOf(examRecordDTO);

                    children.add(_ExamRecordCard(
                      dto: examRecordDTO,
                      dataDto: examRecordDTO.examRecordDatas,
                      index: index,
                    ));
                  }
                  return list.isEmpty
                      ? Container(
                          margin: const EdgeInsets.only(top: 80),
                          child: Column(
                            children: [
                              Center(
                                child: Image.asset(
                                  "assets/images/no_data.png",
                                  width: 300,
                                  height: 300,
                                  fit: BoxFit.cover,
                                ),
                              ),
                              const Text(
                                "暂无数据，先看看别的吧",
                                style: TextStyle(fontSize: 18),
                              ),
                            ],
                          ),
                        )
                      : GridView(
                          gridDelegate:
                              const SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 1,
                            mainAxisSpacing: 16,
                            crossAxisSpacing: 20,
                            childAspectRatio: 900 / 180,
                          ),
                          children: children,
                        );
                },
              ),
              onRefresh: () async {}),
        ));
  }
}

class _ExamRecordCard extends StatelessWidget {
  final ExamRecordDTO dto;
  final List<ExamRecordDataDTO>? dataDto;
  final int index;
  _ExamRecordCard({
    required this.dto,
    this.dataDto,
    required this.index,
  });
  final controller = Get.find<HealthCheckRecordController>();
  @override
  Widget build(BuildContext context) {
    final body = Stack(
      children: [
        Row(
          children: [
            Expanded(
              flex: 10,
              child: Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: 30,
                  vertical: 12,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const SizedBox(
                      height: 8,
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Wrap(
                      alignment: WrapAlignment.start,
                      spacing: 20,
                      runSpacing: 8,
                      children: [
                        SizedBox(
                          width: 200,
                          child: RecordCommonItem(
                            itemName: '姓名',
                            itemValue: dto.patientName ?? "",
                            fontSize: 18,
                          ),
                        ),
                        const SizedBox(
                          width: 20,
                        ),
                        RecordCommonItem(
                          itemName: '签约医生',
                          itemValue: dto.contractedDoctor ?? "",
                          fontSize: 18,
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Wrap(
                      children: [
                        RecordCommonItem(
                          itemName: '开始体检日期',
                          itemValue: DateFormat("yyyy-MM-dd")
                              .format(dto.examTime!.toLocal()),
                          fontSize: 18,
                        ),
                      ],
                    )
                  ],
                ),
              ),
            ),
            Expanded(
              child: Icon(
                Icons.keyboard_arrow_right,
                size: 64,
                color: Colors.grey.shade400,
              ),
            )
          ],
        ),
        Positioned(
          top: 16,
          right: 0,
          child: _CheckRecordSignStatusTag(
            dto: dto,
          ),
        ),
        Positioned(
          bottom: 16,
          right: 12,
          child: IconButton(
            icon: Icon(
              Icons.edit,
              size: 26,
              color: Theme.of(context).primaryColor,
            ),
            onPressed: () {
              controller.toCheckPage(dto, dataDto, index); //跳转到体检
            },
          ),
        ),
      ],
    );
    return Material(
      borderRadius: GlobalStyles.borderRadius,
      child: Ink(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: GlobalStyles.borderRadius,
        ),
        child: InkWell(
          borderRadius: GlobalStyles.borderRadius,
          onTap: () {
            /// TODO 编辑
            controller.toExamDetailPage(index, dto); //跳转到检查页面
          },
          child: body,
        ),
      ),
    );
  }
}

// ignore: camel_case_types
class _CheckRecordSignStatusTag extends StatelessWidget {
  final ExamRecordDTO dto;

  _CheckRecordSignStatusTag({required this.dto});
  final healthCheckRecordController = Get.find<HealthCheckRecordController>();
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.centerRight,
      width: 120,
      child: StatusLabel(
        title: healthCheckRecordController.examStateTransition(dto.examState),
        color: healthCheckRecordController.examStateColors(dto.examState),
      ),
    );
  }
}
