import 'dart:async';
import 'package:fis_common/env/env.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/webview/webview.dart';
import 'package:flutter/widgets.dart';
import 'package:flyinsono/architecture/app_parameters.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/managers/server_config_manager.dart';
import 'package:flyinsono/pages/user/report_template/controller.dart';
import 'package:webviewx/webviewx.dart';

class ReportDesignerPage extends FisView<ReportTemplateController> {
  final _webViewController = Completer<WebViewXController>();
  @override
  FWidget build(BuildContext context) {
    return _buildWebView(context);
  }

  FWidget _buildWebView(BuildContext context) {
    return FObx(() {
      var templateId = controller.state.templateId;
      if (templateId.isEmpty) {
        return FSizedBox();
      }
      var name = controller.state.templateName;
      var type = controller.state.templateType;
      var root = AppParameters.data.reportDesignerRoot;
      var openMode = controller.state.openMode;

      String url;
      if (FPlatform.isMacOS || FPlatform.isWindows) {
        var lang = i18nBook.locale.toCodeString('-');
        url =
            '$root?id=$templateId&name=$name&type=$type&category=1&lang=$lang';
      } else {
        url =
            '$root/reportTemplateDesigner?id=$templateId&name=$name&type=$type&category=0';
      }
      url += "&isASROn=${ServerConfigManager.isASROn}";
      if (controller.state.isOrg) {
        url +=
            '&orgCode=${Store.user.rootOrganizationCode}&openMode=$openMode&templateType=1';
      } else {
        url += '&userCode=${Store.user.code}&openMode=$openMode&templateType=2';
      }

      print(url);
      return FContainer(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: FWebViewX(
          key: key,
          initialContent: url,
          initialSourceType: SourceType.url,
          javascriptMode: JavascriptMode.unrestricted, // 使用JS没限制
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
        ),
      );
    });
  }
}
