// 切换用户
import CommonConstants from '../common/constants/CommonConstants'
import ProcessRequest from '../common/util/request/ProcessRequest'
import RequestParamModel from '../viewmodel/RequestParamModel'
import { UserInfo } from '../viewmodel/UserInfo'

@CustomDialog
export struct SwitchUserDialog {
  //当前用户
  @Consume('currentUserName') currentUserName: string
  @State userName: string = ''
  @State password : string = ''
  // 当前组织id
  @Consume('currentOrgId') currentOrgId: number
  //当前产线code
  @Consume('currentPLCode') currentPLCode: string
  // 当前工位id
  @Consume('currentStationId') currentStationId: string
  @Consume('currentUserId') currentUserId: number

  controller: CustomDialogController

  login=async ()=>{
    let token:string = await ProcessRequest.post('api/auth/aioLogin', {
      orgId:this.currentOrgId,
      password: this.password,
      userName: this.userName,
      proCode: this.currentPLCode,
      stationId: this.currentStationId
      } as RequestParamModel) ;
    if (!token || token.length == 0)  return
    CommonConstants.AUTH_TOKEN = token
    this.currentUserName = this.userName
    let res:UserInfo = await ProcessRequest.get('/api/auth') ;
    this.currentUserId = res.id! as number
    this.controller.close()
}

  build() {
    Column() {
      Column(){
        Text("切换用户")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }.height('25%')
      .justifyContent(FlexAlign.Center)

      Column() {
        Text("账户")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_16'))
          .width('70%')
          .textAlign(TextAlign.Start)
          .margin({left:'2%',bottom:'2%'})
        Row() {
          Image($r('app.media.process_user_name'))
            .width($r('app.float.virtualSize_40'))
            .height($r('app.float.virtualSize_40'))
            .fillColor($r('app.color.FFFFFF'))
            .margin({left:'2%'})

          TextInput({text:this.currentUserName,placeholder:"请录入账户"})
            .width('85%')
            .height('100%')
            .backgroundColor('#000000')
            .fontSize($r('app.float.fontSize_16'))
            .placeholderFont({size:$r('app.float.fontSize_16')})
            .placeholderColor($r('app.color.30FFFFFF'))
            .fontColor($r('app.color.FFFFFF'))
            .onChange((value: string) => {
              this.userName = value;
            })
        }
        .backgroundColor($r('app.color.000000'))
        .width('70%')
        .height('30%')
        .borderRadius($r('app.float.virtualSize_16'))
        Text("密码")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_16'))
          .width('70%')
          .textAlign(TextAlign.Start)
          .margin({left:'2%',bottom:'2%',top:"5%"})
        Row() {
          Image($r('app.media.process_password'))
            .width($r('app.float.virtualSize_40'))
            .height($r('app.float.virtualSize_40'))
            .fillColor($r('app.color.FFFFFF'))
            .margin({left:'2%'})
          TextInput({text:this.password, placeholder:"请录入密码"} )
            .width('85%')
            .height('100%')
            .placeholderFont({size:$r('app.float.fontSize_16')})
            .placeholderColor($r('app.color.30FFFFFF'))
            .backgroundColor('#000000')
            .fontSize($r('app.float.fontSize_16'))
            .fontColor($r('app.color.FFFFFF'))
            .type(InputType.Password)
            .onChange((value: string) => {
              this.password = value;
            })
        }
        .width('70%')
        .height('30%')
        .backgroundColor($r('app.color.000000'))
        .borderRadius($r('app.float.virtualSize_16'))
      }
      .justifyContent(FlexAlign.Start)
      .height('40%')
      .width('100%')
      .margin({bottom:'16%'})
      // 按钮区域
      Column(){
        Divider()
          .vertical(false)
          .strokeWidth(1)
          .color($r('app.color.15FFFFFF'))
        Row(){
          Row(){
            Text('取消')
              .fontColor($r('app.color.60FFFFFF'))
              .fontSize($r('app.float.fontSize_30'))
          }
          .justifyContent(FlexAlign.Center)
          .width('50%')
          .onClick(() => {
            this.controller.close()
          })
          Divider()
            .vertical(true)
            .strokeWidth(1)
            .color($r('app.color.15FFFFFF'))
          Row(){
            Text('确认')
              .fontColor($r('app.color.007AFF'))
              .fontSize($r('app.float.fontSize_30'))
          }
          .justifyContent(FlexAlign.Center)
          .width('50%')
          .onClick(() => {
            this.login()
          })
        }
      }
      .width('100%')
      .height('13%')

    }
    .height('44%')
    .width('31%')
    .backgroundColor($r('app.color.2A2A2A'))
    .justifyContent(FlexAlign.End)
    .borderColor($r('app.color.000000'))
    .borderRadius($r('app.float.virtualSize_16'))
  }
}