import ProcessRequest from '../../common/util/request/ProcessRequest'
import ProcessCheck from '../../viewmodel/process/ProcessCheck'
import ProcessInfo from '../../viewmodel/process/ProcessInfo'
import RequestParamModel from '../../viewmodel/RequestParamModel'
import promptAction from '@ohos.promptAction'
import PageInfo from '../../viewmodel/PageInfo'
import { DefectRecordDialog } from '../DefectRecordDialog'
import DictValue from '../../viewmodel/DictValue'
import ProcessDefectRecord from '../../viewmodel/process/ProcessDefectRecord'
import OperationComponent from '../../viewmodel/process/OperationComponent'

// 自检工序
@Component
export struct SelfInspectView {
  @Link opComponents: OperationComponent[]
  @Link index:number
  // 点检列表
  @Link checkArray: ProcessCheck[]
  @State selectCheck: number = -1
  // 不良记录列表
  @Link defectRecords: ProcessDefectRecord[]
  // 不良类型（数据字典）
  @State defectTypes: DictValue[] = []
  @State isAllCheck:boolean = false
  // // 扫码开工状态(0:未开工 1:已开工)
  @State scanState:number =1
  @State selectDefectIndex:number = -1

  editDefectRecord: boolean = false
  // 修改时选中的不良类型记录
  selectDefectRecord: ProcessDefectRecord = {}

  defectTypeDictCode: string = 'defect_mana'

  allChecksCompleted(): boolean {
    for (let i = 0; i < this.checkArray.length; i++) {
      const item = this.checkArray[i]
      if (!item || item.isCheck !== true) {
        console.log(`项目 ${i} 未完成:`, item)
        return false
      }
    }

    console.log('所有点检项已完成')
    return true
  }


  async aboutToAppear() {
    this.isAllCheck = this.allChecksCompleted()

  }

