import 'package:fis_jsonrpc/services/contractRecord.m.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vnoteapp/components/appbar.dart';
import 'package:vnoteapp/consts/styles.dart';
import 'package:vnoteapp/managers/contract/index.dart';
import 'package:vnoteapp/pages/patient/list/widgets/status.dart';
import 'package:vnoteapp/pages/widgets/record_common_item.dart';

import 'controller.dart';

class ContractRecordsPage extends GetView<ContractRecordsController> {
  const ContractRecordsPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: ContractRecordsController(),
        id: "ContractRecords",
        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 _buildListView() {
    return Expanded(
      flex: 2,
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: RefreshIndicator(
            child: Obx(() {
              final list = controller.state.contractRecordDTOList;
              final children = <Widget>[];
              for (var contractRecordDTO in list) {
                children.add(_ContractRecordCard(dto: contractRecordDTO));
              }
              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(
                      shrinkWrap: true,
                      gridDelegate:
                          const SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 1,
                        mainAxisSpacing: 16,
                        crossAxisSpacing: 20,
                        childAspectRatio: 900 / 180,
                      ),
                      children: children,
                    );
            }),
            onRefresh: () async {}),
      ),
    );
  }

  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,
          ),
        ),
      ),
    );
  }
}

// ignore: unused_element
class _ContractRecordCard extends StatelessWidget {
  final ContractRecordDTO dto;

  _ContractRecordCard({required this.dto});
  final controller = Get.find<ContractRecordsController>();
  @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),
                    Wrap(
                      alignment: WrapAlignment.start,
                      spacing: 20,
                      runSpacing: 8,
                      children: [
                        RecordCommonItem(
                          itemName: '服务包',
                          itemValue: controller.getServicePackNames(dto),
                          fontSize: 20,
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Wrap(
                      alignment: WrapAlignment.start,
                      spacing: 20,
                      runSpacing: 8,
                      children: [
                        SizedBox(
                          width: 250,
                          child: RecordCommonItem(
                            itemName: '姓名',
                            itemValue: dto.patientName!,
                            fontSize: 18,
                          ),
                        ),
                        const SizedBox(
                          width: 20,
                        ),
                        RecordCommonItem(
                          itemName: '身份证号',
                          itemValue: dto.cardNo!,
                          fontSize: 18,
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Wrap(
                      alignment: WrapAlignment.start,
                      spacing: 20,
                      runSpacing: 8,
                      children: [
                        SizedBox(
                          width: 250,
                          child: RecordCommonItem(
                              itemName: '签约团队',
                              itemValue: dto.contractedTeam!,
                              fontSize: 18),
                        ),
                        const SizedBox(
                          width: 20,
                        ),
                        RecordCommonItem(
                            itemName: '签约时间',
                            itemValue: DateFormat("yyyy-MM-dd")
                                .format(dto.contractedTime!.toLocal()),
                            fontSize: 18),
                      ],
                    ),
                    // const SizedBox(
                    //   height: 20,
                    // ),
                    // Wrap(
                    //   alignment: WrapAlignment.start,
                    //   spacing: 20,
                    //   runSpacing: 8,
                    //   children: [
                    //     SizedBox(
                    //       width: 250,
                    //       child: _buildItem('签约医生', dto.contractedDoctor!, 18),
                    //     ),
                    //     const SizedBox(
                    //       width: 20,
                    //     ),
                    //   ],
                    // ),
                  ],
                ),
              ),
            ),
            Expanded(
                flex: 1,
                child: Icon(
                  Icons.keyboard_arrow_right,
                  size: 64,
                  color: Colors.grey.shade400,
                ))
          ],
        ),
        Positioned(
          top: 16,
          right: 0,
          child: _contractSignStatusTag(
            dto: dto,
          ),
        ),
      ],
    );
    return Material(
      borderRadius: GlobalStyles.borderRadius,
      child: Ink(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: GlobalStyles.borderRadius,
          ),
          child: InkWell(
            borderRadius: GlobalStyles.borderRadius,
            onTap: () {
              Get.toNamed(
                "/contract/contract_record_html",
                parameters: {"patientCode": dto.code!},
              );
            },
            child: body,
          )),
    );
  }
}

// ignore: camel_case_types
class _contractSignStatusTag extends StatelessWidget {
  final ContractRecordDTO dto;

  _contractSignStatusTag({required this.dto});
  final ContractUtils _contractUtils = ContractUtils();
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.centerRight,
      width: 120,
      child: StatusLabel(
        title: _contractUtils.contractStateTransition(dto.contractState),
        color: _contractUtils.contractStateColors(dto.contractState),
      ),
    );
  }
}
