import {ProcessCurrentView} from '../viewmodel/process/EnumerationType'
// 物料采集工序
@Component
export struct SwitchAndScanProductView {
  @Link processCurrentView:ProcessCurrentView
  @Link previousView:ProcessCurrentView

  @State scanCode: string = ''
  private isSubmitting: boolean = false

  controller: TextInputController = new TextInputController()
  aboutToAppear() {
    setTimeout(() => {
      focusControl.requestFocus('CATCH_FOCUS')
    }, 500)
  }

  private delay(ms: number): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }
  private async submitScanCode(code: string) {
    // 1. 基础校验：非空+防抖
    if (!code || this.isSubmitting) return
    this.isSubmitting = true
    await this.delay(200)
    try {
      if(this.scanCode = '79714')
      {
        this.previousView = this.processCurrentView;
        this.processCurrentView = ProcessCurrentView.WORK_ORDER_VIEW
      }
      // 2. 执行提交逻辑（示例：打印编码，实际替换为接口调用/业务处理）
      console.log('扫码内容自动提交：', code)
      this.scanCode = ''
      // 4. 重新聚焦输入框（保持扫码状态）
      focusControl.requestFocus('CATCH_FOCUS')
    } catch (error) {
      console.error('提交失败：', error)
      // 提交失败可提示用户（如Toast）
      // prompt.showToast({ message: '提交失败，请重试' })
    } finally {
      // 重置防抖标记
      this.isSubmitting = false
    }
  }

  build() {
    Column() {
      Row() {
        Text('扫描产品编码')
          .fontSize($r('app.float.fontSize_30'))
          .fontWeight(FontWeight.Medium)
          .fontColor($r('app.color.FFFFFF'))
      }
      .height('10%')
      .width('97.2%')
      .justifyContent(FlexAlign.Start)

      Row() {
        Column({ space: 5 }) {
          Row().height('37%')
          Row() {
            Text('请扫描产品编码')
              .fontSize($r('app.float.fontSize_16'))
              .fontWeight(FontWeight.Regular)
              .fontColor($r('app.color.FFFFFF'))
          }
          .width('62%')

          Row() {
            Row().width('3.4%')
            // 左侧二维码图标
            Image($r('app.media.material_qr_code'))// 请替换为您的二维码图片资源
              .width($r('app.float.virtualSize_24'))
              .height($r('app.float.virtualSize_24'))
              .fillColor($r('app.color.FFFFFF'))
            // 扫码输入框
            TextInput({ placeholder: '请扫描产品编码',
              controller: this.controller, text: this.scanCode })
              .type(InputType.Normal)
              .placeholderFont({ size: $r('app.float.fontSize_16') })
              .placeholderColor($r('app.color.30FFFFFF'))
              .fontSize($r('app.float.fontSize_16'))
              .fontColor($r('app.color.FFFFFF'))
              .enableKeyboardOnFocus(false)
              .key('CATCH_FOCUS')
              .onChange((value: string) => {
                this.scanCode = value
              })
              .onSubmit(() => {
                // 回车键提交
                this.submitScanCode(this.scanCode )
              })
          }
          .width('65%')
          .height('6.5%')
          .borderRadius($r('app.float.virtualSize_16'))
          .backgroundColor($r('app.color.000000'))
          .justifyContent(FlexAlign.Start)
        }
        .width('52.9%')
        .height('100%')
      }
      .width('100%')
      .height('87.7%')
      .justifyContent(FlexAlign.Center)

    }
    .width('100%')
    .height('100%')
  }

}