//添加或者编辑不良类型记录
import ProcessRequest from '../common/util/request/ProcessRequest';
import RequestParamModel from '../viewmodel/RequestParamModel';
import DictValue from '../viewmodel/DictValue';
import ProcessBaseBug from '../viewmodel/process/ProcessBaseBug';
import ProcessInfo from '../viewmodel/process/ProcessInfo';
import ProcessDefectRecord from '../viewmodel/process/ProcessDefectRecord';
import RecordItem from '../viewmodel/process/RecordItem';
import prompt from '@ohos.prompt';

@CustomDialog
export struct RecordItemDialog {
  controller: CustomDialogController
  onConfirm: ()=> void = () => {}
  scroller: Scroller = new Scroller()
  //是否是编辑状态（需要传入的参数）
  @Prop isEdit: boolean = false
  @Prop RecordIndex:number
  recordItem: RecordItem = {}
  // 不良类型记录列表
  @Link recordItems: RecordItem[]
  // 备注
  @State recordContent: string = ''
  @State recordTitle: string = ''

  private deleteDefectRecord() :boolean{
    // 从数组中删除记录 - 使用索引查找
    if (this.RecordIndex >= 0 && this.RecordIndex < this.recordItems.length) {
      this.recordItems.splice(this.RecordIndex, 1)
      // 触发数组更新
      this.recordItems = [...this.recordItems]
      return true
    } else {
      return false
      console.error('未找到要删除的记录，索引无效：' + this.RecordIndex)
    }
  }
  aboutToAppear(): void {
    if(this.isEdit)
    {
      this.recordTitle = this.recordItem.title!
      this.recordContent = this.recordItem.content!
    }
  }


  build() {
    Column(){
      Column() {
        Text(this.isEdit ? '记录项' : '添加记录项')
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }
      .height('14%')
      .width('100%')
      .justifyContent(FlexAlign.Center)
      Column(){
        Text('标题')
          .fontSize($r('app.float.fontSize_16'))
          .fontColor($r('app.color.FFFFFF'))
        TextInput({ text: this.recordTitle, placeholder: '请输入标题' })
          .height('14%')
          .width('100%')
          .borderRadius($r('app.float.virtualSize_16'))
          .backgroundColor($r('app.color.20FFFFFF'))
          .fontSize($r('app.float.fontSize_24'))
          //.fontColor(this.selectDefectType === -1 ? $r('app.color.30FFFFFF'):$r('app.color.FFFFFF'))
          .margin({top:'3%',bottom:'3%'})
          .onChange((value: string) => {
            this.recordTitle = value
          })
        Text('内容')
          .fontSize($r('app.float.fontSize_16'))
          .fontColor($r('app.color.FFFFFF'))
        TextArea({placeholder:'请输入内容', text: this.recordContent})
          .backgroundColor($r('app.color.000000'))
          .height('60%')
          .width('100%')
          .fontColor($r('app.color.FFFFFF'))
          .placeholderColor($r('app.color.30FFFFFF'))
          .placeholderFont({size:$r('app.float.fontSize_16')})
          .fontSize($r('app.float.fontSize_16'))
          .textAlign(TextAlign.Start)
          .maxLength(200)
          .margin({top:'3%',bottom:'3%'})
          .borderRadius($r('app.float.virtualSize_16'))
          .onChange((value: string) => {
            this.recordContent = value
          })
      }
      .width('70%')
      .height('72%')
      .margin({left:'15%',right:'15%'})
      .alignItems(HorizontalAlign.Start)
      if(this.isEdit){
        Column() {
          Divider()
            .vertical(false)
            .strokeWidth(1)
            .color($r('app.color.15FFFFFF'))
          Row() {
            Row() {
              Text('删除')
                .fontColor($r('app.color.FF453A'))
                .fontSize($r('app.float.fontSize_30'))
            }
            .justifyContent(FlexAlign.Center)
            .width('33.3%')
            .onClick(async () => {
              AlertDialog.show(
                {
                  title: '确认删除',
                  message: '确定要删除这条记录项吗？',
                  primaryButton: {
                    value: '取消',
                    action: () => {
                      console.info('取消删除')
                    }
                  },
                  secondaryButton: {
                    value: '确定',
                    action: () => {
                      if (this.controller) {
                        this.controller.close()
                      }
                      if(this.deleteDefectRecord()) {
                        prompt.showToast({ message: '删除成功', duration: 2000 })
                      }
                    }
                  },
                  cancel: () => {
                    console.info('取消删除操作')
                  }
                }
              )
            })
            Divider()
              .vertical(true)
              .strokeWidth(1)
              .color($r('app.color.15FFFFFF'))
            Row() {
              Text('取消')
                .fontColor($r('app.color.60FFFFFF'))
                .fontSize($r('app.float.fontSize_30'))
            }
            .justifyContent(FlexAlign.Center)
            .width('33.3%')
            .onClick(() => this.controller.close())
            Divider()
              .vertical(true)
              .strokeWidth(1)
              .color($r('app.color.15FFFFFF'))
            Row() {
              Text('确定')
                .fontColor($r('app.color.007AFF'))
                .fontSize($r('app.float.fontSize_30'))
            }
            .justifyContent(FlexAlign.Center)
            .width('33.3%')
            .onClick(async () => {
              if(!this.recordTitle&&!this.recordContent)
              {
                prompt.showToast({ message: '标题和内容不能为空', duration: 2000 })
                return
              }
              this.recordItem.title = this.recordTitle
              this.recordItem.content = this.recordContent
              const updateRecordItem= this.recordItem
              this.recordItems.splice(this.RecordIndex,1,updateRecordItem)
              this.recordItems = [...this.recordItems]
              if (this.controller) {
                this.controller.close()
              }
            })
          }
        }
        .width('100%')
        .height('12%')
      }else{
        Column() {
          Divider()
            .vertical(false)
            .strokeWidth(1)
            .color($r('app.color.15FFFFFF'))
          Row() {
            Row() {
              Text('取消')
                .fontColor($r('app.color.60FFFFFF'))
                .fontSize($r('app.float.fontSize_30'))
            }
            .justifyContent(FlexAlign.Center)
            .width('50%')
            .onClick(() => this.controller.close())
            Divider()
              .vertical(true)
              .strokeWidth(1)
              .color($r('app.color.15FFFFFF'))
            Row() {
              Text('确定')
                .fontColor($r('app.color.007AFF'))
                .fontSize($r('app.float.fontSize_30'))
            }
            .justifyContent(FlexAlign.Center)
            .width('50%')
            .onClick(async () => {
              if(!this.recordTitle&&!this.recordContent)
              {
                prompt.showToast({ message: '标题和内容不能为空', duration: 2000 })
                return
              }
              this.recordItem.title = this.recordTitle
              this.recordItem.content = this.recordContent
              this.recordItems.push(this.recordItem)
              if (this.controller) {
                this.controller.close()
              }
            })
          }
        }
        .width('100%')
        .height('12%')
      }
    }
    .height('50%')
    .width('40%')
    .backgroundColor($r('app.color.2A2A2A'))
    .justifyContent(FlexAlign.End)
    .alignItems(HorizontalAlign.Start)
    .borderColor($r('app.color.000000'))
    .borderWidth(1)
    .borderRadius($r('app.float.virtualSize_16'))
  }
}