import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

/**
 * 获取本地缓存的企业设备列表；
 */
function GetLocalDevices() {
    console.log('获取本地缓存的企业设备列表')
    console.log(JSON.parse(cache.getSession('devices')))
    return JSON.parse(cache.getSession('devices'))
}

/**
 * 获取本地缓存的企业设备信息
 * @param {String} deviceKey 设备的唯一标识
 */
function GetLocalDevice(deviceKey) {
    let result = null
    for (const item of GetLocalDevices()) {
        if (item.deviceKey === deviceKey) {
            result = item
            break
        }
    }
    return result
}

/**
 * 获取企业设备文本显示信息
 * @param {String} deviceKey 设备的唯一标识
 */
function GetDeviceLabel(deviceKey) {
    let result = deviceKey
    for (const item of GetLocalDevices()) {
        if (item.deviceKey === deviceKey) {
            result = item.deviceLabel
            break
        }
    }
    return result
}

/**
 * 企业设备监控信息查询
 */
function EntDeviceMonitorInfo() {
    const url = '/sys/entDevice/monitorInfo'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId
    }
    console.log('企业设备监控信息查询 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('企业设备监控信息查询 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

export {
    GetLocalDevices,
    GetLocalDevice,
    GetDeviceLabel,
    EntDeviceMonitorInfo
}
