import { request} from '@/utils/request'
import { update } from 'three/examples/jsm/libs/tween.module.js'

export interface Device{
    id:number,
    deviceCode:string,
    deviceName:string,
    deviceTypeId:number,
    deviceType:string,
    sectionId:number,
    sectionName?:string,
    spec:string,
    manufacturer:string,
    manufactureDate:string,
    workshopId:number,
    remark:string,
    status:number,
    deviceFlag?:string,
    /** 新代 SynFactory equipID */
    synEquipId?: number | null,
    createTime:string,
    updateTime:string
    abnormalRecordList:[],
    deviceManagers:[]
    deviceManager:string
}

export interface DeviceRealtimeVO {
  deviceId?: number
  deviceCode?: string
  displayName: string
  synEquipId: number
  bound: boolean
  connected?: number | null
  cncStatus?: number | null
  cncStatusText?: string
  partCount?: number | null
  mainProgram?: string | null
  alarmCount?: number
  alarmMessages?: string[]
  dataTimestamp?: number
  sectionName?: string
}


export const deviceApi = {

    page(params:{
        page:number,
        pageSize:number,
        deviceCode:any
        deviceFlag?: string
    }) {
        return request({
            url:"/biz/device/page",
            method:"get",
            params
        })
    },

    /** 设备机联网看板：MES名称 + 新代即时状态 */
    realtimeBoard(equipIds?: string | number[]) {
        const params: Record<string, any> = {}
        if (Array.isArray(equipIds) && equipIds.length) {
            params.equipIds = equipIds.join(',')
        } else if (typeof equipIds === 'string' && equipIds) {
            params.equipIds = equipIds
        }
        return request<DeviceRealtimeVO[]>({
            url: '/biz/device/realtime-board',
            method: 'get',
            params
        })
    },

    create(data:Device) {
        return request({
            url:"/biz/device",
            method:"post",
            data
        })
    },

    update(data:Device) {
        return request({
            url:"/biz/device",
            method:"put",
            data
        })
    },

    list(sectionId:number) {
        return request({
            url:`/biz/device/list/${sectionId}`,
            method:"get"
        })
    },
    delete(ids:any) {
        return request({
            url:`/biz/device/${ids.join(",")}`,
            method:"delete"
        })

    },
    export(
        params?:{
            ids?:string[],
            deviceCode:any,
            deviceFlag?:string
        }){
            const p:Record<string,any> = {}
        console.log( params)
        if(params?.ids?.length) p.ids = params.ids.join(',')
        if(params?.deviceCode != undefined && params?.deviceCode !== null) p.sectionCode = params.deviceCode
        if(params?.deviceFlag != undefined && params?.deviceFlag !== null) p.deviceFlag = params.deviceFlag
        return request({
            url:`/biz/device/report`,
            method:"get",
            params:p,
            responseType:"blob"
        })
    
    },
    getDeviceList(params:{sectionIds:any}){
        return request({
            url:"/biz/device/getDeviceList",
            method:"get",
            params
        })
    },
    deviceList() {
        return request({
            url:"/biz/device/list",
            method:"get"
        })
    }
}