import ElectricScrewdriver from '../../viewmodel/device/ElectricScrewdriver'

@Component
export struct AddSubtractComp {
  // 输入值
  @State inputNum: number = 0.00
  // 输入值所在的对象用于修改(不存在则不传)
  @Link obj: ElectricScrewdriver
  // 需要修改的对象的属性名
  editPropertyName: string=''
  // 输入值上限、下限
  upperLimit: number | undefined = undefined
  lowerLimit: number | undefined = undefined
  // 点击加、减事件调用的方法，及输入修改调用的方法
  addFunction: Function | undefined = undefined
  subFunction: Function | undefined = undefined
  editFunction: Function | undefined = undefined


  build() {
    Row() {
      Row() {
        Image($r('app.media.general_subtract'))
          .fillColor($r('app.color.FFFFFF'))
          .width($r('app.float.virtualSize_48'))
          .height($r('app.float.virtualSize_48'))
      }
      .width('25%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
      .onClick(()=>{
        if (this.lowerLimit != undefined && this.inputNum <= this.lowerLimit) {
          return
        }
        this.inputNum --
        if (this.obj) {
          // 设置属性
          Reflect.set(this.obj, this.editPropertyName, this.inputNum);
        }
        if (this.subFunction) {
          this.subFunction()
        }
      })
      Row() {
        TextInput({text: this.inputNum.toString()})
          .fontSize($r('app.float.fontSize_38'))
          .fontWeight(FontWeight.Lighter)
          .fontColor($r('app.color.FFFFFF'))
          .height('100%')
          .width('100%')
          .type(InputType.Normal)
          .textAlign(TextAlign.Center)
          .onChange((value: string)=>{
            if (!value || value.length === 0) {
              this.inputNum = 0.00
              return
            }

            let exchangeNumber: number = Number.parseFloat(value)
            // 校验数据有效性
            if (this.lowerLimit != undefined && exchangeNumber < this.lowerLimit) {
              this.inputNum = this.lowerLimit
            } else if (this.upperLimit != undefined && exchangeNumber > this.upperLimit) {
              this.inputNum = this.upperLimit
            } else {
              this.inputNum = exchangeNumber
            }
            if (this.obj) {
              // 设置属性
              Reflect.set(this.obj, this.editPropertyName, this.inputNum);
            }
            if (this.editFunction) {
              this.editFunction()
            }
          })
      }
      .width('50%')
      .height('100%')
      .borderColor($r('app.color.10FFFFFF'))

      Row() {
        Image($r('app.media.general_add'))
          .fillColor($r('app.color.FFFFFF'))
          .width($r('app.float.virtualSize_48'))
          .height($r('app.float.virtualSize_48'))
      }
      .width('25%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
      .onClick(()=>{
        if (this.upperLimit != undefined && this.inputNum >= this.upperLimit) {
          return
        }
        this.inputNum ++
        if (this.obj) {
          // 设置属性
          Reflect.set(this.obj, this.editPropertyName, this.inputNum);
        }
        if (this.addFunction) {
          this.addFunction()
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.20FFFFFF'))
    .borderRadius($r('app.float.virtualSize_16'))
  }
}