import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/report.m.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/pages/user/report_template/controller.dart';
import 'package:fis_theme/theme.dart';

///添加报告模板
class AddTemplatePage extends FisView<ReportTemplateController>
    implements FPage {
  @override
  final String pageName = "AddTemplatePage";

  @override
  FWidget build(BuildContext context) {
    final templates = controller.state.publishTemplates;
    List<FWidget> children = [];
    int index = 0;
    templates.forEach((element) {
      children.add(_buildTemplateCard(index));
      index++;
    });
    children.add(const FSizedBox(height: 20));
    return FSimpleDialog(
      bottomButton: _buildBottomButton(),
      title: FText(
        i18nBook.user.createTemplate.t,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onClose: () {
        router.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      backgroundColor: Color.fromRGBO(230, 230, 230, 1),
      children: [
        FContainer(
          margin: const EdgeInsets.only(top: 20, left: 20),
          child: FText(
            i18nBook.user.useFlyinsonoTemplate.t,
            style: const TextStyle(
              fontSize: 18,
            ),
          ),
        ),
        FContainer(
          margin: EdgeInsets.symmetric(vertical: 30),
          width: 680,
          child: FWrap(
            children: children,
          ),
        ),
      ],
    );
  }

  ///底部按钮
  FWidget _buildBottomButton() {
    return FSizedBox();
  }

  ///模板卡片
  FWidget _buildTemplateCard(int index) {
    final templates = controller.state.publishTemplates;
    final template = templates[index];
    String reportTemplateUsage =
        template.reportTemplateUsageType == ReportTemplateUsageTypeEnum.People
            ? i18nBook.remedical.humanAlong.t
            : i18nBook.user.animalTemplate.t;

    return FContainer(
      height: 84,
      width: 310,
      margin: EdgeInsets.symmetric(vertical: 20, horizontal: 20),
      child: FStack(
        children: [
          FOutlinedButton(
            name: "openTemplateDesigner",
            businessParent: this,
            onPressed: () async {
              controller.openTemplateDesigner(
                (template.reportTemplateName ?? ''),
                template.reportTemplateUsageType,
                template.reportTemplateCode ?? '',
              );
            },
            child: FRow(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const FSizedBox(width: 15),
                FIcon(FIcons.report_template_org, size: 42),
                FExpanded(
                  child: FColumn(
                    children: [
                      const FSizedBox(height: 25),
                      FText(
                        template.reportTemplateName ?? '',
                        style: TextStyle(
                          fontSize: 20,
                          color: Color.fromARGB(255, 36, 36, 36),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          FPositioned(
            top: 5,
            right: 0,
            child: FContainer(
                padding: EdgeInsets.symmetric(horizontal: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(11.0),
                    bottomLeft: Radius.circular(11.0),
                  ),
                  color: FTheme.ins.colorScheme.primary,
                ),
                child: FText(
                  _getTagText(template.reportTemplateType),
                  style: TextStyle(
                    color: Colors.white,
                  ),
                )),
          )
        ],
      ),
    );
  }

  String _getTagText(ReportTemplateTypeEnum reportTemplateType) {
    switch (reportTemplateType) {
      case ReportTemplateTypeEnum.Default:
        return i18nBook.remedical.defualt.t;
      case ReportTemplateTypeEnum.Organization:
        return i18nBook.remedical.organization.t;
      case ReportTemplateTypeEnum.Personal:
        return i18nBook.remedical.selfCreated.t;
    }
  }
}
