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

interface updateExtraData {
  uuid: string,
  userName: string,
  type: string,
  intro: string,
}

interface callBackData {
  expert:ExpertData,
  code:number,
  message:string,
  specialy:[],
  data:ExpertData,
  special:[]
}

@Entry
@Component
struct EditIntroductionPage {
  controller : TextAreaController = new TextAreaController()
  @State inputValue: string = authStore.getUser().intro;

  commitIntroductionData(inputStr:string){
    const updateDataUrl:string = BasicConstant.urlExpert + 'modify';
    hdHttp.post<string>(updateDataUrl, {
      uuid: authStore.getUser().uuid,
      userName: authStore.getUser().userName,
      intro: inputStr,
      type:'2'
    } as updateExtraData).then(async (res: HdResponse<string>) => {
      let json:callBackData = JSON.parse(res+'') as callBackData;
      if(json.code == 1 && json.data && typeof json.data === 'object') {
        authStore.updateUser(json.data)
        console.log('更新用户个人简介成功:', authStore.getUser().email);
        promptAction.showToast({message:'修改成功', duration: 1000})
        router.back();
      } else {
        console.error('更新用户个人简介失败：'+json.message)
        promptAction.showToast({ message: json.message, duration: 1000 })
      }
    }).catch((err: BusinessError) => {
      console.info(`更新用户个人简介请求失败: ${err}`);
    })
  }

  build() {
    Column() {
      HdNav({ title: '修改个人简介', showRightIcon: false, hasBorder: true })

      // 手机号输入框
      Row() {
        TextArea({controller:this.controller,text:this.inputValue,placeholder:'请编辑您的个人简介（500字以内）'})
          .fontSize(16)
          .borderRadius(5)
          .borderWidth(1)
          .borderColor('#999999')
          .backgroundColor(Color.White)
          .height(150)
          .onChange((value:string)=>{
            this.inputValue = value;
          })
      }
      .margin({ top: 10 })
      .width('95%')

      // 验证码输入区域
      Row() {
        Text('请填写您的个人简介，让更多的人了解您')
          .fontSize(15)
          .fontColor('#333333')
          .width('95%')
      }
      .margin({top:15})
      .width('95%')
      .justifyContent(FlexAlign.SpaceBetween)

      // 登录按钮
      Button({type:ButtonType.Normal}){
        Text('保 存')
      }
      .width('90%')
      .height(48)
      .borderRadius(8)
      .borderWidth(1)
      .borderColor('#8B2316')
      .backgroundColor(Color.White)
      .fontColor('#8B2316')
      .fontSize(18)
      .position({x:'5%',y:'80%'})
      .onClick(() => {
        // 处理登录逻辑
        if (this.inputValue.length <= 0 || !this.inputValue) {
          promptAction.showToast({message:'请填写您的个人简介'});
          return;
        }
        if (this.inputValue.length > 500) {
          promptAction.showToast({message:'内容不能超过500字'});
          return;
        }
        this.commitIntroductionData(this.inputValue);
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#FFFFFF')
  }
}