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

interface updateExtraData {
  content: string,
  expertUuid: string
}

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

@Entry
@Component
struct FeedbackPage {
  @State feedbackText: string = ''
  controller:TextAreaController = new TextAreaController();

  uploadChangePasswordAction() {
    hdHttp.post<string>(BasicConstant.urlExpert + 'feedBack', {
      expertUuid: authStore.getUser().uuid,
      content: this.feedbackText,
    } 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() {
      HdNav({ title: '意见反馈', showRightIcon: false, hasBorder: true })
      // 输入框区域
      TextArea({
        placeholder:'欢迎对我们的软件提供建议，帮助我们更快更好地改进"肝胆相照"APP(200字以内)',
        controller:this.controller
      })
        .onChange((value: string) => {
          this.feedbackText = value
        })
        .placeholderFont({size:16})
        .placeholderColor('#999999')
        .width('95%')
        .height(230)
        .margin(10)
        .fontSize(16)
        .backgroundColor(Color.White)
        .borderRadius(5)
        .border({
          width:1,
          color:'#999999'
        })

      // 提交按钮
      Button('提交', {
        type: ButtonType.Normal,
        stateEffect: true
      })
        .width('90%')
        .height(45)
        .backgroundColor(Color.White)
        .borderRadius(5)
        .fontColor('#8B2316')
        .position({x:'5%',y:'90%'})
        .border({
          width:1,
          color:'#8B2316'
        })
        .onClick(() => {
          if (this.feedbackText.length <= 0 || !this.feedbackText) {
            promptAction.showToast({message:'请输入您的建议'})
            return
          }
          if (this.feedbackText.length > 200) {
            promptAction.showToast({message:'内容不能大于200字'})
            return
          }
          this.uploadChangePasswordAction();
        })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.White)
  }
}