import { uploadFile } from '@/api/system/oss';
import { VXETable, VxeColumnPropTypes } from 'vxe-table';

export const cmToPx = (cm: number, dpi = 96) => {
    return (cm * dpi) / 2.54;
};
export const importFileGetUrl = async (types: string[] = ['xlsx', 'xls']) => {
    const { file } = await VXETable.readFile({
        types
    });
    const curPath = URL.createObjectURL(file);
    const formData = new FormData();
    formData.append('file', file, file.name);
    const { data } = await uploadFile(formData);
    return data;
};
export const importFileGetUrls = async (types: string[] = ['png', 'jpg'], multiple = true) => {
    const { files } = await VXETable.readFile({
        types,
        multiple
    });
    const promises = Array.from(files).map((file: File) => importFileGetUrlByFile(file));
    return Promise.all(promises);
};
// 传入file， file.name 利用formData上传，用
export const importFileGetUrlByFile = async (file: File) => {
    const formData = new FormData();
    formData.append('file', file, file.name);
    return uploadFile(formData);
};
export const filterBool = (val: string) => {
    if (val) {
        return +val ? '是' : '否';
    } else {
        return '-';
    }
};
export const colNoData: VxeColumnPropTypes.Formatter<any> = ({ cellValue }) => {
    if (cellValue) {
        return cellValue;
    } else {
        return '-';
    }
};

export const formatterfilterBool: VxeColumnPropTypes.Formatter<any> = ({ cellValue }) => {
    if (cellValue) {
        return +cellValue ? '是' : '否';
    } else {
        return '-';
    }
};

export const formatWarehouseRow = (warehouseInfo: any) => {
    if (warehouseInfo && warehouseInfo.length) {
        const strTem: string[] = [];
        for (let i = 0; i < warehouseInfo.length; i++) {
            const { warehouseName = '', shelvesName = '', warehouseSn = '', shelvesSn = '' } = warehouseInfo[i];
            const strKf = warehouseName ? `${warehouseName}${warehouseSn ? '(' + warehouseSn + ')' : ''}` : '';
            const strCk = shelvesName ? `${shelvesName}${shelvesSn ? '(' + shelvesSn + ')' : ''}` : '';
            strTem.push(strKf + (strCk ? '/' + strCk : ''));
        }
        return strTem.toString();
    } else {
        return '-';
    }
};
// 格式化产地
export const formatOrigin = (adcdCodeDesc: any, nationTypeDesc: any) => {
    if (nationTypeDesc === '中国大陆' || !nationTypeDesc) {
        return adcdCodeDesc || '-';
    } else {
        return nationTypeDesc || '-';
    }
};
