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

/**
 * 搜索企业的产品（商品管理-分页条件查询）
 * @param {String} productName : 产品名称  模糊查询
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function searchEntProduct({ productName = '', pageNum = 1, pageSize = 10, groupId = '' }) {
    const url = '/sys/product/findPage'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        mobile: mobile,
        entTaxId: entTaxId,
        groupId: groupId,
        reqChannel: 4,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (productName != '') {
        args.productName = productName
    }
    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} taxationCateCode 税收分类编码
 * @param {String} taxationCateName 税收分类名称
 * @param {String} taxationCateAbbr 税收分类简称
 * @param {String} productId 产品ID，修改商品时必填，默认值空字符串
 * @param {String} productName 产品名称
 * @param {String} shortCode 简码
 * @param {String} specsModel 规格型号
 * @param {float} price 单价
 * @param {String} unit 单位
 * @param {Boolean} taxPriceFlag 含税价标志
 * @param {float} taxRate 税率
 * @param {Number} zeroRateFlag 零税率标识
 * @param {Number} preferentialFlag 是否使用优惠政策
 * @param {String} preferentialType 优惠政策类型
 */
function productModifyOrAdd({
    taxationCateCode, taxationCateName, taxationCateAbbr,
    productId = '', productName, shortCode, specsModel, price, unit, taxPriceFlag,
    taxRate, zeroRateFlag, preferentialFlag, preferentialType, groupId }) {
    const url = '/sys/product/save'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        taxationCateCode: taxationCateCode,
        taxationCateName: taxationCateName,
        taxationCateAbbr: taxationCateAbbr,
        productName: productName,
        shortCode: shortCode,
        specsModel: specsModel,
        price: price,
        unit: unit,
        taxPriceFlag: taxPriceFlag,
        taxRate: taxRate,
        zeroRateFlag: zeroRateFlag,
        preferentialFlag: preferentialFlag,
        preferentialType: preferentialType,
        mobile: mobile,
        reqChannel: 4,
        entTaxId: entTaxId,
        groupId: groupId
    }
    if (productId) {
        args.productId = productId
    }
    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)
                }
            })
    })
}

/**
 * 商品管理-删除商品
 * @param {Array} productList : 删除商品列表
 */
function deleteProduct(delList) {
    let mobile = JSON.parse(cache.getSession('user')).account
    const url = '/sys/product/delete'
    let token = cache.getToken()
    let args = {
        mobile: mobile,
        reqChannel: 4,
        delList: delList
    }
    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)
                }
            })
    })
}

/**
 * 商品智能赋码查询
 * @param {String} productName : 商品名称
 */
function znfmCommoditySearch(productName) {
    const url = '/sys/product/commoditySearch'
    let token = cache.getToken()
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        productName: productName,
        entTaxId: entTaxId
    }
    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} taxCateCode 对应产品的税收分类编码
 */
function getEntTaxRates(taxCateCode) {
    const url = '/sys/entInfo/rates'
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let token = cache.getToken()
    let args = {
        entTaxId: entTaxId,
        taxCateCode: taxCateCode
    }
    console.log('获取企业可用税率 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(v => {
                console.log('获取企业可用税率 结果：')
                console.log(v)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 获取优惠政策类型列表
 */
function getPreferentials() {
    const url = '/auth/param/getPreferentials'
    let token = cache.getToken()
    let 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} mobile : 查找条件：用户手机号
 * @param {String} name : 查找条件：用户姓名
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function searchAccountUsers({ searchUserMobile = '', searchUserName = '', pageNum = 1, pageSize = 10 }) {
    let mobile = JSON.parse(cache.getSession('user')).account
    const url = '/auth/comm/user/findChildUsers'
    let token = cache.getToken()
    let args = {
        currentUserMobile: mobile,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (searchUserMobile != '') {
        args.mobile = searchUserMobile
    }
    if (searchUserName != '') {
        args.name = searchUserName
    }
    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} delUserMobile : 要删除的用户手机号
 */
function entUserDelete(delUserMobile) {
    let mobile = JSON.parse(cache.getSession('user')).account
    const url = '/auth/ent/user/delete'
    let token = cache.getToken()
    let args = {
        currentUserMobile: mobile,
        reqChannel: 4,
        mobile: delUserMobile
    }
    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)
                }
            })
    })
}

