import 'package:fis_jsonrpc/services/contractRecord.m.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';
import 'package:vitalapp/components/appbar.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/components/dialog_input.dart';
import 'package:vitalapp/pages/contract/contract_record_html/controller.dart';

class ContractRecordsHtmlPage extends GetView<ContractRecordsHtmlController> {
  const ContractRecordsHtmlPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: VAppBar(
        titleWidget: const Text(
          "合同",
          style: TextStyle(fontSize: 24),
        ),
      ),
      body: Stack(
        children: [
          Obx(
            () => InAppWebView(
              key: UniqueKey(),
              initialData: InAppWebViewInitialData(
                data:
                    '<body style="padding: 100px;">${controller.state.contractRecordDTO.contractedFileUrl ?? ''}</body>',
                mimeType: 'text/html',
                encoding: 'utf-8',
              ),
              initialOptions: InAppWebViewGroupOptions(
                crossPlatform: InAppWebViewOptions(
                  useShouldOverrideUrlLoading: true,
                  mediaPlaybackRequiresUserGesture: false,
                  disableVerticalScroll: false,
                ),
                android: AndroidInAppWebViewOptions(
                  //设置为 true 以启用混合组合
                  useHybridComposition: true,
                ),
              ),
              onWebViewCreated: (InAppWebViewController controller) {
                // _webViewController = controller;
              },
              onLoadHttpError: (controller, url, statusCode, description) {
                // ignore: avoid_print
                print("InAppWebView onLoadHttpError: $description");
              },
            ),
          ),
          Obx(
            () {
              return controller.state.contractRecordDTO.contractState !=
                      ContractStateEnum.Cancelled
                  ? Positioned(
                      bottom: 8,
                      left: 200,
                      right: 200,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          VButton(
                            label: "解 约",
                            onTap: () async {
                              var result = await const VDialogInput(
                                title: '解约原因',
                              ).show();
                              if (result != null) {
                                var uplateResult = await controller
                                    .updateContractRecordStateAsync(result);
                                if (uplateResult) {
                                  PromptBox.toast('解约成功');
                                }
                              }
                            },
                          ),
                        ],
                      ),
                    )
                  : Positioned(
                      bottom: 8,
                      left: 200,
                      right: 200,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          VButton(
                            label: "续 约",
                            onTap: () async {
                              var uplateResult =
                                  await controller.renewalContractRecordAsync();
                              if (uplateResult.isNotEmpty) {
                                PromptBox.toast('续约成功');
                              }
                            },
                          ),
                        ],
                      ),
                    );
            },
          ),
        ],
      ),
    );
  }
}
