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

import 'controller.dart';

// ignore: must_be_immutable
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: Stack(
        children: [
          Obx(() {
            return InAppWebView(
              key: UniqueKey(),
              initialData: InAppWebViewInitialData(
                data:
                    '<body style="padding: 100px;">${controller.state.templateContent}</body>',
                mimeType: 'text/html',
                encoding: 'utf-8',
              ),
              initialOptions: InAppWebViewGroupOptions(
                crossPlatform: InAppWebViewOptions(
                  useShouldOverrideUrlLoading: true,
                  mediaPlaybackRequiresUserGesture: false,
                ),
              ),
              onWebViewCreated: (InAppWebViewController controller) {
                _webViewController = controller;
              },
            );
            // }
          }),
          Obx(
            () => Positioned(
              bottom: 8,
              left: 200,
              right: 200,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  VButton(
                    label: "签字",
                    onTap: () {
                      gotoSignature();
                    },
                  ),
                  if (controller.state.userSignature != '')
                    VButton(
                      label: "提交",
                      onTap: () async {
                        await controller.createContractRecord();
                        Get.back();
                      },
                    ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Future<void> gotoSignature() async {
    final result = await Get.toNamed("/contract/signature");
    if (kDebugMode) {
      // TODO: 调试记录签名
      // await TextStorage(
      //   fileName: "signature_${DateTime.now().millisecondsSinceEpoch}.txt",
      //   directory: "debug",
      // ).save(result);
    }
    controller.state.userSignature = 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',
    );
  }
}
