/*
 * 验证码输入框
 * EZOpenSDK
 *
 * Created by junecheng on 2024/3/6.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import { common } from '@kit.AbilityKit'
import { PreferenceUtil } from '../../utils/PreferenceUtil'

@CustomDialog
export default struct VerifyCodeDialog {
  deviceSerial: string = ''
  title: ResourceStr = ''
  message: ResourceStr = ''
  placeholder?: ResourceStr = ''
  @Link inputContent: string
  confirmTitle?: ResourceStr = $r('app.string.dialog_ok')
  cancelTitle?: ResourceStr = $r('app.string.dialog_cancel')
  controller: CustomDialogController
  cancelClick?: () => void
  confirmClick?: () => void

  build() {
    Column() {
      Text(this.title).fontSize(18).fontColor(Color.Black).margin({ top: 20 })
      Text(this.message)
        .fontSize(14)
        .textAlign(TextAlign.Center)
        .margin({
          top: 10,
          bottom: 20,
          left: 20,
          right: 20
        })
      // 验证码输入框
      TextInput({ placeholder: this.placeholder, text: this.inputContent })
        .width('90%')
        .height(40)
        .placeholderFont({ size: 14 })
        .fontSize(14)
        .type(InputType.Normal)
        .onChange((value: string) => {
          this.inputContent = value
        })
      Divider().vertical(false).margin({ top: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        // 取消按钮
        Button(this.cancelTitle)
          .width('50%')
          .height('100%')
          .backgroundColor(Color.White)
          .fontColor(Color.Blue)
          .onClick(() => {
            this.controller.close()
            if (this.cancelClick) {
              this.cancelClick()
            }
          })
        Divider().vertical(true)
        // 确定按钮
        Button(this.confirmTitle)
          .width('50%')
          .height('100%')
          .backgroundColor(Color.White)
          .fontColor(Color.Blue)
          .enabled(this.inputContent.length >= 6)
          .onClick(() => {
            this.controller.close()
            if (this.confirmClick) {
              if (this.deviceSerial) {
                let context: Context = getContext(this) as common.UIAbilityContext
                PreferenceUtil.saveString(context, this.deviceSerial, this.inputContent)
              }
              this.confirmClick()
            }
          })
      }.height(44)
    }.width('75%').height(200).backgroundColor(Color.White).borderRadius(10)
  }
}