import {  hdHttp, HdResponse,BasicConstant, logger,RequestDefaultModel, Data } from '@itcast/basic'
import { promptAction } from '@kit.ArkUI'
import HashMap from '@ohos.util.HashMap';
import { BusinessError } from '@kit.BasicServicesKit';
import { authStore } from '@itcast/basic'

interface  DefaultData {
  'officeName':string;
  'officeUuid':string;
}

@CustomDialog
export struct OfficeSelectedSheet {
  controller: CustomDialogController;
  @State officeNameArr:Array<string> = [];
  private officeArr:Array<DefaultData> = [];
  @State selectedModel:DefaultData = { officeName: '', officeUuid: '' };

  @State selectedIndex:number = 0;

  // 添加回调函数属性
  private officeSelected: (name:string , uuid:string) => void = () => {};

  // 修改构造函数
  constructor(controller: CustomDialogController, officeSelected: (name: string,uuid:string) => void) {
    super();
    this.controller = controller;
    this.officeSelected = officeSelected;
  }

  officeRequestUrl:string = BasicConstant.urlExpert+'officeList'
  hashMap: HashMap<string, string> = new HashMap();

  aboutToAppear(): void {
    this.uploadOffice();
  }

  uploadOffice() {
    hdHttp.httpReq<string>(this.officeRequestUrl,this.hashMap).then(async (res: HdResponse<string>) => {
      let json:RequestDefaultModel = JSON.parse(res+'') as RequestDefaultModel;
      if(json.code=='1') {
        this.officeArr = json.data as DefaultData[];
        this.officeNameArr = json.data.map(item => item.officeName);
        console.log('科室名称数组:', this.officeNameArr);
        for (let index = 0; index < this.officeNameArr.length; index++) {
          const officeObject = this.officeArr[index] as DefaultData;
          const name = this.officeNameArr[index];
          if (name == authStore.getUser().officeName) {
            this.selectedIndex = index;
            this.selectedModel = {officeName:name,officeUuid:officeObject.officeUuid}
          }
        }
      } else {
        console.error('科室数据失败：'+json.message)
        promptAction.showToast({ message: json.message, duration: 1000 })
      }
    }).catch((err: BusinessError) => {
      console.info(`Response fail: ${err}`);
    })
  }

  build() {
    Column() {
      // 操作按钮区域
      Row({space:70}) {
        Button('取消')
          .layoutWeight(1)
          // .backgroundColor(Color.Transparent)
          .fontColor($r('app.color.main_color'))
          .backgroundColor(Color.White)
          .width(80)
          .onClick(() => {
            this.controller.close()
          })

        Text('请选择科室')
          .fontSize(15)
          .fontColor('#666666')

        Button('确定')
          .layoutWeight(1)
          .fontColor($r('app.color.main_color'))
          .backgroundColor(Color.White)
          .width(80)
          .onClick(() => {
            this.controller.close()
            this.officeSelected(this.selectedModel.officeName, this.selectedModel.officeUuid);
          })
      }
      .height(40)

      TextPicker({
        range:this.officeNameArr,
        selected:this.selectedIndex
      })
        .selectedTextStyle({
          color: '#007AFF',
          font: { size: 20, weight: FontWeight.Medium }
        })
        .onChange((name: string | string[], index: number | number[]) => {
          // 处理单列选择场景
          if (typeof index === "number" && this.officeArr[index]) {
             this.selectedModel = this.officeArr[index];
          }
        })
    }
    .width('100%')
    .height(240)
    .backgroundColor(Color.White)
  }
}
