import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/components/appbar.dart';

import 'controller.dart';

class ContractTemplatePage extends GetView<ContractTemplateController> {
  ContractTemplatePage({super.key});
  late InAppWebViewController? _webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: const Color.fromRGBO(238, 238, 238, 1),
      appBar: VAppBar(
        titleWidget: const Text(
          "合同",
          style: TextStyle(fontSize: 24),
        ),
        actions: [
          TextButton(
            onPressed: () async {
              await controller.createContractRecord();
              Get.back();
            },
            child: const Text(
              '提交',
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          ),
          const SizedBox(
            width: 15,
          ),
        ],
      ),
      body: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return Stack(
            children: [
              SizedBox(
                child: Transform.scale(
                  scale: 1,
                  child: Container(
                    width: constraints.maxWidth,
                    height: constraints.maxHeight,
                    padding: const EdgeInsets.symmetric(horizontal: 50),
                    child: Obx(() {
                      String templateContent = controller.state.templateContent;
                      if (templateContent.isEmpty) {
                        return const SizedBox();
                      } else {
                        return InAppWebView(
                          initialData: InAppWebViewInitialData(
                            data: controller.state.templateContent,
                            mimeType: 'text/html',
                            encoding: 'utf-8',
                          ),
                          initialOptions: InAppWebViewGroupOptions(
                            crossPlatform: InAppWebViewOptions(
                              useShouldOverrideUrlLoading: true,
                              mediaPlaybackRequiresUserGesture: false,
                            ),
                          ),
                          onWebViewCreated:
                              (InAppWebViewController controller) {
                            _webViewController = controller;
                          },
                        );
                      }
                    }),
                  ),
                ),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: Container(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  color: Colors.white,
                  alignment: Alignment.bottomCenter,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 100,
                      vertical: 5,
                    ),
                    child: TextButton(
                      onPressed: () async {
                        final result = await Get.toNamed("/contract/signature");
                        controller.state.mock2Base64 = result;
                        final dto = await controller.contractTemplateManager
                            .getContractTemplateDetail(
                                controller.state.templateCode);
                        if (dto != null) {
                          controller.state.templateContent =
                              dto.templateContent ?? '';
                          controller.getkey();
                        }
                        _webViewController?.loadData(
                            data: controller.state.templateContent,
                            mimeType: 'text/html',
                            encoding: 'utf-8');
                      },
                      child: const Text(
                        '签字',
                        style: TextStyle(fontSize: 30),
                      ),
                    ),
                  ),
                ),
              )
            ],
          );
        },
      ),
    );
  }
}
