import apis from '../utils/request';
import { getConfigByEnv } from "../utils/index"
const { GET, POST, PUT, DELETE } = apis

export const uploadFile = (filePath) => {
    const { api } = getConfigByEnv();
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            filePath: filePath,
            name: 'file',
            url: `${api}/file/upload`,
            header: {
                authorization: 'Bearer ' + wx.getStorageSync("token")
            },
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

export const uploadOcrFile = (filePath) => {
    const { api } = getConfigByEnv();
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            
            filePath: filePath,
            name: 'data',
            url: `${api}/image/to/meter/reading`,
            header: {
                authorization: 'Bearer ' + wx.getStorageSync("token")
            },
            success: (res) => {
                resolve(res?.data ? JSON.parse(res?.data) : res?.data);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

export const uploadInstallMeter = (filePath) => {
    const { api } = getConfigByEnv();
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            filePath: filePath,
            name: 'data',
            url: `${api}/image/meter/reading`,
            header: {
                authorization: 'Bearer ' + wx.getStorageSync("token")
            },
            success: (res) => {
                resolve(res?.data ? JSON.parse(res?.data) : res?.data);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

export const uploadMeterForce = (filePath) => {
    const { api } = getConfigByEnv();
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            filePath: filePath,
            name: 'data',
            url: `${api}/image/meter/force`,
            header: {
                authorization: 'Bearer ' + wx.getStorageSync("token")
            },
            success: (res) => {
                resolve(res?.data ? JSON.parse(res?.data) : res?.data);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}
export const uploadUninstallMeter = (filePath) => {
    const { api } = getConfigByEnv();
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            filePath: filePath,
            name: 'data',
            url: `${api}/image/meter/delete`,
            header: {
                authorization: 'Bearer ' + wx.getStorageSync("token")
            },
            success: (res) => {
                resolve(res?.data ? JSON.parse(res?.data) : res?.data);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

export const uploadPublicFile = (filePath) => {
    const { api } = getConfigByEnv();
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            filePath: filePath,
            name: 'file',
            url: `${api}/wx/public/upload`,
            header: {
                authorization: wx.getStorageSync("token")
            },
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}