//齐套接收
import ProcessRequest from '../common/util/request/ProcessRequest'
import MaterialInfo from '../viewmodel/MaterialInfo'
import OperationInfo from '../viewmodel/process/OperationInfo'
import RequestParamModel from '../viewmodel/RequestParamModel'
import VehicleInfo from '../viewmodel/VehicleInfo'

@CustomDialog
export struct CompleteReceiveDialog {
  private scrollerMaterial: Scroller = new Scroller()
  private scrollerVehicle: Scroller = new Scroller()
  //工序载具列表
  @Link processVehicleList: VehicleInfo[]
  //当前工单号
  currentWorkOrderCode:string = ''
  //当前工序号
  @Link currentOperationId:string
  //当前载具物料列表
  @State vehicleMaterialsList: MaterialInfo[] = [];
  //载具绑定工序id
  @State vehicleOperationId:string =''
  //选择的载具索引
  @State selectedVehicleIndex: number = -1
  controller: CustomDialogController
  onConfirm: () => void = () => {}

  //加载工序所有载具
  // loadProcessVehicle = async () => {
  //   let res = await ProcessRequest.post('/api/v1/process/vehicleOperation/list', {
  //     workOrderCode:this.currentWorkOrderCode,
  //     operationId:this.currentOperationId
  //   } as RequestParamModel) as OperationInfo;
  //   this.processVehicleList = res.processVehicleList??[]
  // };

  //加载载具物料信息
  loadVehicleMaterial = async () => {
    this.vehicleMaterialsList = await ProcessRequest.post('/api/v1/process/vehicleMaterial/list', {
      vehicleOperationId:this.vehicleOperationId
    } as RequestParamModel) as MaterialInfo[];
  };

  //确认出库
  confirmOutBound = async ()=>{
    let res = await ProcessRequest.post('/api/v1/process/vehicleOperation/vehicleOut', {
      vehicleOperationId:this.vehicleOperationId
    } as RequestParamModel) as VehicleInfo;
  }


  private onSelectOrder(index: number) {
    this.selectedVehicleIndex = index
  }

  // aboutToAppear(): void {
  //   this.loadProcessVehicle();
  // }

  build() {
    Column() {
      Column() {
        Text("齐套接收")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }.height('8%')
      .width('100%')
      .justifyContent(FlexAlign.Center)

      Row(){
        Column() {
          List({scroller:this.scrollerVehicle}) {
            ForEach(this.processVehicleList, (item: VehicleInfo, index) => {
              ListItem(){
                Column() {
                  Text(`${item.vehicleCode}`)
                    .fontSize($r('app.float.fontSize_24'))
                    .fontColor($r('app.color.FFFFFF'))
                    .textAlign(TextAlign.Start)
                    .margin({bottom:'2%' })
                  Text(`工单编号: ${item.workOrderCode}`)
                    .fontColor($r('app.color.FFFFFF'))
                    .fontSize($r('app.float.fontSize_16'))
                    .fontWeight(FontWeight.Lighter)
                    .margin({bottom:'1%' })
                  Text(`工序: ${item.operationName}`)
                    .fontColor($r('app.color.FFFFFF'))
                    .fontSize($r('app.float.fontSize_16'))
                    .fontWeight(FontWeight.Lighter)
                }
                .alignItems(HorizontalAlign.Start)
                .height('90%')
                .width('90%')
                .margin({left:'2%' })
                .justifyContent(FlexAlign.Center)
              }
              .height('15%')
              .width('100%')
              .backgroundColor(index === this.selectedVehicleIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF'))
              .borderRadius($r('app.float.virtualSize_24'))
              .border({
                width: index === this.selectedVehicleIndex ? 2 : 0,
                color: index === this.selectedVehicleIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF')
              })
              .onClick(() => {
                this.onSelectOrder(index)
                this.vehicleOperationId = item.id??""
                this.loadVehicleMaterial()
              })

            })
          }
          .width('90%')
          .height('100%')
        }
        .width('48%')
        .alignItems(HorizontalAlign.Center)
        Divider()
          .vertical(true)
          .strokeWidth(1)
          .color($r('app.color.15FFFFFF'))
          .margin({ bottom: '2%',left:'2%',right:'2%'})
        Column() {
          List({scroller:this.scrollerMaterial}) {
            ForEach(this.vehicleMaterialsList, (item:MaterialInfo) => {
              ListItem() {
                Row() {
                  Column(){
                    Text(item.materialName)
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                    Text(`型号： ${item.materialCode}`)
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                      .fontWeight(FontWeight.Lighter)
                  }
                  .width('80%')
                  .alignItems(HorizontalAlign.Start)
                  Row(){
                    Text(`${item.num}`)
                      .fontSize($r('app.float.fontSize_30'))
                      .fontColor($r('app.color.FFFFFF'))
                    Text(`${item.unit}`)
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                      .fontWeight(FontWeight.Lighter)
                      .margin({left:'1%'})
                  }
                  .width('20%')
                  .alignItems(VerticalAlign.Bottom)
                  .height('100%')
                  .justifyContent(FlexAlign.Center)
                }
                .width('100%')
                .justifyContent(FlexAlign.SpaceEvenly)
                .height('8%')
              }
            })
          }
          .width('90%')
          .height('100%')
          .divider({
            strokeWidth: 1,
            color: $r('app.color.20FFFFFF')
          })
        }
        .width('48%')

      }
      .height('81%')
      .width('100%')
      .margin({top:'2%'})

      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(() => {
            if(this.selectedVehicleIndex==-1) return
            this.confirmOutBound()
            this.controller.close();
          })
        }
      }
      .width('100%')
      .height('8%')

    }
    .height('71%')
    .width('62%')
    .backgroundColor($r('app.color.2A2A2A'))
    .justifyContent(FlexAlign.End)
    .alignItems(HorizontalAlign.Start)
    .borderColor($r('app.color.000000'))
    .borderWidth(1)
    .borderRadius($r('app.float.virtualSize_16'))
  }
}