/**
 * 根据企业用户手机号获取对应管理相关信息
 * @param {String} mobile : 企业用户的手机号
 */
function getEntUserDetail(mobile) {
    const url = '/auth/ent/user/findManageInfoByMobile'
    let token = cache.getToken()
    let args = {
        mobile: mobile
    }
    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)
                }
            })
    })
}

/**
 * 获取企业用户角色
 */
function getEntUserRoles() {
    const url = '/auth/ent/role/findCategoryRoles'
    let token = cache.getToken()
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, {}, false, 2)
            .then(val => {
                console.log('获取企业用户角色 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 企业管理者增加开票相关业务人员
 * @param {String} userMobile : 增加用户的手机号
 */
function addEntUser({ userMobile, name, role, bindEntList }) {
    let mobile = JSON.parse(cache.getSession('user')).account
    const url = '/auth/ent/user/save'
    let token = cache.getToken()
    let args = {
        mobile: userMobile,
        name: name,
        currentUserMobile: mobile,
        reqChannel: 4,
        roleIds: [role],
        entTaxIds: bindEntList
    }
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 企业管理者更改业务员管理信息
 * @param {String} userMobile : 修改用户的手机号
 * @param {String} name : 用户名
 * @param {String} role : 修改赋予的角色
 * @param {Array} bindEntList : 修改绑定管理的企业
 * @param {Boolean} status : 用户是否有效 默认为true
 */
function modifyEntUser({ userMobile, name, role, bindEntList, status = true }) {
    let mobile = JSON.parse(cache.getSession('user')).account
    const url = '/auth/ent/user/updateManageInfo'
    let token = cache.getToken()
    let args = {
        mobile: userMobile,
        name: name,
        currentUserMobile: mobile,
        reqChannel: 4,
        roleIds: [role],
        entTaxIds: bindEntList,
        status: status
    }
    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)
                }
            })
    })
}

/**
 * 客户搜索
 * @param {String} customerName : 客户姓名
 * @param {String} customerCode : 客户编码
 * @param {String} customerEntTaxId : 客户企业税号
 * @param {string} groupId 分组id
 * @param {boolean} hasGroup 是否已分组
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function customerSearch({ customerName = '', customerCode = '', customerEntTaxId = '', groupId = '', hasGroup = '', pageNum = 1, pageSize = 10 }) {
    const url = '/sys/customer/findPage'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        mobile: mobile,
        entTaxId: entTaxId,
        reqChannel: 4,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (customerName != '') {
        args.customerName = customerName
    }
    if (customerCode != '') {
        args.customerCode = customerCode
    }
    if (customerEntTaxId != '') {
        args.entTaxId = customerEntTaxId
    }
    if (groupId) {
        args.groupId = groupId;
    }
    if (hasGroup) {
        args.hasGroup = hasGroup;
    }
    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} delList : 要删除的客户列表
 */
function customerDelete(delList) {
    const url = '/sys/customer/delete'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        delList: delList,
        reqChannel: 4,
        mobile: mobile
    }
    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)
                }
            })
    })
}

/**
 * 客户信息修改或新增客户
 * @param {String} customerId : 客户ID，修改客户信息，此字段为客户信息唯一标识；
 * @param {String} customerName : 客户姓名
 * @param {Number} customerType : 客户类型
 * @param {String} customerCode : 客户编码
 * @param {String} parentCode : 上级编码
 * @param {String} customerEntTaxId : 客户企业税号
 * @param {String} shortCode : 简码
 * @param {String} email : 客户邮箱（接收电子发票）
 * @param {String} customerMobile : 客户手机号（接收电子发票）
 * @param {String} address : 地址
 * @param {String} contactPhone : 客户联系电话
 * @param {String} bankName : 开户银行
 * @param {String} bankAccountNumber : 银行账号
 */
