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

/**
 * 获取产品子类别代码列表
 * @param {String} parentCode 上级税收分类编码,获取最顶级类别代码参数值为空字符串（‘’）
 */
function GetProductChildCateCodes(parentCode = '') {
    const url = '/sys/product/childCateCodes'
    let token = cache.getToken()
    let args = {
        parentCode: parentCode
    }
    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)
                }
            })
    })
}

/**
 * 获取产品名称检索结果树
 * @param {String} nameKeyword 关键字
 */
function GetProductCateCodeTree(nameKeyword) {
    const url = '/sys/product/cateCodeTree'
    let token = cache.getToken()
    let args = {
        nameKeyword: nameKeyword
    }
    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)
                }
            })
    })
}

/**
 * 商品批量赋码
 * @param {Array} productIds 产品id 集合
 * @param {String} cateCode 税收分类编码
 * @param {String} cateName 编码名称
 * @param {String} cateAbbr 编码简称
 */
function ProductBatchSaveCode(productIds, cateCode, cateName, cateAbbr) {
    const url = '/sys/product/batchSaveCode'
    let token = cache.getToken()
    let args = {
        productIds: productIds,
        cateCode: cateCode,
        cateName: cateName,
        cateAbbr: cateAbbr,
        reqChannel: 4,
    }
    console.log('商品批量赋码 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(val => {
                console.log('商品批量赋码 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

export {
    GetProductChildCateCodes,
    GetProductCateCodeTree,
    ProductBatchSaveCode
}
