//设备点检
import ProcessRequest from '../common/util/request/ProcessRequest'
import ProcessDeviceDailyCheck from '../viewmodel/process/ProcessDeviceDailyCheck'
import DictValue from '../viewmodel/DictValue'
import promptAction from '@ohos.promptAction'
import HashMap from '@ohos.util.HashMap'


@CustomDialog
export struct DeviceInspectionDialog {
  private scrollerDevice: Scroller = new Scroller()
  //查找设备编码
  @State queryDeviceNo: string = 'KLA-METRO01'
  //当前日期
  @State currentDate: string = ''
  // 设备点检列表
  @Link deviceChecks: ProcessDeviceDailyCheck[]
  //选中设备索引
  @State selectDeviceIndex: number=-1
  // 设备类型(key为数据字典值，value为数据字典标签)
  @State deviceTypes: HashMap<string, string> = new HashMap()
  @Consume('stationIp') stationIp: string
  controller: CustomDialogController
  onConfirm: () => void = () => {}

  deviceTypeDictCode: string = 'device_type'

  async aboutToAppear() {
    let deviceDicts: DictValue[] = await ProcessRequest.get(`/api/v1/sys/dictData/queryByType/${this.deviceTypeDictCode}`)
    if (deviceDicts) {
      for (const dict of deviceDicts) {
        console.log('hhtest', JSON.stringify(dict))
        this.deviceTypes.set(dict.dictValue!, dict.dictLabel!);
      }
    }
  }

