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

interface updateExtraData {
  expertUuid: string,
  content:string
}

interface callBackData {
  code: number,
  message:string,
  data: Array<object>
}

@Entry
@Component
struct CancelAccount {
  @State feedbackText: string =
    ' 账户注销后不可恢复，您将永久放弃此账户现有权益\n' +
    ' 1.您将不能再使用此账户随访患者，不能接收随访患者发送的信息\n' +
    ' 2.您所有的咨询、订单、下载记录将被清除且不能找回\n' +
    ' 3.肝胆相照平台提供的文献、视频等服务将结束\n' +
    ' 4.申请注销账户前，请确保账户相关权益已结清\n' +
    ' 收到您的注销申请后，我们将在10个工作日内帮您完成注销及数据清除工作'
  controller:TextAreaController = new TextAreaController();

  uploadCancleAccountAction() {
    hdHttp.post<string>(BasicConstant.urlExpert + 'patientList', {
      expertUuid: authStore.getUser().uuid
    } as updateExtraData).then(async (res: HdResponse<string>) => {
      let json: callBackData = JSON.parse(res + '') as callBackData;
      console.log('注销账户数据:', json);
      if (json.data.length > 0) {
        promptAction.showDialog({
          title: '温馨提示',
          message: '请您解除所有随访患者后再提交注销申请',
          buttons: [{ text: '确定', color: '#000000' }]
        })
      } else {
        this.uploadFeedBackAction();
      }
    }).catch((err: BusinessError) => {
      console.info(`Response fail: ${err}`);
    })
  }

  uploadFeedBackAction() {
    hdHttp.post<string>(BasicConstant.urlExpert + 'feedBack', {
      expertUuid: authStore.getUser().uuid,
      content: '注销此账户',
    } as updateExtraData).then(async (res: HdResponse<string>) => {
      let json: callBackData = JSON.parse(res + '') as callBackData;
      console.log('提交注销账户数据:', json);
      if (json.code == 1) {
        promptAction.showDialog({
          title: '温馨提示',
          message: '提交成功！',
          buttons: [{ text: '取消', color: '#000000' }, { text: '确定', color: '#000000' }]
        })
      } else {
        promptAction.showToast({ message: json.message });
      }
    }).catch((err: BusinessError) => {
      console.info(`Response fail: ${err}`);
    })
  }

  build() {
    Column() {
      HdNav({ title: '注销账户', showRightIcon: false, hasBorder: true })

      Row() {
        Text(this.feedbackText)
          .fontSize(13)
          .margin(8)
      }
      .width('100%')
      .height('auto')
      .margin(0)
      .backgroundColor(Color.White)
      .borderRadius(5)
      .border({
        width:1,
        color:$r('app.color.main_color')
      })

      // 提交按钮
      Button('我已确认风险，提交注销申请', {
        type: ButtonType.Normal,
        stateEffect: true
      })
        .fontSize(13)
        .fontColor(Color.White)
        .width('90%')
        .height(45)
        .backgroundColor('#8B2316')
        .borderRadius(3)
        .margin(20)
        .border({
          width:1,
          color:'#8B2316'
        })
        .onClick(() => this.submitFeedback())
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.White)
  }
  // 提交反馈处理
  private submitFeedback(): void {
    // 提交逻辑实现
    promptAction.showDialog({
      title:'注销账户',
      message:'确定要注销肝胆相照平台账号吗？\n注销后您的所有数据将无法恢复',
      alignment:DialogAlignment.Center,
      buttons:[
        {
          text:'取消',
          color:'#333333'
        },
        {
          text:'确定',
          color:'#999999'
        }
      ]
    })
      .then(data=>{
        if (data.index === 1) {
          this.uploadCancleAccountAction();
        }
      })
      .catch((error:Error)=>{
        console.info('showDialog error:'+error)
      })
  }
}