import ProcessRequest from '../common/util/request/ProcessRequest'
import { MessageInfo, MessagePage } from '../viewmodel/MessageInfo'
import TaskSeqVO from '../viewmodel/process/TaskSeqInfo'
import { DeptInfo, ProductionLine, WorkstationInfo } from '../viewmodel/UserInfo'
import RequestParamModel from '../viewmodel/RequestParamModel'

@CustomDialog
export struct SwitchingStationDialog{
  private scrollerList: Scroller = new Scroller()
  //当前工位
  @Consume('currentStation') currentStation:string
  //当前部门
  @Consume('currentDept') currentDept:string
  //当前产线code
  @Consume('currentPLCode') currentPLCode:string
  // 当前工位id
  @Consume('currentStationId') currentStationId:string

  @State selectStationIndex: number = -1
  //工位列表
  @State stationsList : WorkstationInfo[]=[]
  loadStations = async () => {
    this.stationsList = await ProcessRequest.post(`/api/v1/base/station/list`, {} as RequestParamModel) as WorkstationInfo[];
  };
  controller: CustomDialogController
  aboutToAppear(): void {
    this.loadStations();
  }

  private onSelectStation(index: number) {
    this.selectStationIndex = index
  }

  build() {
    Column() {
      Column() {
        Text("切换工位")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }
      .height('8%')
      .width('100%')
      .justifyContent(FlexAlign.Center)

      Column(){
            List({space:10,scroller:this.scrollerList}) {
              ForEach(this.stationsList, (item:WorkstationInfo,index) => {
                ListItem() {
                    Column(){
                      Text(item.name)
                        .fontSize($r('app.float.fontSize_24'))
                        .fontColor($r('app.color.FFFFFF'))
                    }  .backgroundColor(index === this.selectStationIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF'))
                    .borderRadius($r('app.float.virtualSize_16'))
                    .padding(8)
                    .width('100%')
                    .height('12%')
                    .alignItems(HorizontalAlign.Center)
                    .justifyContent(FlexAlign.Center)
                    .border({
                      width: index === this.selectStationIndex ? 2 : 0,
                      color: index === this.selectStationIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF')
                    })
                    .onClick(() => {
                      this.onSelectStation(index)
                    })
                }
              })
            }
            .width('70%')
            .height('100%')
          }
      .justifyContent(FlexAlign.SpaceEvenly)
      .width('100%')
      .height('81%')
      .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.stationsList && this.selectStationIndex >=0 && this.stationsList[this.selectStationIndex]) {
              this.currentStation = this.stationsList[this.selectStationIndex].name ?? ""
              this.currentStationId = this.stationsList[this.selectStationIndex].id ?? ""
            }
            this.controller.close();
          })
        }
      }
      .width('100%')
      .height('8%')

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

@CustomDialog
export struct SwitchingProductLineDialog{
  private scrollerList: Scroller = new Scroller()
  //当前产线
  @Consume('currentProductLine') currentProductLine:string
  //当前产线code
  @Consume('currentPLCode') currentPLCode:string

  @State selectProductLineIndex: number = -1
  //产线列表
  @State productLineList : ProductionLine[]=[]
  loadStations = async () => {
    this.productLineList = await ProcessRequest.post('api/v1/base/productionLine/list/list', {}as RequestParamModel)  as ProductionLine[];
  };
  controller: CustomDialogController
  aboutToAppear(): void {
    this.loadStations();
  }

  private onSelectProductLine(index: number) {
    this.selectProductLineIndex = index
  }

  build() {
    Column() {
      Column() {
        Text("切换工位")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }
      .height('8%')
      .width('100%')
      .justifyContent(FlexAlign.Center)

      Column(){
        List({space:10,scroller:this.scrollerList}) {
          ForEach(this.productLineList, (item:ProductionLine,index) => {
            ListItem() {
              Column(){
                Text(item.name)
                  .fontSize($r('app.float.fontSize_24'))
                  .fontColor($r('app.color.FFFFFF'))
              }  .backgroundColor(index === this.selectProductLineIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF'))
              .borderRadius($r('app.float.virtualSize_16'))
              .padding(8)
              .width('100%')
              .height('12%')
              .alignItems(HorizontalAlign.Center)
              .justifyContent(FlexAlign.Center)
              .border({
                width: index === this.selectProductLineIndex ? 2 : 0,
                color: index === this.selectProductLineIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF')
              })
              .onClick(() => {
                this.onSelectProductLine(index)
              })
            }
          })
        }
        .width('70%')
        .height('100%')
      }
      .justifyContent(FlexAlign.SpaceEvenly)
      .width('100%')
      .height('81%')
      .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(() => {
            this.currentProductLine = this.productLineList[this.selectProductLineIndex].name??""
            this.currentPLCode =  this.productLineList[this.selectProductLineIndex].code??""
            this.controller.close();
          })
        }
      }
      .width('100%')
      .height('8%')

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

@CustomDialog
export struct SwitchingDeptDialog{
  private scrollerList: Scroller = new Scroller()
  //当前部门
  @Consume('currentDept') currentDept:string
  // 当前组织id
  @Consume('currentOrgId') currentOrgId:number

  @State selectDeptIndex: number = -1
  //部门列表
  @State departmentsList : DeptInfo[]=[]
  loadStations = async () => {
    this.departmentsList = await ProcessRequest.get('/api/v1/sys/dept/orgList', {})  as DeptInfo[];
  };
  controller: CustomDialogController
  aboutToAppear(): void {
    this.loadStations();
  }

  private onSelectStation(index: number) {
    this.selectDeptIndex = index
  }

  build() {
    Column() {
      Column() {
        Text("切换部门")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }
      .height('8%')
      .width('100%')
      .justifyContent(FlexAlign.Center)

      Column(){
        List({space:10,scroller:this.scrollerList}) {
          ForEach(this.departmentsList, (item:DeptInfo,index) => {
            ListItem() {
              Column(){
                Text(item.deptName)
                  .fontSize($r('app.float.fontSize_24'))
                  .fontColor($r('app.color.FFFFFF'))
              }  .backgroundColor(index === this.selectDeptIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF'))
              .borderRadius($r('app.float.virtualSize_16'))
              .padding(8)
              .width('100%')
              .height('12%')
              .alignItems(HorizontalAlign.Center)
              .justifyContent(FlexAlign.Center)
              .border({
                width: index === this.selectDeptIndex ? 2 : 0,
                color: index === this.selectDeptIndex ? $r('app.color.2030D158') : $r('app.color.20FFFFFF')
              })
              .onClick(() => {
                this.onSelectStation(index)
              })
            }
          })
        }
        .width('70%')
        .height('100%')
      }
      .justifyContent(FlexAlign.SpaceEvenly)
      .width('100%')
      .height('81%')
      .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(() => {
            this.currentDept = this.departmentsList[this.selectDeptIndex].deptName??""
            this.currentOrgId = this.departmentsList[this.selectDeptIndex].id??0
            this.controller.close();
          })
        }
      }
      .width('100%')
      .height('8%')

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


