import { WebParams } from '@ohos/common/src/main/ets/viewmodel/ParamBean';
import { webview } from '@kit.ArkWeb';
import { TitleBarComponent } from '../view/TitleBarComponent';

@Builder
export function WebLayoutBuilder() {
  WebLayout()
}

/**
 * 内置Web页面
 */
@Component
export struct WebLayout {
  @StorageProp('statusBarHeight') statusBarHeight: number = 0;
  @Consume('pageInfos') pageStack: NavPathStack;
  @State urlParams: WebParams | null = null;

  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    NavDestination() {
      Stack() {
        Column() {
          //标题栏
          TitleBarComponent({titleName:this.urlParams?.name, isShowTitle:true, isShowBack:true})

          Column(){
            Web({
              src: this.urlParams?.url,
              controller: this.controller // 绑定控制器
            })
              .onPageEnd(() => {
                // 页面加载完成后执行 JS
                this.controller.runJavaScript('document.title = "HarmonyOS"');
              })
          }
        }
        .width('100%')
        .height('100%')
      }
      .width('100%')
      .height('100%')
      .backgroundColor('#FFFFFFFF')
    }
    .onReady((context: NavDestinationContext) => {
      this.urlParams = context.pathInfo.param as WebParams;
    })
    .hideTitleBar(true)
  }
}

