import { HdNav,BasicConstant } from '@itcast/basic'
import { emitter } from '@kit.BasicServicesKit'
import { router,promptAction } from '@kit.ArkUI';
import { hdHttp, HdResponse ,logger} from '@itcast/basic/Index'
import HashMap from '@ohos.util.HashMap';
import { BusinessError } from '@kit.BasicServicesKit';
import inputMethod from '@ohos.inputMethod';

@Entry
@Component
struct CommentReplyPage {
  @State params:Record<string, string> = router.getParams() as Record<string, string>;
  @State finalContent: string = '';
  hashMap: HashMap<string, string> = new HashMap();

  aboutToAppear() {
    emitter.on({ eventId: 250516 }, () => {
      this.commitAction()
    })
  }

  aboutToDisappear() {
    emitter.off(250516)
  }

  commitAction() {
    inputMethod.getController().hideTextInput();//关闭键盘
    if (this.finalContent.length === 0) {
      promptAction.showToast({ message: '请输入回复内容', duration: 1000 })
      return;
    }
    this.hashMap.clear();
    this.hashMap.set('article_uuid', this.params.comment_id)
    this.hashMap.set('comment', `${this.finalContent}||${this.params.replyName}：${this.params.comment}`)
    this.hashMap.set('type', '8')
    hdHttp.httpReq<string>(BasicConstant.addCommentV2,this.hashMap).then(async (res: HdResponse<string>) => {
      logger.info('Response replyComment:'+res);
      let json:CommentV2Model = JSON.parse(res+'') as CommentV2Model;
      if(json.code == 200) {
        emitter.emit({
          eventId: 250529,
          priority: emitter.EventPriority.HIGH
        })
        promptAction.showToast({ message: '回复评论成功', duration: 1000 })
        router.back();
      } else {
        promptAction.showToast({ message: json.message, duration: 1000 })
      }
    }).catch((err: BusinessError) => {
    })
  }

  build() {
    Column() {
      HdNav({ title: '回复', showRightIcon: false, showLeftIcon: true,showRightText:true,rightText:'确定'})
      TextArea({placeholder:`回复：${this.params.replyName}`})
        .fontSize(14)
        .width('100%')
        .height('50%')
        .backgroundColor(Color.White)
        .onChange((value: string) => {
          this.finalContent = value; // 实时更新临时内容[3](@ref)
          console.info('回复输入框内容：', this.finalContent);
        })
    }
    .height('100%')
    .width('100%')
  }
}

export interface CommentV2Model{
  code:number;
  data:Object[];
  message:string;
  msg:string
}