import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/pages/contract/package_list/controller.dart';

/// 包详情
class PackageInfoPage extends GetView<ServicePackageContractController> {
  final ServicePackDTO? dto;
  const PackageInfoPage({super.key, this.dto});
  Widget buildAlertDialog(ServicePackDTO? servicePackDTO) {
    return Container(
      margin: const EdgeInsets.only(left: 55),
      alignment: Alignment.topLeft,
      padding: const EdgeInsets.symmetric(horizontal: 15),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '服务名称',
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
          ),
          const SizedBox(
            height: 10,
          ),
          Container(
            color: const Color.fromARGB(255, 243, 240, 240),
            child: _buildItemContent(servicePackDTO?.name ?? ""),
          ),
          const SizedBox(
            height: 10,
          ),
          const Text(
            '服务介绍',
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
          ),
          const SizedBox(
            height: 10,
          ),
          Container(
            color: const Color.fromARGB(255, 243, 240, 240),
            child: _buildItemContent(servicePackDTO?.content ?? ""),
          ),
          const SizedBox(
            height: 10,
          ),
          const Text(
            '服务项目',
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
          ),
          const SizedBox(
            height: 10,
          ),
          Expanded(
            child: Container(
              color: const Color.fromARGB(255, 243, 240, 240),
              child: Scrollbar(
                thumbVisibility: true,
                child: ListView(
                  children: [
                    ...controller
                        .getServiceItemsName(
                          servicePackDTO?.items ?? [],
                        )
                        .map(
                          (ServiceItemDTO e) => _buildServiceItem(e),
                        ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildServiceItem(ServiceItemDTO dto) {
    return Container(
      margin: const EdgeInsets.only(left: 20),
      padding: const EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            dto.name ?? '',
            style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          const SizedBox(
            height: 5,
          ),
          Container(
            margin: const EdgeInsets.only(left: 20),
            child: Text(
              dto.content ?? '',
              style: const TextStyle(fontSize: 18),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildItemContent(String itemName) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return ConstrainedBox(
          constraints: const BoxConstraints(
            maxHeight: 100,
          ),
          child: Scrollbar(
            thumbVisibility: true,
            child: ListView(
              shrinkWrap: true,
              children: [
                Container(
                  margin: const EdgeInsets.only(left: 20),
                  padding: const EdgeInsets.all(5),
                  child: Text(
                    itemName,
                    style: const TextStyle(fontSize: 18),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "服务包详情",
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
        ),
        // leading: Container(),
        // leadingWidth: 0,
        // actions: [
        //   IconButton(
        //       onPressed: () {
        //         Navigator.pop(context);
        //       },
        //       icon: const Icon(
        //         Icons.cancel,
        //         size: 24,
        //       ))
        // ],
      ),
      body: buildAlertDialog(dto),
    );
  }
}