  build() {
    Row() {
      // 点检
      Column() {
        Row() {
          Text('点检列表')
            .fontSize($r('app.float.fontSize_24'))
            .fontWeight(FontWeight.Medium)
            .fontColor($r('app.color.FFFFFF'))
        }
        .height('10.2%')
        .width('100%')

        List({space: 6}) {
          ForEach(this.checkArray, (item: ProcessCheck, index: number) => {
            ListItem() {
              Column() {
                Row() {
                  Text(item.checkName ? item.checkName : '')
                    .fontSize($r('app.float.fontSize_24'))
                    .fontWeight(FontWeight.Medium)
                    .fontColor($r('app.color.FFFFFF'))
                  Row() {
                    Text('检查确认')
                      .fontSize($r('app.float.fontSize_16'))
                      .fontWeight(FontWeight.Regular)
                      .fontColor($r('app.color.FFFFFF'))
                    Toggle({ type: ToggleType.Switch, isOn: item.isCheck  })
                      .selectedColor($r('app.color.30D158'))
                      .switchPointColor($r('app.color.FFFFFF'))
                      //.enabled(this.scanState === 1 ? true : false)
                      .onChange( (isOn: boolean) => {
                        this.selectCheck = index
                        this.checkArray[index].isCheck = isOn
                        this.isAllCheck = this.allChecksCompleted()
                      })
                  }
                  .height('100%')
                }
                .width('95.3%')
                .justifyContent(FlexAlign.SpaceBetween)
                .height($r('app.float.virtualSize_88'))
                Column() {
                  Text('点检内容：' + item.content ? item.content : '')
                    .fontSize($r('app.float.fontSize_16'))
                    .fontWeight(FontWeight.Lighter)
                    .fontColor($r('app.color.FFFFFF'))
                }
                .width('95.3%')
                .alignItems(HorizontalAlign.Start)
              }
              .borderRadius($r('app.float.virtualSize_16'))
              .backgroundColor(this.selectCheck === index && this.scanState === 1 ? $r('app.color.2030D158') : $r('app.color.10FFFFFF'))
              .width('100%')
              .onClick(()=>{
                this.selectCheck = index
              })
            }
          })
        }
        .width('100%')
        .height('87.6%')
        .alignListItem(ListItemAlign.Center)
      }
      .height('100%')
      .width('59%')
      Divider()
        .vertical(true)
        .strokeWidth(1)
        .color($r('app.color.15FFFFFF'))
        .height('100%')
        .padding({top: '5.3%', bottom: '2.2%'})
      // 不良类型记录
      Column() {
        Row() {
          Text('不良类型记录')
            .fontSize($r('app.float.fontSize_24'))
            .fontWeight(FontWeight.Medium)
            .fontColor($r('app.color.FFFFFF'))
        }
        .height('10.2%')
        .width('100%')
        // 添加不良类型
        Row({space: 4}) {
          Image($r("app.media.general_create"))
            .width($r('app.float.virtualSize_24'))
            .height($r('app.float.virtualSize_24'))
            .fillColor($r('app.color.0A84FF'))
          Text('添加')
            .fontColor($r('app.color.0A84FF'))
            .fontSize($r('app.float.fontSize_24'))
            .fontWeight(FontWeight.Medium)
        }
        .width('28.2%')
        .height('7.1%')
        .justifyContent(FlexAlign.Center)
        .backgroundColor($r('app.color.20FFFFFF'))
        .borderRadius($r('app.float.fontSize_16'))
        .onClick(()=>{
          this.editDefectRecord = false
          this.defectRecordController.open()
        })
        // 不良类型记录列表
        List({space: 6}) {
          ForEach(this.defectRecords, (item: ProcessDefectRecord, index: number) => {
            ListItem() {
              Column() {
                Text(item.defectType ? item.defectType! + '（' + item.bugName! + '）' : '')
                  .fontColor($r('app.color.FFFFFF'))
                  .fontSize($r('app.float.fontSize_16'))
                  .fontWeight(FontWeight.Bold)
                  .height($r('app.float.virtualSize_48'))
                Text('备注：' + (item.remark ? item.remark : ''))
                  .fontColor($r('app.color.FFFFFF'))
                  .fontSize($r('app.float.fontSize_16'))
                  .fontWeight(FontWeight.Lighter)
                  .padding({bottom: 10})
              }
              .width('92%')
              .alignItems(HorizontalAlign.Start)
            }
            .backgroundColor($r('app.color.20FFFFFF'))
            .borderRadius($r('app.float.fontSize_16'))
            .width('100%')
            .onClick(()=>{
              this.selectDefectRecord = item
              this.selectDefectIndex =index
              this.editDefectRecord = true
              this.defectRecordController.open()
            })
          })
        }
        .width('100%')
        .height('70.8%')
        .alignListItem(ListItemAlign.Center)
        .margin({top: 10})
        Row({space: 4}) {
          Image($r("app.media.next_step"))
            .width($r('app.float.virtualSize_24'))
            .height($r('app.float.virtualSize_24'))
            .fillColor($r('app.color.0A84FF'))
          Text('下一步')
            .fontColor($r('app.color.0A84FF'))
            .fontSize($r('app.float.fontSize_24'))
            .fontWeight(FontWeight.Medium)
        }
        .width('28.2%')
        .height('7.1%')
        .justifyContent(FlexAlign.Center)
        .backgroundColor(this.isAllCheck ?$r('app.color.20FFFFFF'):$r('app.color.20000000'))
        .borderRadius($r('app.float.fontSize_16'))
        .onClick(()=>{
          if (!this.isAllCheck) {
            promptAction.showToast({
              message: '请完成所有点检项后再进行下一步',
              duration: 1500,
              bottom: 100
            })
            return
          }
          this.opComponents[this.index].isComplete=true
          this.index++

        })
      }
      .height('100%')
      .width('35.4%')
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.SpaceEvenly)
    .alignItems(VerticalAlign.Top)
  }

  // 添加/修改不良类型记录弹窗
  defectRecordController: CustomDialogController = new CustomDialogController({
    builder: DefectRecordDialog({
      isEdit: this.editDefectRecord,
      defectRecords:this.defectRecords,
      defectRecord:this.editDefectRecord ? this.selectDefectRecord : {},
      defectIndex:this.selectDefectIndex,
    }),
    autoCancel: true, // 点击遮罩关闭
    customStyle: true,
    alignment:DialogAlignment.Center,
    maskColor: 'rgba(0,0,0,0.8)',  // 黑色遮罩
  })
}