  build() {
    Column() {
      Column() {
        Text("设备点检")
          .fontColor($r('app.color.FFFFFF'))
          .fontSize($r('app.float.fontSize_30'))
      }
      .height('8%')
      .width('100%')
      .justifyContent(FlexAlign.Center)

      Column() {
        Text("扫描设备 ")
            .fontColor($r('app.color.FFFFFF'))
            .fontSize($r('app.float.fontSize_24'))
        Row(){
          Row() {
            // 左侧二维码图标
            Image($r('app.media.material_qr_code'))
              .width($r('app.float.virtualSize_32'))
              .height($r('app.float.virtualSize_32'))
              .fillColor($r('app.color.FFFFFF'))
              .objectFit(ImageFit.Contain)
              .margin({left:'2%'})
            // 扫码输入框
            TextInput({text:this.queryDeviceNo, placeholder: '请扫描设备编码' })
              .type(InputType.Normal)
              .placeholderFont({ size: $r('app.float.fontSize_16') })
              .placeholderColor($r('app.color.30FFFFFF'))
              .fontSize($r('app.float.fontSize_16'))
              .fontColor($r('app.color.FFFFFF'))
              .enableKeyboardOnFocus(false)
              .onSubmit(async () => {
                if (this.deviceChecks) {
                  for (const element of this.deviceChecks) {
                    if (element.deviceNo === this.queryDeviceNo) {
                      promptAction.showToast({
                        message: `设备今日已点检，无需重复点检！`,
                        duration: 1500,
                        bottom: 100
                      })
                      return
                    }
                  }
                }
                let check: ProcessDeviceDailyCheck = await ProcessRequest.get(`/api/v1/process/equipDailyCheck/getServiceLifeByDeviceNo/${this.queryDeviceNo}`)
                this.deviceChecks.unshift(check)
              })
              .onChange((value: string) => {
                this.queryDeviceNo = value;
                //this.onQueryDeviceCode()
              })
          }
          .height('100%')
          .width('65%')
          .borderRadius($r('app.float.virtualSize_16'))
          .backgroundColor($r('app.color.000000'))
          Text(this.currentDate)
            .fontColor($r('app.color.FFFFFF'))
            .fontSize($r('app.float.fontSize_16'))
            .margin({left:'3%'})
        }
        .height('8%')
        .width('50%')
        .margin({bottom:'1%',top:'1%'})
        List({scroller: this.scrollerDevice }) {
            ForEach(this.deviceChecks, (item: ProcessDeviceDailyCheck, index) => {
              ListItem() {
                Row() {
                  Column(){
                    Text(`${item.deviceName!}`)
                      .fontSize($r('app.float.fontSize_24'))
                      .fontColor($r('app.color.FFFFFF'))
                    Text('型号：' + (this.deviceTypes && item.deviceType && this.deviceTypes.hasKey(item.deviceType) ? this.deviceTypes.get(item.deviceType) : ''))
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                      .fontWeight(FontWeight.Lighter)
                      .margin({top:'2%',bottom:'1%'})
                    Text(`设备编码：${item.deviceNo!}`)
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                      .fontWeight(FontWeight.Lighter)
                  }
                  .width('30%')
                  .height('100%')
                  .justifyContent(FlexAlign.Center)
                  .alignItems(HorizontalAlign.Start)
                  .margin({right:'16%'})
                  Column({space:5}){
                    Text(`计量有效期：`)
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                      .fontWeight(FontWeight.Lighter)
                    Row(){
                      Image(item.meteringState! === 1 ? $r('app.media.device_normal') : $r('app.media.device_expire'))
                        .width($r('app.float.virtualSize_24'))
                        .height($r('app.float.virtualSize_24'))
                        .margin({left:'2%'})
                      Text(item.meteringDate ? `${item.meteringDate}` : '长期有效')
                        .fontSize($r('app.float.fontSize_16'))
                        .fontColor(item.meteringState! === 1 ? $r('app.color.30D158') : $r('app.color.FF453A'))
                        .margin({left:'2%'})
                    }
                    .width('65%')
                    .borderRadius($r('app.float.virtualSize_16'))
                    .backgroundColor($r('app.color.000000'))
                    .height('25%')
                    .alignItems(VerticalAlign.Center)
                  }
                  .width('22%')
                  .height('100%')
                  .justifyContent(FlexAlign.Center)
                  .alignItems(HorizontalAlign.Start)
                  Column({space:5}){
                    Text(`维保有效期：`)
                      .fontSize($r('app.float.fontSize_16'))
                      .fontColor($r('app.color.FFFFFF'))
                      .fontWeight(FontWeight.Lighter)
                    Row(){
                      Image(item.warrantyState! === 1 ? $r('app.media.device_normal') :$r('app.media.device_expire'))
                        .width($r('app.float.virtualSize_24'))
                        .height($r('app.float.virtualSize_24'))
                        .margin({left:'2%'})
                      Text(item.warrantyPeriod ? `${item.warrantyPeriod}` : '长期有效')
                        .fontSize($r('app.float.fontSize_16'))
                        .fontColor(item.warrantyState! === 1 ? $r('app.color.30D158') : $r('app.color.FF453A'))
                        .margin({left:'2%'})
                    }
                    .width('65%')
                    .borderRadius($r('app.float.virtualSize_16'))
                    .backgroundColor($r('app.color.000000'))
                    .height('25%')
                    .alignItems(VerticalAlign.Center)
                  }
                  .width('22%')
                  .height('100%')
                  .justifyContent(FlexAlign.Center)
                  .alignItems(HorizontalAlign.Start)
                  Column(){
                   Image($r('app.media.process_delete_seq'))
                     .width($r('app.float.virtualSize_48'))
                     .height($r('app.float.virtualSize_48'))
                     .fillColor($r('app.color.FF453A'))
                     .onClick(()=>{
                       this.deviceChecks.splice(index, 1);
                     })
                  }
                  .width('10%')
                  .height('100%')
                  .justifyContent(FlexAlign.Center)

                }
                .height('90%')
                .width('95%')
                .justifyContent(FlexAlign.Start)
              }
              .height('18%')
              .width('100%')
              .margin({ bottom: 8})
              .borderRadius($r('app.float.virtualSize_16'))
              .backgroundColor($r('app.color.10FFFFFF'))
            })
          }.height('82%')
        }
        .width('96%')
        .height('84%')
        .alignItems(HorizontalAlign.Start)
        .justifyContent(FlexAlign.Start)
        .margin({left:'2%',right:'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(async () => {
            if (!this.deviceChecks || this.deviceChecks.length <= 0) {
               return
            }
            for (const check of this.deviceChecks) {
              check.stationIp = this.stationIp
            }
            await ProcessRequest.post('/api/v1/process/equipDailyCheck/batchSave', this.deviceChecks)
            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'))
  }
}
