import { formatDate } from '../util/DateUtils'

@CustomDialog
export struct DatePickerDialog {
  @Prop selectedDateString:string = formatDate(new Date(),'YYYY-MM-DD');
  controller: CustomDialogController;

  // 添加回调函数属性
  private dateSelected: (date: string) => void = () => {};

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

  // 初始化日期范围（示例为1930-至今）
  private dateOptions: DatePickerOptions = {
    start: new Date('1930-01-01'),
    end: new Date(this.selectedDateString)
  }

  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)
          .backgroundColor(Color.Transparent)
          .fontColor($r('app.color.main_color'))
          .backgroundColor(Color.White)
          .width(80)
          .onClick(() => {
            this.controller.close()
            this.dateSelected(this.selectedDateString)
          })
      }
      .height(40)

      // 日期选择器主体
      DatePicker(this.dateOptions)
        .lunar(false)  // 禁用农历显示[1](@ref)
        .selectedTextStyle({
          color: '#007AFF',
          font: { size: 20, weight: FontWeight.Medium }
        })
        .onDateChange((selected:Date)=>{
          this.selectedDateString = formatDate(selected,'YYYY-MM-DD');
          console.info('select current date is: ' + formatDate(selected,'YYYY-MM-DD'));
        })
    }
    .width('100%')
    .height(240)
    .backgroundColor(Color.White)
  }
}
