import { hdHttp, HdResponse, BasicConstant, ExpertData, authStore } from '@itcast/basic'
import { BusinessError } from '@kit.BasicServicesKit';
import promptAction from '@ohos.promptAction';
import HashMap from '@ohos.util.HashMap';
import { router } from '@kit.ArkUI';

interface updateExtraData {
  expertUuid: string,
  password: string,
  oriPwd: string
}

interface callBackData {
  code: number,
  message:string,
  msg:string,
  data: string
}

@Component
export struct ChangePasswordComp {
  @State oldPassword: string = ''
  @State newPassword: string = ''
  @State confirmPassword: string = ''

  uploadChangePasswordAction(){
    hdHttp.post<string>(BasicConstant.urlExpert+'modifyPwd', {
      expertUuid: authStore.getUser().uuid,
      password: this.confirmPassword,
      oriPwd: this.oldPassword
    } as updateExtraData).then(async (res: HdResponse<string>) => {
      let json:callBackData = JSON.parse(res+'') as callBackData;
      console.log('更新登录密码数据:',json);
      if (json.code == 1) {
        promptAction.showToast({message:'修改成功'});
        router.back();
      } else {
        promptAction.showToast({message:json.message});
      }
    }).catch((err: BusinessError) => {
      console.info(`Response fail: ${err}`);
    })
  }

  build() {
    Column() {
      // 原密码输入框
      TextInput({ placeholder: '请输入原密码' })
        .width('95%')
        .height(40)
        .margin({ top: 10 })
        .borderWidth(1)
        .borderRadius(8)
        .borderColor('#cccccc')
        .type(InputType.Password)
        .onChange((value: string) => {
          this.oldPassword = value
        })

      // 新密码输入框
      TextInput({ placeholder: '新密码（6~16位数字字母组合）' })
        .width('95%')
        .height(40)
        .margin({ top: 10 })
        .borderWidth(1)
        .borderRadius(8)
        .borderColor('#cccccc')
        .type(InputType.Password)
        .onChange((value: string) => {
          this.newPassword = value
        })

      // 确认新密码输入框
      TextInput({ placeholder: '确认新密码' })
        .width('95%')
        .height(40)
        .margin({ top: 10 })
        .borderWidth(1)
        .borderRadius(8)
        .borderColor('#cccccc')
        .type(InputType.Password)
        .onChange((value: string) => {
          this.confirmPassword = value
        })

      // 忘记密码链接
      Text('忘记密码？')
        .fontSize(14)
        .fontColor('#666666')
        .margin({ top: 12 })
        .onClick(() => {
          // 处理忘记密码逻辑
          router.pushUrl({url:'pages/MinePage/ForgetPassword'})
        })

      // 确定按钮
      Button({ type: ButtonType.Normal }){
        Text('确 定')
      }
      .width('90%')
      .height(40)
      .position({x:'5%',y:'80%'})
      .backgroundColor('#ffffff')
      .borderColor($r('app.color.main_color'))
      .borderRadius(8)
      .borderWidth(1)
      .fontColor($r('app.color.main_color'))
      .onClick(() => {
        const passwordRegex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
        if (this.oldPassword.length <= 0 || !this.oldPassword) {
          promptAction.showToast({message:'请输入原密码'})
          return
        }
        if (!passwordRegex.test(this.oldPassword)) {
          promptAction.showToast({message:'原密码格式错误'})
          return
        }
        if (this.newPassword.length <= 0 || !this.newPassword) {
          promptAction.showToast({message:'请输入原密码'})
          return
        }
        if (!passwordRegex.test(this.newPassword)) {
          promptAction.showToast({message:'新密码格式错误'})
          return
        }
        if (this.confirmPassword.length <= 0 || !this.confirmPassword) {
          promptAction.showToast({message:'请确认新密码'})
          return
        }
        if (this.newPassword !== this.confirmPassword) {
          promptAction.showToast({message:'新密码与确认密码不一致'})
          return
        }
        if (!passwordRegex.test(this.confirmPassword)) {
          promptAction.showToast({message:'密码格式不正确，请重新输入！'})
          return
        }
        this.uploadChangePasswordAction()
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.White)
  }

}