function customerModifyOrAdd({ customerId = '', customerName, customerType,
    customerCode = '', parentCode = '', customerEntTaxId = '', shortCode = '', email, groupId = '',
    customerMobile, address = '', contactPhone = '', bankName = '', bankAccountNumber = '' }) {
    const url = '/sys/customer/save'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        customerName: customerName,
        customerType: customerType,
        email: email,
        groupId: groupId,
        customerMobile: customerMobile, // 客户手机号
        reqChannel: 4,
        mobile: mobile,
        entTaxId: entTaxId
    }
    // 修改客户信息，此字段为客户信息唯一标识；
    if (customerId != '') {
        args.customerId = customerId
    }
    if (customerCode != '') {
        args.customerCode = customerCode
    }
    if (parentCode != '') {
        args.parentCode = parentCode
    }
    if (customerEntTaxId != '') {
        args.customerEntTaxId = customerEntTaxId
    }
    if (shortCode != '') {
        args.shortCode = shortCode
    }
    if (address != '') {
        args.address = address
    }
    if (bankName != '') {
        args.bankName = bankName
    }
    if (bankAccountNumber != '') {
        args.bankAccountNumber = bankAccountNumber
    }
    if (contactPhone != '') {
        args.contactPhone = contactPhone
    }
    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)
                }
            })
    })
}

/**
 * 企业云抬头查询
 * @param {String} entName : 企业名称
 */
function yttCompanySearch(entName) {
    const url = '/sys/customer/companySearch'
    let token = cache.getToken()
    let args = {
        entName: entName
    }
    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} entName : 企业名称
 */
function findCustomerGroup({ groupName = '', pageNum = 1, pageSize = 10 }) {
    const url = '/sys/customer/group/findPage'
    let token = cache.getToken()
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        entTaxId: entTaxId,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (groupName) {
        args.groupName = groupName;
    }
    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} groupId 分组id,修改时必输
 * @param {string} groupName 分组名称
 * @param {string} groupDesc 分组描述
 * @param {number} sort 排序序号
 */
function saveCustomerGroup({ groupId = '', groupName, groupDesc = '', sort }) {
    const url = '/sys/customer/group/save'
    let token = cache.getToken()
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        groupName: groupName,
        sort: sort,
        entTaxId: entTaxId,
        reqChannel: 4,
    }
    if (groupId) {
        args.groupId = groupId;
    }
    if (groupDesc) {
        args.groupDesc = groupDesc;
    }
    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)
                }
            })
    })
}

/**
 * 客户管理-删除客户分组信息
 * @param {object} delList 要删除的分组id 集合
 */
function deleteCustomerGroup(delList) {
    const url = '/sys/customer/group/delete'
    let token = cache.getToken()
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        delList: delList,
    }
    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)
                }
            })
    })
}

/**
 * 客户管理-变更客户分组信息
 * @param {Array} moveList
 */
function changeCustomerGroup(moveList) {
    const url = '/sys/customer/changeGroup'
    let token = cache.getToken()
    // let args = {
    //     groupId: groupId,
    //     customerList: customerId,
    // }
    console.log('变更客户分组信息 参数：')
    console.log(moveList)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, moveList, false, 1)
            .then(val => {
                console.log('变更客户分组信息 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 客户管理-获取用户搜索历史
 * @param {Array} moveList
 */
 function getSearchHistory() {
    const url = '/sys/customer/getSearchHis'
    let token = cache.getToken()
    let user = JSON.parse(cache.getSession('user'))
    let args = {
        userId: user.userId,
    }
    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 {
    searchEntProduct,
    productModifyOrAdd,
    deleteProduct,
    znfmCommoditySearch,
    getEntTaxRates,
    getPreferentials,
    searchAccountUsers,
    entUserDelete,
    getEntUserDetail,
    getEntUserRoles,
    addEntUser,
    modifyEntUser,
    customerSearch,
    customerDelete,
    customerModifyOrAdd,
    yttCompanySearch,
    findCustomerGroup,
    saveCustomerGroup,
    deleteCustomerGroup,
    changeCustomerGroup,
    getSearchHistory
}
